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