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
@@ -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;
33int timediff_option; 34int timediff_option;
34 35
35void 36void
37verror(const char *fmt, va_list ap)
38{
39 fprintf(stderr, "%s: ", progname);
40 vfprintf(stderr, fmt, ap);
41 fputc('\n', stderr);
42}
43
44void
45error(const char *fmt, ...)
46{
47 va_list ap;
48
49 va_start(ap, fmt);
50 verror(fmt, ap);
51 va_end(ap);
52}
53
54void
55packerror(const char *fmt, ...)
56{
57 va_list ap;
58
59 va_start(ap, fmt);
60 verror(fmt, ap);
61 va_end(ap);
62}
63
64void
36catlog(const char *fname) 65catlog(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

Return to:

Send suggestions and report system problems to the System administrator.