1. Sort out PC41 handling to include type 5 records for QRA locators and also
[spider.git] / perl / watchdbg
1 #!/usr/bin/perl
2 #
3 # watch the end of the current debug file (like tail -f) applying
4 # any regexes supplied on the command line.
5 #
6 # examples:-
7
8 #   watchdbg g1tlh       # watch everything g1tlh does
9 #   watchdbg 2 PCPROT       # watch all PCPROT messages + up to 2 lines before
10 #   watchdbg gb7baa gb7djk   # watch the conversation between BAA and DJK 
11 #
12
13 require 5.004;
14
15 # search local then perl directories
16 BEGIN {
17         # root of directory tree for this system
18         $root = "/spider"; 
19         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
20         
21         unshift @INC, "$root/perl";     # this IS the right way round!
22         unshift @INC, "$root/local";
23 }
24
25 use IO::File;
26 use DXVars;
27 use DXUtil;
28 use DXLog;
29
30 use strict;
31
32 my $fp = DXLog::new('debug', 'dat', 'd');
33 my @today = Julian::unixtoj(time()); 
34 my $fh = $fp->open(@today) or die $!; 
35 my $nolines = 1;
36 $nolines = shift if $ARGV[0] =~ /^-?\d+$/;
37 $nolines = abs $nolines if $nolines < 0;  
38 my $exp = join '|', @ARGV;
39 my @prev;
40
41 # seek to end of file
42 $fh->seek(0, 2);
43 for (;;) {
44         my $line = <$fh>;
45         if ($line) {
46                 if ($exp) {
47                         push @prev, $line;
48                         shift @prev while @prev > $nolines; 
49                         if ($line =~ m{(?:$exp)}oi) {
50                                 printit(@prev); 
51                                 @prev = ();
52                         }
53                 } else {
54                         printit($line);
55                 }
56         } else {
57                 sleep(1);
58                 
59                 # check that the debug hasn't rolled over to next day
60                 # open it if it has
61                 my @now = Julian::unixtoj(time()); 
62                 if ($today[1] != $now[1]) {
63                         $fp->close;
64                         my $i;
65                         for ($i = 0; $i < 20; $i++) {
66                                 last if $fh = $fp->open(@now);
67                                 sleep 5;
68                         }
69                         die $! if $i >= 20; 
70                         @today = @now;
71                 }
72         }
73 }
74
75 sub printit
76 {
77         while (@_) {
78                 my $line = shift;
79                 chomp $line;
80                 $line =~ s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
81                 my ($t, $l) =  split /\^/, $line, 2;
82                 my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
83                 my $buf = sprintf "%02d:%02d:%02d", $hour, $min, $sec;
84                 
85                 print $buf, ' ', $l, "\n"; 
86         }
87 }
88 exit(0);