3 # Program to do a grep with dates and times on the debug
6 # grepdbg [nn] [-mm] <regular expression>
8 # nn - is the day you what to look at: 1 is yesterday, 0 is today
9 # and is optional if there is only one argument
11 # -mmm - print the mmm lines before the match. So -3 will print
12 # 4 lines altogether, the 3 lines before the line matching
13 # the regular expression.
15 # <regexp> is the regular expression you are searching for,
16 # a caseless search is done. There can be more than one <regexp>
17 # a <regexp> preceeded by a '!' is treated as NOT <regexp>. Each
18 # <regexp> is implcitly ANDed together.
20 # If you specify something that likes a filename and that filename
21 # has a .pm on the end of it and it exists then rather than doing
22 # the regex match it executes the "main::handle()" function passing
23 # it one line at a time.
31 # search local then perl directories
33 # root of directory tree for this system
35 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
37 unshift @INC, "$root/perl"; # this IS the right way round!
38 unshift @INC, "$root/local";
48 use vars qw(@days $fp $today $string);
51 $fp = DXLog::new('debug', 'dat', 'd');
52 $today = $fp->unixtoj(time());
57 foreach my $arg (@ARGV) {
60 if ($arg =~ /\?|^he?l?p?/) {
64 $nolines += $arg if $arg =~ /^\d+$/;
65 } elsif ($arg =~ /^\d+$/) {
67 } elsif ($arg =~ /\.pm$/) {
72 die "requiring $fn failed $@" if $@;
73 die "required $fn does not contain 'sub handle' (check that 'package main;' exists)" unless main->can('handle');
82 push @patt, '.*' unless @patt;
84 push @days, "0" unless @days;
85 for my $entry (@days) {
86 my $now = $today->sub($entry);
87 my $fh = $fp->open($now);
92 begin() if main->can('begin');
95 if (main->can('handle')) {
103 end() if main->can('end');
106 total() if main->can('total');
114 shift @prev while @prev > $nolines;
116 foreach my $p (@patt) {
118 my $r = substr $p, 1;
119 last if $line =~ m{$r}i;
121 last unless $line =~ m{$p}i;
125 if ($flag == @patt) {
127 s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg;
128 my ($t, $l) = split /\^/, $_, 2;
129 print atime($t), ' ', $l, "\n";
131 print "------------------\n" if $nolines > 1;
140 usage: grepdbg [nn days before] [-nnn lines before] [<perl filter module>] [<regexp>|!<regexp>]...
142 You can have more than one <regexp> with an implicit 'and' between them. All
143 <regexes> are caseless. It's recommended to put 'not' (!<regex>) first in any list.
144 Don't forget that you are doing this in a shell and you may need to quote your
147 grepdbg with no arguments will simply list the current debug log with the timestamp
148 for each line decoded into a human readable form.
152 is a handy way of scrolling through the debug log.
156 will display any line containing 'progress' and also the two lines before that.
158 You can install your own content and display arrangement (useful for filtering data
159 in some complicated way). You call it like this (assuming it is called 'filter.pm').
160 This is what is meant by <perl filter module>.
164 All the other arguments to grepdbg are available to limit the input to your filter.
167 The filter module MUST contain at least:
177 It can also have a 'sub begin {...}' and / or 'sub end {...}' which are executed
178 immediately after opening a logfile and then just before closing it, respectively.
180 You can also add a 'sub total {...}' which executes after the last line is
181 printed and grepdbg exits.
183 Read the code of this program and copy'n'paste the 'sub
184 process' code into a new file. Then change 'sub process'
185 to 'sub handle'. Add the line 'package main;' at the beginning
186 of the file and a line '1;' at the end and then modify it to