a968e53b1f818d18c9cb467faa1e6b6a98757e57
[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 qw(dbg isdbg);
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use RingBuf;
18
19 use strict;
20
21 use vars qw($VERSION $BRANCH);
22 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
23 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
24 $main::build += $VERSION;
25 $main::branch += $BRANCH;
26
27 #
28 # print some items from the log backwards in time
29 #
30 # This command outputs a list of n lines starting from time t with $pattern tags
31 #
32 sub print
33 {
34         my $fcb = $DXLog::log;
35         my $from = shift || 0;
36         my $to = shift || 10;
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|chat)\\^}";
51         }
52         if ($who) {
53                 $hint .= ' && ' if $hint;
54                 $hint .= 'm{\\Q$who\\E}i';
55         } 
56         $hint = "next unless $hint" if $hint;
57         $hint .= ";next unless /^\\d+\\^$pattern\\^/" if $pattern;
58         $hint ||= "";
59         
60         $eval = qq(while (<\$fh>) {
61                                    $hint;
62                                    chomp;
63                                    \$ring->write(\$_);
64                            } );
65         
66         if (isdbg('search')) {
67                 dbg("sh/log hint: $hint");
68                 dbg("sh/log eval: $eval");
69         }
70         
71         $fcb->close;                                      # close any open files
72
73         my $fh = $fcb->open($jdate); 
74  L1: for (;@in < $to;) {
75                 my $ref;
76                 my $ring = RingBuf->new($tot);
77
78                 if ($fh) {
79                         my @tmp;
80                         eval $eval;               # do the search on this file
81                         return ("Log search error", $@) if $@;
82                         
83                         @in = ($ring->readall, @in);
84                         last L1 if @in > $tot;
85                 }
86
87                 $fh = $fcb->openprev();      # get the next file
88                 last if !$fh;
89         }
90         for (@in) {
91                 my @line = split /\^/ ;
92                 push @out, print_item(\@line);
93         
94         }
95         return @out;
96 }
97
98
99 #
100 # the standard log printing interpreting routine.
101 #
102 # every line that is printed should call this routine to be actually visualised
103 #
104 # Don't really know whether this is the correct place to put this stuff, but where
105 # else is correct?
106 #
107 # I get a reference to an array of items
108 #
109 sub print_item
110 {
111         my $r = shift;
112         my $d = atime($r->[0]);
113         my $s = 'undef';
114         
115         if ($r->[1] eq 'rcmd') {
116                 if ($r->[2] eq 'in') {
117                         $r->[5] ||= "";
118                         $s = "$r->[4] (priv: $r->[3]) rcmd: $r->[5]";
119                 } else {
120                         $r->[4] ||= "";
121                         $s = "$r->[3] reply: $r->[4]";
122                 }
123         } elsif ($r->[1] eq 'talk') {
124                 $r->[5] ||= "";
125                 $s = "$r->[3] -> $r->[2] ($r->[4]) $r->[5]";
126         } elsif ($r->[1] eq 'ann' || $r->[1] eq 'chat') {
127                 $r->[4] ||= "";
128                 $r->[4] =~ s/^\#\d+ //;
129                 $s = "$r->[3] -> $r->[2] $r->[4]";
130         } else {
131                 $r->[2] ||= "";
132                 $s = "$r->[2]";
133         }
134         return "$d $s";
135 }
136
137 1;