-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcbglog
More file actions
executable file
·183 lines (147 loc) · 3.81 KB
/
cbglog
File metadata and controls
executable file
·183 lines (147 loc) · 3.81 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! /usr/bin/perl
# Combine Couchbase log files by timestamp, filter messages
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
use Getopt::Long;
Getopt::Long::Configure("bundling");
my %opt;
sub usage {
return <<EOF;
Usage: $0 [options] ns_server.*.log
Example:
$0 --181 node4/ns_server.*.log
Options:
--181 -1 Logs are in 1.8.1 format (default is 2.0 format)
EOF
}
GetOptions(\%opt,
'181|1!',
) and @ARGV > 0 or die usage();
if ($opt{'181'}) {
$::rx_entry = qr/
# Entry starts with [module:level], e.g., [stats:debug]
^\[(\w+):(\w+)\]
# Followed by a timestamp
\ \[([^\]]+)\]
# Followed by the location of the message
\ \[([^\]]+)\]
# And then the first line of text
(.*)
/x;
}
else {
$::rx_entry = qr/
# Entry starts with [ at start of line
^\[
# module:level, e.g., stats:debug
(\w+):(\w+)
# timestamp
,([^,]+)
# location of the message
,([^\]]+)
# first line of text
\](.*)
/x;
}
my @logs;
foreach my $logname (@ARGV) {
my $l = LogReader->new($logname);
LogReader::put_reader_in_list(\@logs, $l);
}
my $count = 0;
while (@logs) {
my $before = scalar(@logs);
my $l = shift @logs;
print $l->cur_msg, "\n"
#unless $l->{cur}{mod} eq 'couchdb'
;
$l->advance;
LogReader::put_reader_in_list(\@logs, $l);
my $after = scalar(@logs);
#last if ++$count > 999;
}
exit 0;
package LogReader;
sub put_reader_in_list {
my ($list, $log) = @_;
unless ($log->{cur}) {
print STDERR "End of '$log->{n}', list has ", scalar(@$list), " readers\n";
return;
}
my $i;
for ($i = 0; $i < @$list; ++$i) {
last if $log->{cur}{time} lt $list->[$i]{cur}{time};
}
#warn "DEBUG: Splicing $log->{n}:$log->{cur}{time} at $i, list has ", scalar(@$list), " readers\n" if $i > 0;
splice @$list, $i, 0, $log;
}
sub new {
my $class = shift;
my ($logname) = @_;
my $self = bless({}, ref($class) || $class);
$self->{n} = $logname;
open $self->{f}, '<', $self->{n} or die "Can't open $self->{n}: $!\n";
$self->{cur} = undef;
$self->{next} = undef;
# Read top of log file, dump junk until first well-formed line is found
while (defined(my $line = $self->read_line)) {
if (ref $line) {
$self->{next} = $line;
last;
}
#warn "DEBUG ignoring junk: '$line'\n";
}
unless ($self->{next}) {
warn "No entries found in $self->{n}\n";
close $self->{f} or warn "Can't close $self->{n}: $!\n";
return undef;
}
$self->advance;
return $self;
}
# Read a single line from log; if start of entry, return array ref of header
# Otherwise just return the string
sub read_line {
my $self = shift;
my $f = $self->{f};
my $line = <$f>;
chomp $line if defined $line;
return $line unless defined $line and $line =~ /$::rx_entry/o;
return {
mod => $1,
level => $2,
time => $3,
loc => $4,
msg => [$5]
};
}
sub advance {
my $self = shift;
$self->{cur} = $self->{next};
unless ($self->{next}) {
close $self->{f} or warn "Can't close $self->{n}: $!\n";
return undef;
}
$self->{next} = undef;
while (defined(my $line = $self->read_line)) {
if (ref $line) {
$self->{next} = $line;
last;
}
push @{$self->{cur}{msg}}, $line;
}
return 1;
}
sub cur_msg {
my $self = shift;
my $msg = $self->{n} . ": ";
my $c = $self->{cur};
unless ($c) {
$msg .= "EOF";
return $msg;
}
$msg .= sprintf "%s:%s:%s:%s %s", $c->{time}, $c->{mod}, $c->{level}, $c->{loc}, join(' ', @{$c->{msg}});
}