change watchdbg and grepdbg to show preceeding lines
[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 # dispdbg [-nnn ...] <string>
7 #
8 # the -nnn 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 # <string> is the string, a caseless search is done
11 #
12 #
13
14 require 5.004;
15
16 # search local then perl directories
17 BEGIN {
18         # root of directory tree for this system
19         $root = "/spider"; 
20         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
21         
22         unshift @INC, "$root/perl";     # this IS the right way round!
23         unshift @INC, "$root/local";
24 }
25
26 use DXVars;
27 use DXUtil;
28 use DXLog;
29
30 use strict;
31
32 use vars qw(@list $fp @today $string);
33
34 $fp = DXLog::new('debug', 'dat', 'd');
35 @today = Julian::unixtoj(time()); 
36 my $nolines = 1;
37 my @prev;
38
39 for my $arg (@ARGV) {
40         if ($arg =~ /^-/) {
41                 $arg =~ s/^-//o;
42                 push @list, $arg;
43         } elsif ($arg =~ /^\d+$/) {
44                 $nolines = $arg;
45         } else {
46                 $string = $arg;
47                 last;
48         }
49 }
50 die "usage: grepdbg [nn] [[-nnn] ..] <regexp>\n" unless  $string;
51
52 push @list, "0" unless @list;
53 for my $entry (@list) {
54         my @now = Julian::sub(@today, $entry); 
55         my $fh = $fp->open(@now); 
56         my $line;
57         if ($fh) {
58                 while (<$fh>) {
59                         my $line = $_;
60                         chomp $line;
61                         push @prev, $line;
62                         shift @prev while @prev > $nolines;
63                         if ($line =~ m{$string}io) {
64                                 for (@prev) {
65                                         my @line =  split '\^';
66                                         my $t = shift @line;
67                                         print atime($t), ' ', join('^', @line), "\n"; 
68                                 }
69                         }
70                 }
71                 $fp->close();
72         }
73 }
74 exit(0);