aboutsummaryrefslogtreecommitdiff
path: root/src/binlogcat.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-10-13 12:13:46 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-10-13 12:13:46 +0300
commit510fc646f13b843d046323e3477edb6c1bc2258d (patch)
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')
-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
@@ -17,12 +17,13 @@
#include <config.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include "vmod-binlog.h"
#include "pack.h"
@@ -30,12 +31,40 @@ char *progname;
char *timefmt = "%c";
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)
{
FILE *fp;
struct binlog_file_header header;
struct binlog_record *rec;
char timebuf[128];
@@ -49,74 +78,69 @@ catlog(const char *fname)
if (strcmp(fname, "-") == 0)
fp = stdin;
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);
}
if (verbose_option)
printf("# %s; format=%s; recsize=%lu; recnum=%lu\n",
fname, dataspec, header.recsize, header.recnum);
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 -
offsetof(struct binlog_record,data));
env->fp = stdout;
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;
}
if (timediff_option) {
if (i == 0)
start_ts = rec->ts;

Return to:

Send suggestions and report system problems to the System administrator.