change build number calculation to be more accurate
[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 use vars qw($VERSION $BRANCH);
21 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
22 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
23 $main::build += $VERSION;
24 $main::branch += $BRANCH;
25
26 #
27 # print some items from the log backwards in time
28 #
29 # This command outputs a list of n lines starting from time t with $pattern tags
30 #
31 sub print
32 {
33         my $fcb = $DXLog::log;
34         my $from = shift || 0;
35         my $to = shift || 20;
36         my $count;
37         my $jdate = $fcb->unixtoj(shift);
38         my $pattern = shift;
39         my $who = uc shift;
40         my $search;
41         my @in;
42         my @out = ();
43         my $eval;
44         my $tot = $from + $to;
45         my $hint = "";
46             
47         if ($pattern) {
48                 $hint = "m{\\Q$pattern\\E}i";
49         }
50         if ($who) {
51                 if ($hint) {
52                         $hint .= ' && ';
53                 }
54                 $hint .= 'm{\\Q$who\\E}i';
55         }
56         $hint = "next unless $hint" if $hint;
57         
58         $eval = qq(
59                            \@in = ();
60                            while (<\$fh>) {
61                                    $hint;
62                                    chomp;
63                                    push \@in, \$_;
64                                    shift \@in, if \@in > $tot;
65                            }
66                    );
67         
68         $fcb->close;                                      # close any open files
69
70         my $fh = $fcb->open($jdate); 
71         L1: for ($count = 0; $count < $to; ) {
72                 my $ref;
73                 if ($fh) {
74                         eval $eval;               # do the search on this file
75                         return ("Log search error", $@) if $@;
76                         my @tmp;
77                         while (@in) {
78                                 last L1 if $count >= $to;
79                                 my $ref = [ split /\^/, shift @in ];
80                                 next if defined $pattern && $ref->[1] ne $pattern;
81                                 push @tmp, print_item($ref);
82                                 $count++;
83                         }
84                         @out = (@tmp, @out);
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;