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 Logclose);
42 $log = new('log', 'dat', 'm');
44 # create a log object that contains all the useful info needed
45 # prefix is the main directory off of the data directory
46 # sort is 'm' for monthly, 'd' for daily
49 my ($prefix, $suffix, $sort) = @_;
51 $ref->{prefix} = "$main::data/$prefix";
52 $ref->{suffix} = $suffix if $suffix;
53 $ref->{'sort'} = $sort;
55 # make sure the directory exists
56 mkdir($ref->{prefix}, 0777) if ! -e $ref->{prefix};
60 # open the appropriate data file
63 my ($self, $year, $thing, $mode) = @_;
65 # if we are writing, check that the directory exists
67 my $dir = "$self->{prefix}/$year";
68 mkdir($dir, 0777) if ! -e $dir;
71 $self->{fn} = sprintf "$self->{prefix}/$year/%02d", $thing if $self->{'sort'} eq 'm';
72 $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing if $self->{'sort'} eq 'd';
73 $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
75 $mode = 'r' if !$mode;
76 $self->{mode} = $mode;
78 my $fh = new FileHandle $self->{fn}, $mode, 0666;
80 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
83 $self->{year} = $year;
84 $self->{thing} = $thing;
86 DXDebug::dbg("dxlog", "opening $self->{fn}\n");
91 # open the previous log file in sequence
95 if ($self->{'sort'} eq 'm') {
96 ($self->{year}, $self->{thing}) = Julian::subm($self->{year}, $self->{thing}, 1);
97 } elsif ($self->{'sort'} eq 'd') {
98 ($self->{year}, $self->{thing}) = Julian::sub($self->{year}, $self->{thing}, 1);
100 return $self->open($self->{year}, $self->{thing}, @_);
103 # open the next log file in sequence
107 if ($self->{'sort'} eq 'm') {
108 ($self->{year}, $self->{thing}) = Julian::addm($self->{year}, $self->{thing}, 1);
109 } elsif ($self->{'sort'} eq 'd') {
110 ($self->{year}, $self->{thing}) = Julian::add($self->{year}, $self->{thing}, 1);
112 return $self->open($self->{year}, $self->{thing}, @_);
115 # convert a date into the correct format from a unix date depending on its sort
120 if ($self->{'sort'} eq 'm') {
121 return Julian::unixtojm(shift);
122 } elsif ($self->{'sort'} eq 'd') {
123 return Julian::unixtoj(shift);
125 confess "shouldn't get here";
128 # write (actually append) to a file, opening new files as required
131 my ($self, $year, $thing, $line) = @_;
133 $self->{mode} ne ">>" ||
134 $year != $self->{year} ||
135 $thing != $self->{thing}) {
136 $self->open($year, $thing, ">>") or confess "can't open $self->{fn} $!";
139 return $self->{fh}->print("$line\n");
142 # write (actually append) using the current date to a file, opening new files as required
145 my ($self, $line) = @_;
147 my @date = $self->unixtoj($t);
148 return $self->write(@date, $line);
151 # write (actually append) using a unix time to a file, opening new files as required
154 my ($self, $t, $line) = @_;
155 my @date = $self->unixtoj($t);
156 return $self->write(@date, $line);
159 # close the log file handle
163 undef $self->{fh}; # close the filehandle
167 # log something in the system log
168 # this routine is exported to any module that declares DXLog
169 # it takes all its args and joins them together with the unixtime writes them out as one line
170 # The user is responsible for making sense of this!
174 $log->writeunix($t, join('^', $t, @_) );