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