summaryrefslogtreecommitdiffabout
path: root/src/binlogcat.c
authorSergey Poznyakoff <gray@gnu.org.ua>2013-10-13 09:13:46 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2013-10-13 09:13:46 (GMT)
commit510fc646f13b843d046323e3477edb6c1bc2258d (patch) (side-by-side diff)
tree3de9ea346897300e00ef169fcc84ab508b3d70a3 /src/binlogcat.c
parentfcc1c011757f71bb3ec1c883a19c1945fd83cf11 (diff)
downloadvmod-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.
Diffstat (limited to 'src/binlogcat.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/binlogcat.c56
1 files changed, 40 insertions, 16 deletions
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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <string.h>
@@ -33,6 +34,34 @@ 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)
{
FILE *fp;
@@ -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;
}

Return to:

Send suggestions and report system problems to the System administrator.