added Log Parsing
[spider.git] / perl / DXLog.pm
1 #
2 # the general purpose logging machine
3 #
4 # This module is designed to allow you to log stuff in specific places
5 # and will rotate logs on a monthly, weekly or daily basis. 
6 #
7 # The idea is that you give it a prefix which is a directory and then 
8 # the system will log stuff to a directory structure which looks like:-
9 #
10 # daily:-
11 #   spots/1998/<julian day no>[.<optional suffix>]
12 #
13 # weekly :-
14 #   log/1998/<week no>[.<optional suffix>]
15 #
16 # monthly
17 #   wwv/1998/<month>[.<optional suffix>]
18 #
19 # Routines are provided to read these files in and to append to them
20
21 # Copyright (c) - 1998 Dirk Koopman G1TLH
22 #
23 # $Id$
24 #
25
26 package DXLog;
27
28 require Exporter;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(Log);
31
32 use FileHandle;
33 use DXVars;
34 use DXDebug ();
35 use DXUtil;
36 use Julian;
37 use Carp;
38
39 use strict;
40 use vars qw($log);
41
42 $log = new('log', 'dat', 'm');
43
44 # create a log object that contains all the useful info needed
45 # prefix is the main directory off of the data directory
46 # sort is 'm' for monthly, 'd' for daily 
47 sub new
48 {
49         my ($prefix, $suffix, $sort) = @_;
50         my $ref = {};
51         $ref->{prefix} = "$main::data/$prefix";
52         $ref->{suffix} = $suffix if $suffix;
53         $ref->{sort} = $sort;
54                 
55         # make sure the directory exists
56         mkdir($ref->{prefix}, 0777) if ! -e $ref->{prefix};
57         return bless $ref;
58 }
59
60 # open the appropriate data file
61 sub open
62 {
63         my ($self, $year, $thing, $mode) = @_;
64         
65         # if we are writing, check that the directory exists
66         if (defined $mode) {
67                 my $dir = "$self->{prefix}/$year";
68                 mkdir($dir, 0777) if ! -e $dir;
69                 $self->{mode} = $mode;
70         } else {
71                 delete $self->{mode};
72         }
73         
74         $self->{fn} = sprintf "$self->{prefix}/$year/%02d", $thing if $self->{sort} eq 'm';
75         $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing if $self->{sort} eq 'd';
76         $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
77         
78         $mode = 'r' if !$mode;
79         my $fh = new FileHandle $self->{fn}, $mode;
80         return undef if !$fh;
81         $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
82         $self->{fh} = $fh;
83
84         $self->{year} = $year;
85         $self->{thing} = $thing;
86         
87         DXDebug::dbg("dxlog", "opening $self->{fn}\n");
88         
89         return $self->{fh};
90 }
91
92 # open the previous log file in sequence
93 sub openprev
94 {
95         my $self = shift;
96         if ($self->{sort} eq 'm') {
97                 ($self->{year}, $self->{thing}) = Julian::subm($self->{year}, $self->{thing}, 1);
98         } elsif ($self->{sort} eq 'd') {
99                 ($self->{year}, $self->{thing}) = Julian::sub($self->{year}, $self->{thing}, 1);
100         }
101         return $self->open($self->{year}, $self->{thing}, @_);
102 }
103
104 # open the next log file in sequence
105 sub opennext
106 {
107         my $self = shift;
108         if ($self->{sort} eq 'm') {
109                 ($self->{year}, $self->{thing}) = Julian::addm($self->{year}, $self->{thing}, 1);
110         } elsif ($self->{sort} eq 'd') {
111                 ($self->{year}, $self->{thing}) = Julian::add($self->{year}, $self->{thing}, 1);
112         }
113         return $self->open($self->{year}, $self->{thing}, @_);
114 }
115
116 # convert a date into the correct format from a unix date depending on its sort
117 sub unixtoj
118 {
119         my $self = shift;
120         
121         if ($self->{sort} eq 'm') {
122                 return Julian::unixtojm(shift);
123         } elsif ($self->{sort} eq 'd') {
124                 return Julian::unixtoj(shift);
125         }
126         confess "shouldn't get here";
127 }
128
129 # write (actually append) to a file, opening new files as required
130 sub write
131 {
132         my ($self, $year, $thing, $line) = @_;
133         if (!$self->{fh} || 
134                 $self->{mode} ne ">>" || 
135                 $year != $self->{year} || 
136                 $thing != $self->{thing}) {
137                 $self->open($year, $thing, ">>") or confess "can't open $self->{fn} $!";
138         }
139
140         return $self->{fh}->print("$line\n");
141 }
142
143 # write (actually append) using the current date to a file, opening new files as required
144 sub writenow
145 {
146         my ($self, $line) = @_;
147         my $t = time;
148         my @date = $self->unixtoj($t);
149         return $self->write(@date, $line);
150 }
151
152 # write (actually append) using a unix time to a file, opening new files as required
153 sub writeunix
154 {
155         my ($self, $t, $line) = @_;
156         my @date = $self->unixtoj($t);
157         return $self->write(@date, $line);
158 }
159
160 # close the log file handle
161 sub close
162 {
163         my $self = shift;
164         undef $self->{fh};                      # close the filehandle
165         delete $self->{fh};
166         delete $self->{mode};
167 }
168
169 # log something in the system log 
170 # this routine is exported to any module that declares DXLog
171 # it takes all its args and joins them together with the unixtime writes them out as one line
172 # The user is responsible for making sense of this!
173 sub Log
174 {
175         my $t = time;
176         $log->writeunix($t, join('^', $t, @_) );
177 }
178
179 sub DESTROY                                             # catch undefs and do what is required further down the tree
180 {
181         my $self = shift;
182         DXDebug::dbg("dxlog", "closing $self->{fn}\n");
183         undef $self->{fh} if defined $self->{fh};
184
185 1;