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