add DXSql module and condiational processing
[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
9 # nn - is the day you what to look at: 1 is yesterday, 0 is today
10 # and is optional if there is only one argument
11 #
12 # -mmm - print the mmm lines before the match. So -10 will print
13 # ten lines including the line matching the regular expression. 
14 #
15 # <regexp> is the regular expression you are searching for, 
16 # a caseless search is done
17 #
18 #
19
20 require 5.004;
21
22 # search local then perl directories
23 BEGIN {
24         # root of directory tree for this system
25         $root = "/spider"; 
26         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
27         
28         unshift @INC, "$root/perl";     # this IS the right way round!
29         unshift @INC, "$root/local";
30         sub main::mkver {}
31 }
32
33 use DXVars;
34 use DXUtil;
35 use DXLog;
36 use Julian;
37
38 use strict;
39
40 use vars qw(@list $fp $today $string);
41
42 $fp = DXLog::new('debug', 'dat', 'd');
43 $today = $fp->unixtoj(time()); 
44 my $nolines = 1;
45 my @prev;
46
47 for my $arg (@ARGV) {
48         if ($arg =~ /^-/) {
49                 $arg =~ s/^-//o;
50                 push @list, $arg;
51         } elsif ($arg =~ /^\d+$/) {
52                 $nolines = $arg;
53         } else {
54                 $string = $arg;
55                 last;
56         }
57 }
58 die "usage: grepdbg [nn] [[-nnn] ..] <regexp>\n" unless  $string;
59
60 push @list, "0" unless @list;
61 for my $entry (@list) {
62         my $now = $today->sub($entry); 
63         my $fh = $fp->open($now); 
64         my $line;
65         if ($fh) {
66                 while (<$fh>) {
67                         my $line = $_;
68                         chomp $line;
69                         push @prev, $line;
70                         shift @prev while @prev > $nolines;
71                         if ($line =~ m{$string}io) {
72                                 for (@prev) {
73                                         s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
74                                         my ($t, $l) =  split /\^/, $_, 2;
75                                         print atime($t), ' ', $l, "\n"; 
76                                 }
77                                 @prev = ();
78                         }
79                 }
80                 $fp->close();
81         }
82 }
83 exit(0);