downgrade perl on console.pl
[spider.git] / perl / grepdbg
1 #!/usr/bin/perl
2 #
3 # Program to do a grep with dates and times on the debug
4 # files
5 #
6 # grepdbg [nn] [-mm] <regular expression>
7 #
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
10 #
11 # -mmm - print the mmm lines before the match. So -10 will print
12 # ten lines including the line matching the regular expression. 
13 #
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. 
18 #
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.
23 #
24 #
25
26 require 5.004;
27
28 # search local then perl directories
29 BEGIN {
30         # root of directory tree for this system
31         $root = "/spider"; 
32         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
33         
34         unshift @INC, "$root/perl";     # this IS the right way round!
35         unshift @INC, "$root/local";
36 }
37
38 use SysVar;
39 use DXUtil;
40 use DXLog;
41 use Julian;
42
43 use strict;
44
45 use vars qw(@list $fp $today $string);
46
47
48 $fp = DXLog::new('debug', 'dat', 'd');
49 $today = $fp->unixtoj(time()); 
50 my $nolines = 1;
51 my @prev;
52 my @patt;
53
54 foreach my $arg (@ARGV) {
55         if ($arg =~ /^-/) {
56                 $arg =~ s/^-//o;
57                 if ($arg =~ /^\s*\-+(?:[h\?]e?l?p?)/) {
58                         usage();
59                         exit(0);
60                 }
61                 push @list, $arg;
62         } elsif ($arg =~ /^\d+$/) {
63                 $nolines = $arg;
64         } elsif ($arg =~ /\.pm$/) {
65                 if (-e $arg) {
66                         my $fn = $arg;
67                         $fn =~ s/\.pm$//;
68                         eval { require $arg};
69                         die "requiring $fn failed $@" if $@;
70                 } else {
71                         die "$arg not found";
72                 }
73         } else {
74                 push @patt, $arg;
75         }
76 }
77
78 push @patt, '.*' unless @patt;
79
80 push @list, "0" unless @list;
81 for my $entry (@list) {
82         my $now = $today->sub($entry); 
83         my $fh = $fp->open($now); 
84         my $line;
85         my $do;
86
87         if (main->can('handle')) {
88                 $do = \&handle;
89         } else {
90                 $do = \&process;
91         }
92
93         begin() if main->can('begin');
94         if ($fh) {
95                 while (<$fh>) {
96                         &$do($_);
97                 }
98                 $fp->close();
99         }
100         end() if main->can('end');
101 }
102
103 sub process
104 {
105         my $line = shift;
106         chomp $line;
107         push @prev, $line;
108         shift @prev while @prev > $nolines;
109         my $flag = 0;
110         foreach my $p (@patt) {
111                 if ($p =~ /^!/) {
112                         my $r = substr $p, 1;
113                         last if $line =~ m{$r}i;
114                 } else {
115                         last unless $line =~ m{$p}i;
116                 }
117                 ++$flag;
118         }               
119         if ($flag == @patt) {
120                 for (@prev) {
121                         s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
122                         my ($t, $l) =  split /\^/, $_, 2;
123                         print atime($t), ' ', $l, "\n";
124                         print '----------------' if $nolines > 1;
125                 }
126                 @prev = ();
127         }
128 }
129         
130 sub usage
131 {
132         die "usage: grepdbg [nn days before] [-nnn lines before] [<perl file name>] [<regexp>|!<regexp>]...\n";
133 }
134 exit(0);