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 -10 will print
12 # ten lines including the line matching the regular expression.
14 # <regexp> is the regular expression you are searching for,
15 # a caseless search is done. There can be more than one <regexp>
16 # a <regexp> preceeded by a '!' is treated as NOT <regexp>. Each
17 # <regexp> is implcitly ANDed together.
19 # If you specify something that likes a filename and that filename
20 # has a .pm on the end of it and it exists then rather than doing
21 # the regex match it executes the "main::handle()" function passing
22 # it one line at a time.
30 # search local then perl directories
32 # root of directory tree for this system
34 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
36 unshift @INC, "$root/perl"; # this IS the right way round!
37 unshift @INC, "$root/local";
47 use vars qw(@days $fp $today $string);
50 $fp = DXLog::new('debug', 'dat', 'd');
51 $today = $fp->unixtoj(time());
56 foreach my $arg (@ARGV) {
59 if ($arg =~ /\?|^he?l?p?/) {
63 $nolines = $arg if $arg =~ /^\d+$/;
64 } elsif ($arg =~ /^\d+$/) {
66 } elsif ($arg =~ /\.pm$/) {
71 die "requiring $fn failed $@" if $@;
72 die "required $fn does not contain 'sub handle' (check that 'package main;' exists)" unless main->can('handle');
81 push @patt, '.*' unless @patt;
83 push @days, "0" unless @days;
84 for my $entry (@days) {
85 my $now = $today->sub($entry);
86 my $fh = $fp->open($now);
91 begin() if main->can('begin');
94 if (main->can('handle')) {
102 end() if main->can('end');
105 total() if main->can('total');
113 shift @prev while @prev > $nolines;
115 foreach my $p (@patt) {
117 my $r = substr $p, 1;
118 last if $line =~ m{$r}i;
120 last unless $line =~ m{$p}i;
124 if ($flag == @patt) {
126 s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg;
127 my ($t, $l) = split /\^/, $_, 2;
128 print atime($t), ' ', $l, "\n";
130 print "------------------\n" if $nolines > 1;
139 usage: grepdbg [nn days before] [-nnn lines before] [<perl file name>] [<regexp>|!<regexp>]...
141 You can have more than one <regexp> with an implicit 'and' between them. All
142 <regexes> are caseless. It's recommended to put 'not' (!<regex>) first in any list.
143 Don't forget that you are doing this in a shell and you may need to quote your
146 grepdbg with no arguments will simply list the current debug log with the timestamp
147 for each line decoded into a human readable form.
151 is a handy way of scrolling through the debug log.
153 You can install your own content and display arrangement (useful for filtering data
154 in some complicated way). You call it like this (assuming it is called 'filter.pm').
158 All the other arguments to grepdbg are available to limit the input to your filter.
161 The filter module MUST contain at least:
171 It can also have a 'sub begin {...}' and / or 'sub end {...}' which are executed
172 immediately after opening a logfile and then just before closing it, respectively.
174 You can also add a 'sub total {...}' which executes after the last line is
175 printed and grepdbg exits.
177 Read the code of this program and copy'n'paste the 'sub process' code and change
178 its name to 'sub handle'. Modify it to your requirements...