diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-13 12:13:46 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-13 12:13:46 +0300 |
commit | 510fc646f13b843d046323e3477edb6c1bc2258d (patch) | |
tree | 3de9ea346897300e00ef169fcc84ab508b3d70a3 | |
parent | fcc1c011757f71bb3ec1c883a19c1945fd83cf11 (diff) | |
download | vmod-binlog-510fc646f13b843d046323e3477edb6c1bc2258d.tar.gz vmod-binlog-510fc646f13b843d046323e3477edb6c1bc2258d.tar.bz2 |
Improve error reporting.
* src/pack.c: Use packerror to report errors.
* src/pack.h (packerror): New proto.
* src/binlog.c (packerror): New function.
* src/binlogcat.c (packerror,error,verror): New functions.
-rw-r--r-- | src/binlog.c | 11 | ||||
-rw-r--r-- | src/binlogcat.c | 56 | ||||
-rw-r--r-- | src/pack.c | 148 | ||||
-rw-r--r-- | src/pack.h | 2 |
4 files changed, 119 insertions, 98 deletions
diff --git a/src/binlog.c b/src/binlog.c index 325f337..7e88817 100644 --- a/src/binlog.c +++ b/src/binlog.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <syslog.h> | 24 | #include <syslog.h> |
25 | #include <stddef.h> | 25 | #include <stddef.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <stdarg.h> | ||
27 | #include <ctype.h> | 28 | #include <ctype.h> |
28 | #include <time.h> | 29 | #include <time.h> |
29 | #include "vrt.h" | 30 | #include "vrt.h" |
@@ -91,7 +92,17 @@ binlog_debug(const char *fmt, ...) | |||
91 | } | 92 | } |
92 | 93 | ||
93 | #define debug(c,l,s) do { if ((c)->debug>=(l)) binlog_debug s; } while(0) | 94 | #define debug(c,l,s) do { if ((c)->debug>=(l)) binlog_debug s; } while(0) |
95 | |||
96 | void | ||
97 | packerror(const char *fmt, ...) | ||
98 | { | ||
99 | va_list ap; | ||
94 | 100 | ||
101 | va_start(ap, fmt); | ||
102 | vsyslog(LOG_DAEMON|LOG_NOTICE, fmt, ap); | ||
103 | va_end(ap); | ||
104 | } | ||
105 | |||
95 | int | 106 | int |
96 | module_init(struct vmod_priv *priv, const struct VCL_conf *vclconf) | 107 | module_init(struct vmod_priv *priv, const struct VCL_conf *vclconf) |
97 | { | 108 | { |
diff --git a/src/binlogcat.c b/src/binlogcat.c index dd36337..8cdfaaa 100644 --- a/src/binlogcat.c +++ b/src/binlogcat.c | |||
@@ -20,6 +20,7 @@ | |||
20 | #include <stddef.h> | 20 | #include <stddef.h> |
21 | #include <stdio.h> | 21 | #include <stdio.h> |
22 | #include <stdlib.h> | 22 | #include <stdlib.h> |
23 | #include <stdarg.h> | ||
23 | #include <errno.h> | 24 | #include <errno.h> |
24 | #include <time.h> | 25 | #include <time.h> |
25 | #include <string.h> | 26 | #include <string.h> |
@@ -33,6 +34,34 @@ int verbose_option; | |||
33 | int timediff_option; | 34 | int timediff_option; |
34 | 35 | ||
35 | void | 36 | void |
37 | verror(const char *fmt, va_list ap) | ||
38 | { | ||
39 | fprintf(stderr, "%s: ", progname); | ||
40 | vfprintf(stderr, fmt, ap); | ||
41 | fputc('\n', stderr); | ||
42 | } | ||
43 | |||
44 | void | ||
45 | error(const char *fmt, ...) | ||
46 | { | ||
47 | va_list ap; | ||
48 | |||
49 | va_start(ap, fmt); | ||
50 | verror(fmt, ap); | ||
51 | va_end(ap); | ||
52 | } | ||
53 | |||
54 | void | ||
55 | packerror(const char *fmt, ...) | ||
56 | { | ||
57 | va_list ap; | ||
58 | |||
59 | va_start(ap, fmt); | ||
60 | verror(fmt, ap); | ||
61 | va_end(ap); | ||
62 | } | ||
63 | |||
64 | void | ||
36 | catlog(const char *fname) | 65 | catlog(const char *fname) |
37 | { | 66 | { |
38 | FILE *fp; | 67 | FILE *fp; |
@@ -52,40 +81,37 @@ catlog(const char *fname) | |||
52 | else { | 81 | else { |
53 | fp = fopen(fname, "r"); | 82 | fp = fopen(fname, "r"); |
54 | if (!fp) { | 83 | if (!fp) { |
55 | fprintf(stderr, "%s: cannot open %s: %s\n", | 84 | error("cannot open %s: %s", strerror(errno)); |
56 | progname, fname, strerror(errno)); | ||
57 | exit(1); | 85 | exit(1); |
58 | } | 86 | } |
59 | } | 87 | } |
60 | 88 | ||
61 | if (fread(&header, sizeof(header), 1, fp) != 1) { | 89 | if (fread(&header, sizeof(header), 1, fp) != 1) { |
62 | fprintf(stderr, "%s: error reading header of %s: %s\n", | 90 | error("error reading header of %s: %s", |
63 | progname, fname, strerror(errno)); | 91 | fname, strerror(errno)); |
64 | exit(1); | 92 | exit(1); |
65 | } | 93 | } |
66 | 94 | ||
67 | if (memcmp(header.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) { | 95 | if (memcmp(header.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) { |
68 | fprintf(stderr, "%s: %s is not a binlog file\n", | 96 | error("%s is not a binlog file", fname); |
69 | progname, fname); | ||
70 | exit(1); | 97 | exit(1); |
71 | } | 98 | } |
72 | 99 | ||
73 | if (header.version != BINLOG_VERSION) { | 100 | if (header.version != BINLOG_VERSION) { |
74 | fprintf(stderr, "%s: %s: unknown version\n", | 101 | error("%s: unknown version", progname, fname); |
75 | progname, fname); | ||
76 | exit(1); | 102 | exit(1); |
77 | } | 103 | } |
78 | 104 | ||
79 | size = header.hdrsize - sizeof(header); | 105 | size = header.hdrsize - sizeof(header); |
80 | dataspec = malloc(size); | 106 | dataspec = malloc(size); |
81 | if (!dataspec) { | 107 | if (!dataspec) { |
82 | fprintf(stderr, "%s: not enough memory", progname); | 108 | error("not enough memory"); |
83 | abort(); | 109 | abort(); |
84 | } | 110 | } |
85 | 111 | ||
86 | if (fread(dataspec, size, 1, fp) != 1) { | 112 | if (fread(dataspec, size, 1, fp) != 1) { |
87 | fprintf(stderr, "%s: error reading header of %s: %s\n", | 113 | error("error reading header of %s: %s", |
88 | progname, fname, strerror(errno)); | 114 | fname, strerror(errno)); |
89 | exit(1); | 115 | exit(1); |
90 | } | 116 | } |
91 | 117 | ||
@@ -95,15 +121,14 @@ catlog(const char *fname) | |||
95 | 121 | ||
96 | inst = packcomp(dataspec, &p); | 122 | inst = packcomp(dataspec, &p); |
97 | if (*p) { | 123 | if (*p) { |
98 | fprintf(stderr, "%s: %s: bad dataspec near %s", | 124 | error("%s: bad dataspec near %s", dataspec, p); |
99 | progname, dataspec, p); | ||
100 | exit(1); | 125 | exit(1); |
101 | } | 126 | } |
102 | free(dataspec); | 127 | free(dataspec); |
103 | 128 | ||
104 | rec = malloc(header.recsize); | 129 | rec = malloc(header.recsize); |
105 | if (!rec) { | 130 | if (!rec) { |
106 | fprintf(stderr, "%s: not enough memory", progname); | 131 | error("not enough memory"); |
107 | abort(); | 132 | abort(); |
108 | } | 133 | } |
109 | env = packenv_create(header.recsize - | 134 | env = packenv_create(header.recsize - |
@@ -112,8 +137,7 @@ catlog(const char *fname) | |||
112 | 137 | ||
113 | for (i = 0; i < header.recnum; i++) { | 138 | for (i = 0; i < header.recnum; i++) { |
114 | if (fread(rec, header.recsize, 1, fp) != 1) { | 139 | if (fread(rec, header.recsize, 1, fp) != 1) { |
115 | fprintf(stderr, "%s: %s: unexpected eof\n", | 140 | error("%s: unexpected eof", fname); |
116 | progname, fname); | ||
117 | break; | 141 | break; |
118 | } | 142 | } |
119 | 143 | ||
@@ -118,12 +118,16 @@ getunum(char *s, uintmax_t maxval, uintmax_t *retval) | |||
118 | 118 | ||
119 | for (; *s && isspace(*s); s++); | 119 | for (; *s && isspace(*s); s++); |
120 | for (; *s; s++) { | 120 | for (; *s; s++) { |
121 | if (!isdigit(*s)) | 121 | if (!isdigit(*s)) { |
122 | errno = EINVAL; | ||
122 | return -1; | 123 | return -1; |
124 | } | ||
123 | x = x*10 + *s - '0'; | 125 | x = x*10 + *s - '0'; |
124 | } | 126 | } |
125 | if (x > maxval) | 127 | if (x > maxval) { |
128 | errno = ERANGE; | ||
126 | return -1; | 129 | return -1; |
130 | } | ||
127 | *retval = x; | 131 | *retval = x; |
128 | return 0; | 132 | return 0; |
129 | } | 133 | } |
@@ -140,18 +144,23 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) | |||
140 | ++s; | 144 | ++s; |
141 | } | 145 | } |
142 | for (; *s; s++) { | 146 | for (; *s; s++) { |
143 | if (!isdigit(*s)) | 147 | if (!isdigit(*s)) { |
148 | errno = EINVAL; | ||
144 | return -1; | 149 | return -1; |
150 | } | ||
145 | x = x*10 + *s - '0'; | 151 | x = x*10 + *s - '0'; |
146 | } | 152 | } |
147 | 153 | ||
148 | if (neg) { | 154 | if (neg) { |
149 | if (x > -minval) | 155 | if (x > -minval) { |
156 | errno = ERANGE; | ||
150 | return -1; | 157 | return -1; |
158 | } | ||
151 | *retval = -x; | 159 | *retval = -x; |
152 | } else if (x > maxval) | 160 | } else if (x > maxval) { |
161 | errno = ERANGE; | ||
153 | return -1; | 162 | return -1; |
154 | else | 163 | } else |
155 | *retval = x; | 164 | *retval = x; |