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