ede817aa446fdae1dc89ae634488401206bc74d5
[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 #
24 #
25
26 package DXLog;
27
28 require Exporter;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(Log LogDbg Logclose);
31
32 use IO::File;
33 use DXVars;
34 use DXUtil;
35 use Julian;
36
37 use Carp;
38
39 use strict;
40
41 use vars qw($log %logs);
42
43 $log = new('log', 'dat', 'm');
44
45 # create a log object that contains all the useful info needed
46 # prefix is the main directory off of the data directory
47 # sort is 'm' for monthly, 'd' for daily 
48 sub new
49 {
50         my ($prefix, $suffix, $sort) = @_;
51         my $ref = bless {}, __PACKAGE__;
52         $ref->{prefix} = "$main::data/$prefix";
53         $ref->{suffix} = $suffix if $suffix;
54         $ref->{sort} = $sort;
55         
56         # make sure the directory exists
57         mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
58         $logs{$ref} = $ref;
59         
60         return $ref;
61 }
62
63 sub _genfn
64 {
65         my ($self, $jdate) = @_;
66         my $year = $jdate->year;
67         my $thing = $jdate->thing;
68         
69         my $fn = sprintf "$self->{prefix}/$year/%02d", $thing if $jdate->isa('Julian::Month');
70         $fn = sprintf "$self->{prefix}/$year/%03d", $thing if $jdate->isa('Julian::Day');
71         $fn .= ".$self->{suffix}" if $self->{suffix};
72         return $fn;
73 }
74
75 # open the appropriate data file
76 sub open
77 {
78         my ($self, $jdate, $mode) = @_;
79         
80         # if we are writing, check that the directory exists
81         if (defined $mode) {
82                 my $year = $jdate->year;
83                 my $dir = "$self->{prefix}/$year";
84                 mkdir($dir, 0777) if ! -e $dir;
85         }
86
87         $self->{fn} = $self->_genfn($jdate);
88         
89         $mode = 'r' if !$mode;
90         $self->{mode} = $mode;
91         $self->{jdate} = $jdate;
92         
93         my $fh = new IO::File $self->{fn}, $mode, 0666;
94         return undef if !$fh;
95         $fh->autoflush(0);                      # autofluahing off
96         $self->{fh} = $fh;
97
98 #       print "opening $self->{fn}\n";
99         
100         return $self->{fh};
101 }
102
103 sub delete($$)
104 {
105         my ($self, $jdate) = @_;
106         my $fn = $self->_genfn($jdate);
107         unlink $fn;
108 }
109
110 sub mtime($$)
111 {
112         my ($self, $jdate) = @_;
113         
114         my $fn = $self->_genfn($jdate);
115         return (stat $fn)[9];
116 }
117
118 # open the previous log file in sequence
119 sub openprev($$)
120 {
121         my $self = shift;
122         my $jdate = $self->{jdate}->sub(1);
123         return $self->open($jdate, @_);
124 }
125
126 # open the next log file in sequence
127 sub opennext($$)
128 {
129         my $self = shift;
130         my $jdate = $self->{jdate}->add(1);
131         return $self->open($jdate, @_);
132 }
133
134 # convert a date into the correct format from a unix date depending on its sort
135 sub unixtoj($$)
136 {
137         my $self = shift;
138         
139         if ($self->{'sort'} eq 'm') {
140                 return Julian::Month->new(shift);
141         } elsif ($self->{'sort'} eq 'd') {
142                 return Julian::Day->new(shift);
143         }
144         confess "shouldn't get here";
145 }
146
147 # write (actually append) to a file, opening new files as required
148 sub write($$$)
149 {
150         my ($self, $jdate, $line) = @_;
151         if (!$self->{fh} || 
152                 $self->{mode} ne ">>" || 
153                 $jdate->year != $self->{jdate}->year || 
154                 $jdate->thing != $self->{jdate}->thing) {
155                 $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
156         }
157
158         return $self->{fh}->print("$line\n");
159 }
160
161 # write (actually append) using the current date to a file, opening new files as required
162 sub writenow($$)
163 {
164         my ($self, $line) = @_;
165         my $t = time;
166         my $date = $self->unixtoj($t);
167         return $self->write($date, $line);
168 }
169
170 # write (actually append) using a unix time to a file, opening new files as required
171 sub writeunix($$$)
172 {
173         my ($self, $t, $line) = @_;
174         my $date = $self->unixtoj($t);
175         return $self->write($date, $line);
176 }
177
178 # close the log file handle
179 sub close
180 {
181         my $self = shift;
182         undef $self->{fh};                      # close the filehandle
183         delete $self->{fh};     
184 }
185
186 sub DESTROY
187 {
188         my $self = shift;
189         delete $logs{$self};
190         undef $self->{fh};                      # close the filehandle
191         delete $self->{fh} if $self->{fh};
192 }
193
194 sub flushall
195 {
196         foreach my $l (values %logs) {
197                 $l->{fh}->flush if exists $l->{fh};
198         }
199 }
200
201 # log something in the system log 
202 # this routine is exported to any module that declares DXLog
203 # it takes all its args and joins them together with the unixtime writes them out as one line
204 # The user is responsible for making sense of this!
205 sub Log
206 {
207         my $t = time;
208         $log->writeunix($t, join('^', $t, @_) );
209 }
210
211 sub LogDbg
212 {
213         DXDebug::dbg($_) for @_;
214         Log(@_);
215 }
216
217 sub Logclose
218 {
219         $log->close();
220 }
221 1;