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