package DXLog;
-use FileHandle;
+require Exporter;
+@ISA = qw(Exporter);
+@EXPORT = qw(Log LogDbg Logclose);
+
+use IO::File;
use DXVars;
-use DXDebug;
use DXUtil;
use Julian;
+
use Carp;
use strict;
+use vars qw($VERSION $BRANCH);
+$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
+$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
+$main::build += $VERSION;
+$main::branch += $BRANCH;
+
+use vars qw($log);
+
+$log = new('log', 'dat', 'm');
+
# create a log object that contains all the useful info needed
# prefix is the main directory off of the data directory
# sort is 'm' for monthly, 'd' for daily
$ref->{prefix} = "$main::data/$prefix";
$ref->{suffix} = $suffix if $suffix;
$ref->{sort} = $sort;
-
+
# make sure the directory exists
- mkdir($ref->{prefix}, 0777) if ! -e $ref->{prefix};
+ mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
return bless $ref;
}
+sub _genfn
+{
+ my ($self, $jdate) = @_;
+ my $year = $jdate->year;
+ my $thing = $jdate->thing;
+
+ my $fn = sprintf "$self->{prefix}/$year/%02d", $thing if $jdate->isa('Julian::Month');
+ $fn = sprintf "$self->{prefix}/$year/%03d", $thing if $jdate->isa('Julian::Day');
+ $fn .= ".$self->{suffix}" if $self->{suffix};
+ return $fn;
+}
+
# open the appropriate data file
sub open
{
- my ($self, $year, $thing, $mode) = @_;
+ my ($self, $jdate, $mode) = @_;
# if we are writing, check that the directory exists
if (defined $mode) {
+ my $year = $jdate->year;
my $dir = "$self->{prefix}/$year";
mkdir($dir, 0777) if ! -e $dir;
- $self->{mode} = $mode;
- } else {
- delete $self->{mode};
}
-
- $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing;
- $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
+
+ $self->{fn} = $self->_genfn($jdate);
$mode = 'r' if !$mode;
- my $fh = new FileHandle $self->{fn}, $mode;
+ $self->{mode} = $mode;
+ $self->{jdate} = $jdate;
+
+ my $fh = new IO::File $self->{fn}, $mode, 0666;
return undef if !$fh;
$fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
$self->{fh} = $fh;
- $self->{year} = $year;
- $self->{thing} = $thing;
-
- dbg("dxlog", "opening $self->{fn}\n");
+# print "opening $self->{fn}\n";
return $self->{fh};
}
+sub delete($$)
+{
+ my ($self, $jdate) = @_;
+ my $fn = $self->_genfn($jdate);
+ unlink $fn;
+}
+
+sub mtime($$)
+{
+ my ($self, $jdate) = @_;
+
+ my $fn = $self->_genfn($jdate);
+ return (stat $fn)[9];
+}
+
# open the previous log file in sequence
-sub openprev
+sub openprev($$)
{
my $self = shift;
- if ($self->{sort} eq 'm') {
- ($self->{year}, $self->{thing}) = Julian::subm($self->{year}, $self->{thing}, 1);
- } elsif ($self->{sort} eq 'd') {
- ($self->{year}, $self->{thing}) = Julian::sub($self->{year}, $self->{thing}, 1);
- }
- return $self->open($self->{year}, $self->{thing}, @_);
+ my $jdate = $self->{jdate}->sub(1);
+ return $self->open($jdate, @_);
}
# open the next log file in sequence
-sub opennext
+sub opennext($$)
{
my $self = shift;
- if ($self->{sort} eq 'm') {
- ($self->{year}, $self->{thing}) = Julian::addm($self->{year}, $self->{thing}, 1);
- } elsif ($self->{sort} eq 'd') {
- ($self->{year}, $self->{thing}) = Julian::add($self->{year}, $self->{thing}, 1);
+ my $jdate = $self->{jdate}->add(1);
+ return $self->open($jdate, @_);
+}
+
+# convert a date into the correct format from a unix date depending on its sort
+sub unixtoj($$)
+{
+ my $self = shift;
+
+ if ($self->{'sort'} eq 'm') {
+ return Julian::Month->new(shift);
+ } elsif ($self->{'sort'} eq 'd') {
+ return Julian::Day->new(shift);
}
- return $self->open($self->{year}, $self->{thing}, @_);
+ confess "shouldn't get here";
}
# write (actually append) to a file, opening new files as required
-sub write
+sub write($$$)
{
- my ($self, $year, $thing, $line) = @_;
- $self->open($year, $thing, ">>") if (!$self->{fh} ||
- $self->{mode} ne ">>" ||
- $year != $self->{year} ||
- $thing != $self->{thing})
- or confess "can't open $self->{fn} $!";
+ my ($self, $jdate, $line) = @_;
+ if (!$self->{fh} ||
+ $self->{mode} ne ">>" ||
+ $jdate->year != $self->{jdate}->year ||
+ $jdate->thing != $self->{jdate}->thing) {
+ $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
+ }
- $self->{fh}->print("$line\n");
- return $self;
+ return $self->{fh}->print("$line\n");
}
# write (actually append) using the current date to a file, opening new files as required
-sub writenow
+sub writenow($$)
{
my ($self, $line) = @_;
- my @date = unixtoj(time) if $self->{sort} = 'd';
- @date = unixtojm(time) if $self->{sort} = 'm';
-
- return $self->write(@date, $line);
+ my $t = time;
+ my $date = $self->unixtoj($t);
+ return $self->write($date, $line);
+}
+
+# write (actually append) using a unix time to a file, opening new files as required
+sub writeunix($$$)
+{
+ my ($self, $t, $line) = @_;
+ my $date = $self->unixtoj($t);
+ return $self->write($date, $line);
}
# close the log file handle
{
my $self = shift;
undef $self->{fh}; # close the filehandle
- delete $self->{fh};
- delete $self->{mode};
+ delete $self->{fh};
}
-sub DESTROY # catch undefs and do what is required further do the tree
+sub DESTROY
{
my $self = shift;
- dbg("dxlog", "closing $self->{fn}\n");
- undef $self->{fh} if defined $self->{fh};
-}
+ undef $self->{fh}; # close the filehandle
+ delete $self->{fh} if $self->{fh};
+}
+
+# log something in the system log
+# this routine is exported to any module that declares DXLog
+# it takes all its args and joins them together with the unixtime writes them out as one line
+# The user is responsible for making sense of this!
+sub Log
+{
+ my $t = time;
+ $log->writeunix($t, join('^', $t, @_) );
+}
+
+sub LogDbg
+{
+ DXDebug::dbg($_[$#_]);
+ Log(@_);
+}
+
+sub Logclose
+{
+ $log->close();
+}
1;