2 # the general purpose logging machine
4 # This module is designed to allow you to log stuff in SMG format
6 # The idea is that you give it a prefix which is a directory and then
7 # the system will log stuff to a directory structure which looks like:-
9 # ./logs/<prefix>/yyyy/mmdd.[log|<optional suffix]
11 # Routines are provided to read these files in and to append to them
13 # Copyright (c) - 1998-2007 Dirk Koopman G1TLH
15 # This library is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
19 package DWeather::Logger;
27 @EXPORT = qw(Log LogDbg);
31 use vars qw($log $path);
35 # make the Log() export use this default file
38 my $default_dir = shift || 'sys_log';
39 $log = __PACKAGE__->new($default_dir) unless $log;
42 # create a log object that contains all the useful info needed
43 # prefix is the main directory off of the data directory
44 # suffix is the suffix after the month/day
47 my ($pkg, $prefix, $suffix) = @_;
49 my $dir = "$path/$prefix";
50 $ref->{prefix} = $dir;
51 $ref->{suffix} = $suffix || 'log';
53 # make sure the directory exists
54 mkpath($dir, 0, 0777) unless -d $dir;
55 die "cannot create or access $dir $!" unless -d $dir;
57 return bless $ref, $pkg;
60 # open the appropriate data file
63 my ($self, $dayno, $mode) = @_;
65 my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
69 # if we are writing, check that the directory exists
71 my $dir = "$self->{prefix}/$year";
72 mkdir($dir, 0777) if ! -e $dir;
75 $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
76 $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
78 $self->{mode} = $mode || 'r';
80 my $fh = new IO::File $self->{fn}, $mode, 0666;
83 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
86 $self->{year} = $year;
87 $self->{month} = $month;
89 $self->{dayno} = $dayno;
91 # DXDebug::dbg("dxlog", "opening $self->{fn}\n");
96 # open the previous log file in sequence
100 return $self->open($self->{dayno} - 1, @_);
103 # open the next log file in sequence
107 return $self->open($self->{dayno} + 1, @_);
110 # write (actually append) to a file, opening new files as required
113 my ($self, $dayno, $line) = @_;
115 $self->{mode} ne ">>" ||
116 $dayno != $self->{dayno}) {
117 $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
120 return $self->{fh}->print("$line\n");
123 # read a line from an opened file
127 confess "can't read $self->{fh} $!" unless $self->{fh};
128 return $self->{fh}->getline;
131 # write (actually append) using the current date to a file, opening new files as required
134 my ($self, $line) = @_;
135 my $dayno = int (time / 86400);
136 return $self->write($dayno, $line);
139 # write (actually append) using a unix time to a file, opening new files as required
142 my ($self, $t, $line) = @_;
143 my $dayno = int ($t / 86400);
144 return $self->write($dayno, $line);
147 # close the log file handle
151 undef $self->{fh}; # close the filehandle
158 undef $self->{fh}; # close the filehandle
159 delete $self->{fh} if $self->{fh};
164 my $l = ref $_[0] ? shift : $log;
167 my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
168 $l->writeunix($t, "$ts $_") for @_;
174 DWeather::Debug::dbg(@_) if DWeather::Debug::isdbg('chan');