change watchdbg and grepdbg to show preceeding lines
[spider.git] / perl / watchdbg
index ca3e2e3b03c134abcbd7ef6b46d0aa63f65864a3..4ac24be1b02dd34c69b1d0bb9b629640a7edf400 100755 (executable)
@@ -31,7 +31,9 @@ use strict;
 my $fp = DXLog::new('debug', 'dat', 'd');
 my @today = Julian::unixtoj(time()); 
 my $fh = $fp->open(@today) or die $!; 
+my $nolines = shift if $ARGV[0] =~ /^\d+$/ || 1;
 my $exp = join '|', @ARGV;
+my @prev;
 
 # seek to end of file
 $fh->seek(0, 2);
@@ -39,19 +41,22 @@ for (;;) {
        my $line = <$fh>;
        if ($line) {
                if ($exp) {
-                       printit($line) if $line =~ m{(?:$exp)}oi;
+                       push @prev, $line;
+                       shift @prev while @prev > $nolines; 
+                       printit(@prev) if $line =~ m{(?:$exp)}oi;
                } else {
-                   printit($line);
-               }
+                       printit($line);
+               }
        } else {
                sleep(1);
-
+               
                # check that the debug hasn't rolled over to next day
                # open it if it has
                my @now = Julian::unixtoj(time()); 
                if ($today[1] != $now[1]) {
                        $fp->close;
-                       for (my $i = 0; $i < 20; $i++) {
+                       my $i;
+                       for ($i = 0; $i < 20; $i++) {
                                last if $fh = $fp->open(@now);
                                sleep 5;
                        }
@@ -63,12 +68,14 @@ for (;;) {
 
 sub printit
 {
-       my $line = shift;
-       my @line =  split '\^', $line;
-       my $t = shift @line;
-        my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
-        my $buf = sprintf "%02d:%02d:%02d", $hour, $min, $sec;
-
-       print $buf, ' ', join('^', @line); 
+       while (@_) {
+               my $line = shift;
+               my @line =  split '\^', $line;
+               my $t = shift @line;
+               my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
+               my $buf = sprintf "%02d:%02d:%02d", $hour, $min, $sec;
+               
+               print $buf, ' ', join('^', @line); 
+       }
 }
 exit(0);