Make sh/log only show stuff that sh/ann, sh/talk, sh/rcmd doesn't.
[spider.git] / perl / DXLogPrint.pm
1 #
2 # Log Printing routines
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package DXLog;
10
11 use IO::File;
12 use DXVars;
13 #use DXDebug ();
14 use DXUtil;
15 use DXLog;
16 use Julian;
17
18 use strict;
19
20 use vars qw($VERSION $BRANCH);
21 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
22 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
23 $main::build += $VERSION;
24 $main::branch += $BRANCH;
25
26 #
27 # print some items from the log backwards in time
28 #
29 # This command outputs a list of n lines starting from time t with $pattern tags
30 #
31 sub print
32 {
33         my $fcb = $DXLog::log;
34         my $from = shift || 0;
35         my $to = shift || 20;
36         my $count;
37         my $jdate = $fcb->unixtoj(shift);
38         my $pattern = shift;
39         my $who = uc shift;
40         my $search;
41         my @in;
42         my @out = ();
43         my $eval;
44         my $tot = $from + $to;
45         my $hint = "";
46             
47         if ($pattern) {
48                 $hint = "m{\\Q$pattern\\E}i";
49         } else {
50                 $hint = "!m{ann|rcmd|talk}";
51         }
52         if ($who) {
53                 if ($hint) {
54                         $hint .= ' && ';
55                 }
56                 $hint .= 'm{\\Q$who\\E}i';
57         } 
58         $hint = "next unless $hint" if $hint;
59         
60         $eval = qq(
61                            \@in = ();
62                            while (<\$fh>) {
63                                    $hint;
64                                    chomp;
65                                    push \@in, \$_;
66                                    shift \@in, if \@in > $tot;
67                            }
68                    );
69         
70         $fcb->close;                                      # close any open files
71
72         my $fh = $fcb->open($jdate); 
73         L1: for ($count = 0; $count < $to; ) {
74                 my $ref;
75                 if ($fh) {
76                         eval $eval;               # do the search on this file
77                         return ("Log search error", $@) if $@;
78                         my @tmp;
79                         while (@in) {
80                                 last L1 if $count >= $to;
81                                 my $ref = [ split /\^/, shift @in ];
82                                 next if defined $pattern && $ref->[1] ne $pattern;
83                                 push @tmp, print_item($ref);
84                                 $count++;
85                         }
86                         @out = (@tmp, @out);
87                 }
88                 $fh = $fcb->openprev();      # get the next file
89                 last if !$fh;
90         }
91         
92         return @out;
93 }
94
95 #
96 # the standard log printing interpreting routine.
97 #
98 # every line that is printed should call this routine to be actually visualised
99 #
100 # Don't really know whether this is the correct place to put this stuff, but where
101 # else is correct?
102 #
103 # I get a reference to an array of items
104 #
105 sub print_item
106 {
107         my $r = shift;
108         my @ref = @$r;
109         my $d = atime($ref[0]);
110         my $s = 'undef';
111         
112         if ($ref[1] eq 'rcmd') {
113                 if ($ref[2] eq 'in') {
114                         $s = "$ref[4] (priv: $ref[3]) rcmd: $ref[5]";
115                 } else {
116                         $s = "$ref[3] reply: $ref[4]";
117                 }
118         } elsif ($ref[1] eq 'talk') {
119                 $s = "$ref[3] -> $ref[2] ($ref[4]) $ref[5]";
120         } elsif ($ref[1] eq 'ann') {
121                 $s = "$ref[3] -> $ref[2] $ref[4]";
122         } else {
123                 $s = "$ref[2]";
124         }
125         return "$d $s";
126 }
127
128 1;