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);
31 use vars qw($log $path);
37 # make the Log() export use this default file
40 $log = SMGLog->new("sys_log");
43 # create a log object that contains all the useful info needed
44 # prefix is the main directory off of the data directory
45 # suffix is the suffix after the month/day
48 my ($pkg, $prefix, $suffix) = @_;
50 my $dir = "$path/$prefix";
51 $ref->{prefix} = $dir;
52 $ref->{suffix} = $suffix || 'log';
54 # make sure the directory exists
55 mkpath($dir, 0, 0777) unless -d $dir;
56 die "cannot create or access $dir $!" unless -d $dir;
58 return bless $ref, $pkg;
61 # open the appropriate data file
64 my ($self, $dayno, $mode) = @_;
66 my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
70 # if we are writing, check that the directory exists
72 my $dir = "$self->{prefix}/$year";
73 mkdir($dir, 0777) if ! -e $dir;
76 $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
77 $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
79 $self->{mode} = $mode || 'r';
81 my $fh = new IO::File $self->{fn}, $mode, 0666;
84 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
87 $self->{year} = $year;
88 $self->{month} = $month;
90 $self->{dayno} = $dayno;
92 # DXDebug::dbg("dxlog", "opening $self->{fn}\n");
97 # open the previous log file in sequence
101 return $self->open($self->{dayno} - 1, @_);
104 # open the next log file in sequence
108 return $self->open($self->{dayno} + 1, @_);
111 # write (actually append) to a file, opening new files as required
114 my ($self, $dayno, $line) = @_;
116 $self->{mode} ne ">>" ||
117 $dayno != $self->{dayno}) {
118 $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
121 return $self->{fh}->print("$line\n");
124 # read a line from an opened file
128 confess "can't read $self->{fh} $!" unless $self->{fh};
129 return $self->{fh}->getline;
132 # write (actually append) using the current date to a file, opening new files as required
135 my ($self, $line) = @_;
136 my $dayno = int (time / 86400);
137 return $self->write($dayno, $line);
140 # write (actually append) using a unix time to a file, opening new files as required
143 my ($self, $t, $line) = @_;
144 my $dayno = int ($t / 86400);
145 return $self->write($dayno, $line);
148 # close the log file handle
152 undef $self->{fh}; # close the filehandle
159 undef $self->{fh}; # close the filehandle
160 delete $self->{fh} if $self->{fh};
165 my $l = ref $_[0] ? shift : $log;
168 my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
169 $l->writeunix($t, "$ts $_") for @_;
175 Debug::dbg(@_) if Debug::isdbg('chan');