fix backcounting
[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 use Julian;
30
31 use strict;
32
33 use vars qw(@list $fp $today $string);
34
35 $fp = DXLog::new('debug', 'dat', 'd');
36 $today = $fp->unixtoj(time()); 
37 my $nolines = 1;
38 my @prev;
39
40 for my $arg (@ARGV) {
41         if ($arg =~ /^-/) {
42                 $arg =~ s/^-//o;
43                 push @list, $arg;
44         } elsif ($arg =~ /^\d+$/) {
45                 $nolines = $arg;
46         } else {
47                 $string = $arg;
48                 last;
49         }
50 }
51 die "usage: grepdbg [nn] [[-nnn] ..] <regexp>\n" unless  $string;
52
53 push @list, "0" unless @list;
54 for my $entry (@list) {
55         my $now = $today->sub($entry); 
56         my $fh = $fp->open($now); 
57         my $line;
58         if ($fh) {
59                 while (<$fh>) {
60                         my $line = $_;
61                         chomp $line;
62                         push @prev, $line;
63                         shift @prev while @prev > $nolines;
64                         if ($line =~ m{$string}io) {
65                                 for (@prev) {
66                                         s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
67                                         my ($t, $l) =  split /\^/, $_, 2;
68                                         print atime($t), ' ', $l, "\n"; 
69                                 }
70                                 @prev = ();
71                         }
72                 }
73                 $fp->close();
74         }
75 }
76 exit(0);