1282d71fb680861baa7d306bb21c9d7a9161d976
[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 package main;
22
23 use vars qw($data);
24
25 # search local then perl directories
26 BEGIN {
27         # root of directory tree for this system
28         $root = "/spider"; 
29         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
30         
31         unshift @INC, "$root/perl";     # this IS the right way round!
32         unshift @INC, "$root/local";
33 }
34
35 $data = "$root/data";
36
37 use DXVars;
38 use DXUtil;
39 use DXLog;
40 use Julian;
41
42 use strict;
43
44 use vars qw(@list $fp $today $string);
45
46 $fp = DXLog::new('debug', 'dat', 'd');
47 $today = $fp->unixtoj(time()); 
48 my $nolines = 1;
49 my @prev;
50
51 for my $arg (@ARGV) {
52         if ($arg =~ /^-/) {
53                 $arg =~ s/^-//o;
54                 push @list, $arg;
55         } elsif ($arg =~ /^\d+$/) {
56                 $nolines = $arg;
57         } else {
58                 $string = $arg;
59                 last;
60         }
61 }
62 die "usage: grepdbg [nn] [[-nnn] ..] <regexp>\n" unless  $string;
63
64 push @list, "0" unless @list;
65 for my $entry (@list) {
66         my $now = $today->sub($entry); 
67         my $fh = $fp->open($now); 
68         my $line;
69         if ($fh) {
70                 while (<$fh>) {
71                         my $line = $_;
72                         chomp $line;
73                         push @prev, $line;
74                         shift @prev while @prev > $nolines;
75                         if ($line =~ m{$string}io) {
76                                 for (@prev) {
77                                         s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
78                                         my ($t, $l) =  split /\^/, $_, 2;
79                                         print atime($t), ' ', $l, "\n"; 
80                                 }
81                                 @prev = ();
82                         }
83                 }
84                 $fp->close();
85         }
86 }
87 exit(0);