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.
27 @EXPORT = qw(Log LogDbg);
32 use vars qw($log $path);
40 # make the Log() export use this default file
43 $log = SMGLog->new("sys_log");
46 # create a log object that contains all the useful info needed
47 # prefix is the main directory off of the data directory
48 # suffix is the suffix after the month/day
51 my ($pkg, $prefix, $suffix) = @_;
53 my $dir = "$path/$prefix";
54 $ref->{prefix} = $dir;
55 $ref->{suffix} = $suffix || 'log';
56 $ref->{dayno} = int (time / 86400);
58 # make sure the directory exists
59 mkpath($dir, 0, 0777) unless -d $dir;
60 die "cannot create or access $dir $!" unless -d $dir;
62 my $self = bless $ref, $pkg;
70 $self->{mode} = shift if @_;
74 # open the appropriate data file
77 my ($self, $dayno, $mode) = @_;
79 my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
83 # if we are writing, check that the directory exists
85 my $dir = "$self->{prefix}/$year";
86 mkdir($dir, 0777) if ! -e $dir;
89 $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
90 $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
92 $self->{mode} = $mode || 'a+';
94 my $fh = new IO::File $self->{fn}, $mode, 0666;
97 $fh->autoflush(0) if $mode ne 'r'; # disable autoflushing if writable
100 $self->{year} = $year;
101 $self->{month} = $month;
103 $self->{dayno} = $dayno;
105 # DXDebug::dbg("dxlog", "opening $self->{fn}\n");
110 # open the previous log file in sequence
114 return $self->open($self->{dayno} - 1, @_);
117 # open the next log file in sequence
121 return $self->open($self->{dayno} + 1, @_);
124 # write (actually append) to a file, opening new files as required
127 my ($self, $dayno, $line) = @_;
128 if ((!$self->{fh} || $dayno != $self->{dayno}) && $self->{mode} ne "r") {
129 $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
132 return $self->{fh} ? $self->{fh}->print("$line\n") : undef;
135 # read a line from an opened file
139 confess "can't read $self->{fh} $!" unless $self->{fh};
140 return $self->{fh}->getline;
143 # write (actually append) using the current date to a file, opening new files as required
146 my ($self, $line) = @_;
147 my $dayno = int (time / 86400);
148 return $self->write($dayno, $line);
151 # write (actually append) using a unix time to a file, opening new files as required
154 my ($self, $t, $line) = @_;
155 my $dayno = int ($t / 86400);
156 return $self->write($dayno, $line);
159 # close the log file handle
163 undef $self->{fh}; # close the filehandle
172 undef $self->{fh}; # close the filehandle
173 delete $self->{fh} if $self->{fh};
178 $_[0]->{fh}->flush if $_[0]->{fh};
183 foreach my $v (values %open) {
188 sub flush_all { goto &flushall }
193 my $l = ref $_[0] ? shift : $log;
196 my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
197 $l->writeunix($t, "$ts $_") for @_;
203 Debug::dbg(@_) if Debug::isdbg('chan');