2 # the general purpose logging machine
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.
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:-
11 # spots/1998/<julian day no>[.<optional suffix>]
14 # log/1998/<week no>[.<optional suffix>]
17 # wwv/1998/<month>[.<optional suffix>]
19 # Routines are provided to read these files in and to append to them
21 # Copyright (c) - 1998 Dirk Koopman G1TLH
30 @EXPORT = qw(Log Logclose);
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;
44 $main::build += $VERSION;
45 $main::branch += $BRANCH;
49 $log = new('log', 'dat', 'm');
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
56 my ($prefix, $suffix, $sort) = @_;
58 $ref->{prefix} = "$main::data/$prefix";
59 $ref->{suffix} = $suffix if $suffix;
62 # make sure the directory exists
63 mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
69 my ($self, $jdate) = @_;
70 my $year = $jdate->year;
71 my $thing = $jdate->thing;
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};
79 # open the appropriate data file
82 my ($self, $jdate, $mode) = @_;
84 # if we are writing, check that the directory exists
86 my $year = $jdate->year;
87 my $dir = "$self->{prefix}/$year";
88 mkdir($dir, 0777) if ! -e $dir;
91 $self->{fn} = $self->_genfn($jdate);
93 $mode = 'r' if !$mode;
94 $self->{mode} = $mode;
96 my $fh = new IO::File $self->{fn}, $mode, 0666;
98 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
101 $self->{jdate} = $jdate;
103 # DXDebug::dbg("opening $self->{fn}\n") if isdbg("dxlog");
110 my ($self, $jdate) = @_;
111 my $fn = $self->_genfn($jdate);
117 my ($self, $jdate) = @_;
119 my $fn = $self->_genfn($jdate);
120 return (stat $fn)[9];
123 # open the previous log file in sequence
127 my $jdate = $self->{jdate}->sub(1);
128 return $self->open($jdate, @_);
131 # open the next log file in sequence
135 my $jdate = $self->{jdate}->add(1);
136 return $self->open($jdate, @_);
139 # convert a date into the correct format from a unix date depending on its sort
144 if ($self->{'sort'} eq 'm') {
145 return Julian::Month->new(shift);
146 } elsif ($self->{'sort'} eq 'd') {
147 return Julian::Day->new(shift);
149 confess "shouldn't get here";
152 # write (actually append) to a file, opening new files as required
155 my ($self, $jdate, $line) = @_;
157 $self->{mode} ne ">>" ||
158 $jdate->year != $self->{jdate}->year ||
159 $jdate->thing != $self->{jdate}->year) {
160 $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
163 return $self->{fh}->print("$line\n");
166 # write (actually append) using the current date to a file, opening new files as required
169 my ($self, $line) = @_;
171 my $date = $self->unixtoj($t);
172 return $self->write($date, $line);
175 # write (actually append) using a unix time to a file, opening new files as required
178 my ($self, $t, $line) = @_;
179 my $date = $self->unixtoj($t);
180 return $self->write($date, $line);
183 # close the log file handle
187 undef $self->{fh}; # close the filehandle
194 undef $self->{fh}; # close the filehandle
195 delete $self->{fh} if $self->{fh};
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!
205 $log->writeunix($t, join('^', $t, @_) );