added Log Parsing
[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 $search;
34         my @in;
35         my @out;
36         my $eval;
37         my $count;
38             
39         $search = $pattern ? "\$ref->[1] =~ /$pattern/" : '1' ;
40         $eval = qq(
41                            my \$c;
42                            my \$ref;
43                            for (\$c = \$#in; \$c >= 0; \$c--) {
44                                         \$ref = \$in[\$c];
45                                         if ($search) {
46                                                 \$count++;
47                                                 next if \$count < $from;
48                                                 push \@out, print_item(\$ref);
49                                                 last LOOP if \$count >= \$to;                  # stop after n
50                                         }
51                                 }
52                           );
53         
54         $self->close;                                      # close any open files
55
56         my $fh = $self->open(@date); 
57 LOOP:
58         while ($count < $to) {
59                 my @spots = ();
60                 if ($fh) {
61                         while (<$fh>) {
62                                 chomp;
63                                 push @in, [ split '\^' ];
64                         }
65                         eval $eval;               # do the search on this file
66                         return ("Spot search error", $@) if $@;
67                 }
68                 $fh = $self->openprev();      # get the next file
69                 last if !$fh;
70         }
71
72         return @out;
73 }
74
75 #
76 # the standard log printing interpreting routine.
77 #
78 # every line that is printed should call this routine to be actually visualised
79 #
80 # Don't really know whether this is the correct place to put this stuff, but where
81 # else is correct?
82 #
83 # I get a reference to an array of items
84 #
85 sub print_item
86 {
87         my $r = shift;
88         my @ref = @$r;
89         my $d = atime($ref[0]);
90         my $s = 'undef';
91         
92         if ($ref[1] eq 'rcmd') {
93                 if ($ref[2] eq 'in') {
94                         $s = "$ref[4] (priv: $ref[3]) rcmd: $ref[5]";
95                 } else {
96                         $s = "$ref[3] reply: $ref[4]";
97                 }
98         } elsif ($ref[1] eq 'talk') {
99                 $s = "$ref[3] -> $ref[2] ($ref[4]) $ref[5]";
100         } elsif ($ref[1] eq 'ann') {
101                 $s = "$ref[2] -> $ref[3] $ref[4]";
102         } else {
103                 $s = "$ref[2]";
104         }
105         return "$d $s";
106 }
107
108 1;