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