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';
57 # make sure the directory exists
58 mkpath($dir, 0, 0777) unless -d $dir;
59 die "cannot create or access $dir $!" unless -d $dir;
61 my $self = bless $ref, $pkg;
69 $self->{mode} = shift if @_;
73 # open the appropriate data file
76 my ($self, $dayno, $mode) = @_;
78 my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
82 # if we are writing, check that the directory exists
84 my $dir = "$self->{prefix}/$year";
85 mkdir($dir, 0777) if ! -e $dir;
88 $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
89 $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
91 $self->{mode} = $mode || 'a+';
93 my $fh = new IO::File $self->{fn}, $mode, 0666;
96 $fh->autoflush(0) if $mode ne 'r'; # disable autoflushing if writable
99 $self->{year} = $year;
100 $self->{month} = $month;
102 $self->{dayno} = $dayno;
104 # DXDebug::dbg("dxlog", "opening $self->{fn}\n");
109 # open the previous log file in sequence
113 return $self->open($self->{dayno} - 1, @_);
116 # open the next log file in sequence
120 return $self->open($self->{dayno} + 1, @_);
123 # write (actually append) to a file, opening new files as required
126 my ($self, $dayno, $line) = @_;
127 if ((!$self->{fh} || $dayno != $self->{dayno}) && $self->{mode} ne "r") {
128 $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
131 return $self->{fh} ? $self->{fh}->print("$line\n") : undef;
134 # read a line from an opened file
138 confess "can't read $self->{fh} $!" unless $self->{fh};
139 return $self->{fh}->getline;
142 # write (actually append) using the current date to a file, opening new files as required
145 my ($self, $line) = @_;
146 my $dayno = int (time / 86400);
147 return $self->write($dayno, $line);
150 # write (actually append) using a unix time to a file, opening new files as required
153 my ($self, $t, $line) = @_;
154 my $dayno = int ($t / 86400);
155 return $self->write($dayno, $line);
158 # close the log file handle
162 undef $self->{fh}; # close the filehandle
171 undef $self->{fh}; # close the filehandle
172 delete $self->{fh} if $self->{fh};
177 $_[0]->{fh}->flush if $_[0]->{fh};
182 foreach my $v (values %open) {
187 sub flush_all { goto &flushall }
192 my $l = ref $_[0] ? shift : $log;
195 my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
196 $l->writeunix($t, "$ts $_") for @_;
202 Debug::dbg(@_) if Debug::isdbg('chan');