aboutsummaryrefslogtreecommitdiff
path: root/src/binlogcat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/binlogcat.c')
-rw-r--r--src/binlogcat.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/binlogcat.c b/src/binlogcat.c
new file mode 100644
index 0000000..c51125b
--- /dev/null
+++ b/src/binlogcat.c
@@ -0,0 +1,134 @@
1/* This file is part of vmod-binlog
2 Copyright (C) 2013 Sergey Poznyakoff
3
4 Vmod-binlog is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 Vmod-binlog is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <config.h>
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <errno.h>
23#include <time.h>
24#include <string.h>
25#include "vmod-binlog.h"
26
27char *progname;
28char *timefmt = "%c";
29int number_option;
30int verbose_option;
31
32void
33catlog(const char *fname)
34{
35 FILE *fp;
36 union binlog_header header;
37 struct binlog_record rec;
38 char timebuf[128];
39 size_t i;
40
41 if (strcmp(fname, "-") == 0)
42 fp = stdin;
43 else {
44 fp = fopen(fname, "r");
45 if (!fp) {
46 fprintf(stderr, "%s: cannot open %s: %s\n",
47 progname, fname, strerror(errno));
48 exit(1);
49 }
50 }
51
52 if (fread(&header, sizeof(header), 1, fp) != 1) {
53 fprintf(stderr, "%s: error reading header of %s: %s\n",
54 progname, fname, strerror(errno));
55 exit(1);
56 }
57
58 if (memcmp(header.hdr.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) {
59 fprintf(stderr, "%s: %s is not a binlog file\n",
60 progname, fname);
61 exit(1);
62 }
63
64 if (header.hdr.version != BINLOG_VERSION) {
65 fprintf(stderr, "%s: %s: unknown version\n",
66 progname, fname);
67 exit(1);
68 }
69
70 if (header.hdr.recsize != sizeof(struct binlog_record)) {
71 fprintf(stderr, "%s: %s: record length mismatch\n",
72 progname, fname);
73 exit(1);
74 }
75
76 if (verbose_option)
77 printf("# %s; %lu records\n", fname, header.hdr.recnum);
78
79 for (i = 0; i < header.hdr.recnum; i++) {
80 if (fread(&rec, sizeof(rec), 1, fp) != 1) {
81 fprintf(stderr, "%s: %s: unexpected eof\n",
82 progname, fname);
83 break;
84 }
85
86 strftime(timebuf, sizeof timebuf, timefmt, localtime(&rec.ts));
87 if (number_option)
88 printf("%lu ", (unsigned long) i);
89 printf("%s %ld %ld\n", timebuf, rec.nid, rec.aid);
90 }
91
92 fclose(fp);
93}
94
95void
96help()
97{
98 printf("usage: %s [-hnv] [t FORMAT] [FILE...]\n");
99}
100
101int
102main(int argc, char **argv)
103{
104 progname = argv[0];
105 int c;
106
107 while ((c = getopt(argc, argv, "ht:nv")) != EOF)
108 switch (c) {
109 case 'h':
110 help();
111 return 0;
112 case 't':
113 timefmt = optarg;
114 break;
115 case 'n':
116 number_option = 1;
117 break;
118 case 'v':
119 verbose_option = 1;
120 break;
121 default:
122 exit(1);
123 }
124
125 argc -= optind;
126 argv += optind;
127
128 if (argc == 0)
129 catlog("-");
130 else while (argc--)
131 catlog(*(argv++));
132 return 0;
133}
134

Return to:

Send suggestions and report system problems to the System administrator.