summaryrefslogtreecommitdiffabout
path: root/src/binlogcat.c
blob: 219f78cce1a8fa821db4178ce1797acc3cc36724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This file is part of vmod-binlog
   Copyright (C) 2013 Sergey Poznyakoff

   Vmod-binlog is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Vmod-binlog is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with vmod-binlog.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include "vmod-binlog.h"

char *progname;
char *timefmt = "%c";
int number_option;
int verbose_option;
int timediff_option;

void
catlog(const char *fname)
{
	FILE *fp;
	union binlog_header header;
	struct binlog_record rec;
	char timebuf[128];
	size_t i;
	time_t start_ts;
	
	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));
			exit(1);
		}
	}

	if (fread(&header, sizeof(header), 1, fp) != 1) {
		fprintf(stderr, "%s: error reading header of %s: %s\n",
			progname, fname, strerror(errno));
		exit(1);
	}

	if (memcmp(header.hdr.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) {
		fprintf(stderr, "%s: %s is not a binlog file\n",
			progname, fname);
		exit(1);
	}

	if (header.hdr.version != BINLOG_VERSION) {
		fprintf(stderr, "%s: %s: unknown version\n",
			progname, fname);
		exit(1);
	}

	if (header.hdr.recsize != sizeof(struct binlog_record)) {
		fprintf(stderr, "%s: %s: record length mismatch\n",
			progname, fname);
		exit(1);
	}

	if (verbose_option)
		printf("# %s; %lu records\n", fname, header.hdr.recnum);
	
	for (i = 0; i < header.hdr.recnum; i++) {
		if (fread(&rec, sizeof(rec), 1, fp) != 1) {
			fprintf(stderr, "%s: %s: unexpected eof\n",
				progname, fname);
			break;
		}

		if (timediff_option) {
			if (i == 0)
				start_ts = rec.ts;
			rec.ts -= start_ts;
		}
		
		strftime(timebuf, sizeof timebuf, timefmt, localtime(&rec.ts));
		if (number_option)
			printf("%lu ", (unsigned long) i);
		printf("%s %ld %ld\n", timebuf, rec.nid, rec.aid);
	}
	
	fclose(fp);
}

void
help()
{
	printf("usage: %s [-dhnv] [t FORMAT] [FILE...]\n");
}

int
main(int argc, char **argv)
{
	progname = argv[0];
	int c;
	
	while ((c = getopt(argc, argv, "dht:nv")) != EOF)
		switch (c) {
		case 'd':
			timediff_option = 1;
			timefmt = "%s";
			break;
		case 'h':
			help();
			return 0;
		case 't':
			timefmt = optarg;
			break;
		case 'n':
			number_option = 1;
			break;
		case 'v':
			verbose_option = 1;
			break;
		default:
			exit(1);
		}

	argc -= optind;
	argv += optind;
	
	if (argc == 0)
		catlog("-");
	else while (argc--)
		     catlog(*(argv++));
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.