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