fixed typo in new file @ modnight code
[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 gb7baa gb7djk   # watch the conversation between BAA and DJK 
10 #
11
12 require 5.004;
13
14 # search local then perl directories
15 BEGIN {
16         # root of directory tree for this system
17         $root = "/spider"; 
18         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
19         
20         unshift @INC, "$root/perl";     # this IS the right way round!
21         unshift @INC, "$root/local";
22 }
23
24 use IO::File;
25 use DXVars;
26 use DXUtil;
27 use DXLog;
28
29 use strict;
30
31 my $fp = DXLog::new('debug', 'dat', 'd');
32 my @today = Julian::unixtoj(time()); 
33 my $fh = $fp->open(@today) or die $!; 
34 my $exp = join '|', @ARGV;
35
36 # seek to end of file
37 $fh->seek(0, 2);
38 for (;;) {
39         my $line = <$fh>;
40         if ($line) {
41                 if ($exp) {
42                         printit($line) if $line =~ m{(?:$exp)}oi;
43                 } else {
44                     printit($line);
45                 }
46         } else {
47                 sleep(1);
48
49                 # check that the debug hasn't rolled over to next day
50                 # open it if it has
51                 my @now = Julian::unixtoj(time()); 
52                 if ($today[1] != $now[1]) {
53                         $fp->close;
54                         my $i;
55                         for ($i = 0; $i < 20; $i++) {
56                                 last if $fh = $fp->open(@now);
57                                 sleep 5;
58                         }
59                         die $! if $i >= 20; 
60                         @today = @now;
61                 }
62         }
63 }
64
65 sub printit
66 {
67         my $line = shift;
68         my @line =  split '\^', $line;
69         my $t = shift @line;
70         my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
71         my $buf = sprintf "%02d:%02d:%02d", $hour, $min, $sec;
72
73         print $buf, ' ', join('^', @line); 
74 }
75 exit(0);