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