From 510fc646f13b843d046323e3477edb6c1bc2258d Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Sun, 13 Oct 2013 12:13:46 +0300 Subject: 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. --- src/binlog.c | 11 +++++ src/binlogcat.c | 56 +++++++++++++++------ src/pack.c | 148 +++++++++++++++++++++++++------------------------------- 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 @@ #include #include #include +#include #include #include #include "vrt.h" @@ -91,7 +92,17 @@ binlog_debug(const char *fmt, ...) } #define debug(c,l,s) do { if ((c)->debug>=(l)) binlog_debug s; } while(0) + +void +packerror(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vsyslog(LOG_DAEMON|LOG_NOTICE, fmt, ap); + va_end(ap); +} + int module_init(struct vmod_priv *priv, const struct VCL_conf *vclconf) { 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 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,34 @@ int number_option; int verbose_option; int timediff_option; +void +verror(const char *fmt, va_list ap) +{ + fprintf(stderr, "%s: ", progname); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); +} + +void +error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verror(fmt, ap); + va_end(ap); +} + +void +packerror(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verror(fmt, ap); + va_end(ap); +} + void catlog(const char *fname) { @@ -52,40 +81,37 @@ catlog(const char *fname) else { fp = fopen(fname, "r"); if (!fp) { - fprintf(stderr, "%s: cannot open %s: %s\n", - progname, fname, strerror(errno)); + error("cannot open %s: %s", strerror(errno)); exit(1); } } if (fread(&header, sizeof(header), 1, fp) != 1) { - fprintf(stderr, "%s: error reading header of %s: %s\n", - progname, fname, strerror(errno)); + error("error reading header of %s: %s", + fname, strerror(errno)); exit(1); } if (memcmp(header.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) { - fprintf(stderr, "%s: %s is not a binlog file\n", - progname, fname); + error("%s is not a binlog file", fname); exit(1); } if (header.version != BINLOG_VERSION) { - fprintf(stderr, "%s: %s: unknown version\n", - progname, fname); + error("%s: unknown version", progname, fname); exit(1); } size = header.hdrsize - sizeof(header); dataspec = malloc(size); if (!dataspec) { - fprintf(stderr, "%s: not enough memory", progname); + error("not enough memory"); abort(); } if (fread(dataspec, size, 1, fp) != 1) { - fprintf(stderr, "%s: error reading header of %s: %s\n", - progname, fname, strerror(errno)); + error("error reading header of %s: %s", + fname, strerror(errno)); exit(1); } @@ -95,15 +121,14 @@ catlog(const char *fname) inst = packcomp(dataspec, &p); if (*p) { - fprintf(stderr, "%s: %s: bad dataspec near %s", - progname, dataspec, p); + error("%s: bad dataspec near %s", dataspec, p); exit(1); } free(dataspec); rec = malloc(header.recsize); if (!rec) { - fprintf(stderr, "%s: not enough memory", progname); + error("not enough memory"); abort(); } env = packenv_create(header.recsize - @@ -112,8 +137,7 @@ catlog(const char *fname) for (i = 0; i < header.recnum; i++) { if (fread(rec, header.recsize, 1, fp) != 1) { - fprintf(stderr, "%s: %s: unexpected eof\n", - progname, fname); + error("%s: unexpected eof", fname); break; } diff --git a/src/pack.c b/src/pack.c index b71823a..9c22932 100644 --- a/src/pack.c +++ b/src/pack.c @@ -118,12 +118,16 @@ getunum(char *s, uintmax_t maxval, uintmax_t *retval) for (; *s && isspace(*s); s++); for (; *s; s++) { - if (!isdigit(*s)) + if (!isdigit(*s)) { + errno = EINVAL; return -1; + } x = x*10 + *s - '0'; } - if (x > maxval) + if (x > maxval) { + errno = ERANGE; return -1; + } *retval = x; return 0; } @@ -140,18 +144,23 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) ++s; } for (; *s; s++) { - if (!isdigit(*s)) + if (!isdigit(*s)) { + errno = EINVAL; return -1; + } x = x*10 + *s - '0'; } if (neg) { - if (x > -minval) + if (x > -minval) { + errno = ERANGE; return -1; + } *retval = -x; - } else if (x > maxval) + } else if (x > maxval) { + errno = ERANGE; return -1; - else + } else *retval = x; return 0; } @@ -165,8 +174,11 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) type v = (type) x; \ memcpy(env->buf_base + env->buf_pos, \ &v, sizeof(v)); \ - } \ - } \ + } else \ + packerror("error converting %s to %s: %s", \ + arg, #type, strerror(errno)); \ + } else \ + packerror("out of arguments"); \ } \ } while(0) @@ -179,8 +191,11 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) type v = (type) x; \ memcpy(env->buf_base + env->buf_pos, \ &v, sizeof(v)); \ - } \ - } \ + } else \ + packerror("error converting %s to %s: %s", \ + arg, #type, strerror(errno)); \ + } else \ + packerror("out of arguments"); \ } \ } while(0) @@ -197,7 +212,8 @@ Z_packer(struct packenv *env, int rep) if (len > rep - 1) len = rep - 1; memcpy(env->buf_base + env->buf_pos, arg, len); - } + } else + packerror("out of arguments"); } env->buf_pos += rep; } @@ -216,6 +232,8 @@ c_packer(struct packenv *env, int rep) char *arg = packenv_get(env); if (arg) env->buf_base[env->buf_pos] = *arg; + else + packerror("out of arguments"); } env->buf_pos++; } @@ -350,8 +368,12 @@ n_packer(struct packenv *env, int rep) uint16_t v = htons((uint16_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint16_t (network order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint16_t); } @@ -376,8 +398,12 @@ N_packer(struct packenv *env, int rep) uint32_t v = htonl((uint32_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint32_t (network order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint32_t); } @@ -402,8 +428,12 @@ v_packer(struct packenv *env, int rep) uint16_t v = ntohs((uint16_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint16_t (host order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint16_t); } @@ -428,8 +458,12 @@ V_packer(struct packenv *env, int rep) uint32_t v = ntohl((uint32_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint32_t (host order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint32_t); } @@ -454,7 +488,13 @@ f_packer(struct packenv *env, int rep) errno = 0; v = strtof(arg, &p); - if (*p == 0 && errno == 0) + if (*p) + packerror("error converting %s to %s (near %s)", + arg, "float", p); + else if (errno) + packerror("error converting %s to %s: %s", + arg, "float", strerror(errno)); + else memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); } @@ -482,7 +522,13 @@ d_packer(struct packenv *env, int rep) errno = 0; v = strtod(arg, &p); - if (*p == 0 && errno == 0) + if (*p) + packerror("error converting %s to %s (near %s)", + arg, "double", p); + else if (errno) + packerror("error converting %s to %s: %s", + arg, "double", strerror(errno)); + else memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); } @@ -760,63 +806,3 @@ packenv_init(struct packenv *env) memset(env->buf_base, 0, env->buf_size); env->buf_pos = 0; } - -#ifdef STANDALONE -#include - -int -main(int argc, char **argv) -{ - void (*fn)(struct packinst *pi, struct packenv *env) = packin; - struct packinst *pi; - struct packenv *env; - char *end; - int c; - - while ((c = getopt(argc, argv, "d")) != EOF) { - switch (c) { - case 'd': - fn = packout; - break; - default: - exit(1); - } - } - - argc -= optind; - argv += optind; - - if (argc == 0) - abort(); - - pi = packcomp(argv[0], &end); - if (!pi) { - fprintf(stderr, "out of memory\n"); - abort(); - } - if (*end) { - fprintf(stderr, "compile error near %s\n", end); - exit(1); - } - env = packenv_create(packsize(pi)); - if (!env) { - fprintf(stderr, "out of memory\n"); - abort(); - } - - env->fp = stdout; - env->argv = argv + 1; - env->argc = argc - 1; - - if (fn == packout) - fread(env->buf_base, env->buf_size, 1, stdin); - - fn(pi, env); - if (fn == packin) { - fwrite(env->buf_base, env->buf_size, 1, stdout); - } - - packenv_free(env); - packfree(pi); -} -#endif diff --git a/src/pack.h b/src/pack.h index 36e20ec..c134878 100644 --- a/src/pack.h +++ b/src/pack.h @@ -40,7 +40,7 @@ struct packenv *packenv_create(size_t size); void packenv_init(struct packenv *env); void packenv_free(struct packenv *env); - +void packerror(const char *, ...); -- cgit v1.2.1