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