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 LogDbg Logclose);
41 use vars qw($VERSION $BRANCH);
42 ($VERSION, $BRANCH) = dxver(q$Revision$);
46 $log = new('log', 'dat', 'm');
48 # create a log object that contains all the useful info needed
49 # prefix is the main directory off of the data directory
50 # sort is 'm' for monthly, 'd' for daily
53 my ($prefix, $suffix, $sort) = @_;
55 $ref->{prefix} = "$main::data/$prefix";
56 $ref->{suffix} = $suffix if $suffix;
59 # make sure the directory exists
60 mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
66 my ($self, $jdate) = @_;
67 my $year = $jdate->year;
68 my $thing = $jdate->thing;
70 my $fn = sprintf "$self->{prefix}/$year/%02d", $thing if $jdate->isa('Julian::Month');
71 $fn = sprintf "$self->{prefix}/$year/%03d", $thing if $jdate->isa('Julian::Day');
72 $fn .= ".$self->{suffix}" if $self->{suffix};
76 # open the appropriate data file
79 my ($self, $jdate, $mode) = @_;
81 # if we are writing, check that the directory exists
83 my $year = $jdate->year;
84 my $dir = "$self->{prefix}/$year";
85 mkdir($dir, 0777) if ! -e $dir;
88 $self->{fn} = $self->_genfn($jdate);
90 $mode = 'r' if !$mode;
91 $self->{mode} = $mode;
92 $self->{jdate} = $jdate;
94 my $fh = new IO::File $self->{fn}, $mode, 0666;
96 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
99 # print "opening $self->{fn}\n";
106 my ($self, $jdate) = @_;
107 my $fn = $self->_genfn($jdate);
113 my ($self, $jdate) = @_;
115 my $fn = $self->_genfn($jdate);
116 return (stat $fn)[9];
119 # open the previous log file in sequence
123 my $jdate = $self->{jdate}->sub(1);
124 return $self->open($jdate, @_);
127 # open the next log file in sequence
131 my $jdate = $self->{jdate}->add(1);
132 return $self->open($jdate, @_);
135 # convert a date into the correct format from a unix date depending on its sort
140 if ($self->{'sort'} eq 'm') {
141 return Julian::Month->new(shift);
142 } elsif ($self->{'sort'} eq 'd') {
143 return Julian::Day->new(shift);
145 confess "shouldn't get here";
148 # write (actually append) to a file, opening new files as required
151 my ($self, $jdate, $line) = @_;
153 $self->{mode} ne ">>" ||
154 $jdate->year != $self->{jdate}->year ||
155 $jdate->thing != $self->{jdate}->thing) {
156 $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
159 return $self->{fh}->print("$line\n");
162 # write (actually append) using the current date to a file, opening new files as required
165 my ($self, $line) = @_;
167 my $date = $self->unixtoj($t);
168 return $self->write($date, $line);
171 # write (actually append) using a unix time to a file, opening new files as required
174 my ($self, $t, $line) = @_;
175 my $date = $self->unixtoj($t);
176 return $self->write($date, $line);
179 # close the log file handle
183 undef $self->{fh}; # close the filehandle
190 undef $self->{fh}; # close the filehandle
191 delete $self->{fh} if $self->{fh};
194 # log something in the system log
195 # this routine is exported to any module that declares DXLog
196 # it takes all its args and joins them together with the unixtime writes them out as one line
197 # The user is responsible for making sense of this!
201 $log->writeunix($t, join('^', $t, @_) );
206 DXDebug::dbg($_) for @_;