92deee427026cf1e574b3a66c5d479a88131304f
[spider.git] / perl / DXLogPrint.pm
1 #
2 # Log Printing routines
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 #
7 #
8
9 package DXLog;
10
11 use 5.10.1;
12
13 use IO::File;
14 use DXVars;
15 use DXDebug qw(dbg isdbg);
16 use DXUtil;
17 use DXLog;
18 use Julian;
19
20
21 our $readback = 1;
22 if ($readback) {
23         $readback = `which tac`;
24
25 chomp $readback;
26 #undef $readback;                               # yet another reason not to use the cloud!
27  
28
29 use strict;
30
31 use vars qw($maxmonths);
32 $maxmonths = 36;
33
34 #
35 # print some items from the log backwards in time
36 #
37 # This command outputs a list of n lines starting from time t with $pattern tags
38 #
39 sub search
40 {
41         my $fcb = $DXLog::log;
42         my $from = shift // 0;
43         my $to = shift // 10;
44         my $jdate = $fcb->unixtoj(shift);
45         my $pattern = shift;
46         my $who = shift;
47         my $search;
48         my @in;
49         my @out = ();
50         my $eval;
51         my $tot = $from + $to;
52         my $hint = "";
53             
54         $who = uc $who if defined $who;
55
56         dbg("from: $from to: $to pattern: $pattern hint: $hint") if isdbg('search');
57         
58         if ($pattern) {
59                 $hint = qq{m{\Q$pattern\E}i};
60         } else {
61                 $hint = q{!m{\^(?:ann|rcmd|talk|chat)\^}};
62         }
63         if ($who) {
64                 $hint .= ' && ' if $hint;
65                 $hint .= q{m{\Q$who\E}i};
66         } 
67         $hint = "next unless $hint" if $hint;
68         $hint .= "; next unless m{^\\d+\\^$pattern\\^}i" if $pattern;
69         $hint ||= "";
70         
71         $eval = qq(while (<\$fh>) {
72                                    $hint;
73                                    chomp;
74                    # say "line: \$_";
75                                    push \@in, \$_;
76                    last L1 if \@in >= $tot;
77                            } );
78         
79         if (isdbg('search')) {
80                 dbg("sh/log hint: $hint");
81                 dbg("sh/log eval: $eval");
82         }
83         
84         $fcb->close;                                      # close any open files
85
86         my $months;
87         my $fh;
88         if ($readback) {
89                 my $fn = $fcb->fn($jdate);
90                 $fh = IO::File->new("$readback $fn |");
91         } else {
92                 $fh = $fcb->open($jdate);       
93         }
94  L1: for ($months = 0; $fh && $months < $maxmonths && @in < $tot; $months++) {
95                 my $ref;
96
97                 if ($fh) {
98                         my @tmp;
99                         eval $eval;               # do the search on this file
100                         return ("Log search error", $@) if $@;
101                 }
102
103                 if ($readback) {
104                         my $fn = $fcb->fn($jdate->sub(1));
105                         $fh = IO::File->new("$readback $fn |");
106                 } else {
107                         $fh = $fcb->openprev();      # get the next file
108                 }
109         }
110
111         unless (@in) {
112                 my $name = $pattern ? $pattern : "log";
113                 my $s = "$who "|| '';
114                 return "show/$name: ${s}not found";
115         } 
116
117         for (sort {$a cmp $b } @in) {
118                 push @out, [ split /\^/ ]
119         }
120
121         return @out;
122 }
123
124 sub print
125 {
126         my @out;
127
128         my @in = search(@_);
129         for (@in) {
130                 push @out, print_item($_);
131         }
132         return @out;
133 }
134
135
136 #
137 # the standard log printing interpreting routine.
138 #
139 # every line that is printed should call this routine to be actually visualised
140 #
141 # Don't really know whether this is the correct place to put this stuff, but where
142 # else is correct?
143 #
144 # I get a reference to an array of items
145 #
146 sub print_item
147 {
148         my $r = shift;
149         my $d = atime($r->[0]);
150         my $s = 'undef';
151         
152         if ($r->[1] eq 'rcmd') {
153                 $r->[6] ||= 'Unknown';
154                 if ($r->[2] eq 'in') {
155                         $r->[5] ||= "";
156                         $s = "in: $r->[4] ($r->[6] priv: $r->[3]) rcmd: $r->[5]";
157                 } else {
158                         $r->[4] ||= "";
159                         $s = "$r->[3] $r->[6] reply: $r->[4]";
160                 }
161         } elsif ($r->[1] eq 'talk') {
162                 $r->[5] ||= "";
163                 $s = "$r->[3] -> $r->[2] ($r->[4]) $r->[5]";
164         } elsif ($r->[1] eq 'ann' || $r->[1] eq 'chat') {
165                 $r->[4] ||= "";
166                 $r->[4] =~ s/^\#\d+ //;
167                 $s = "$r->[3] -> $r->[2] $r->[4]";
168         } else {
169                 $r->[2] ||= "";
170                 $s = "$r->[2]";
171         }
172         return "$d $s";
173 }
174
175 1;