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