X-Git-Url: http://www.dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXLog.pm;h=3a6e0e3589ec86b3b53ff00cb665e804b80299e0;hb=099c6b4a10d9f1e7471b0c94273cd992b5814cdc;hp=f73c3195a83285f47499a7728490934364f3bd55;hpb=ce0803e2110199eca1ad15cd6f66abde58cb949b;p=spider.git diff --git a/perl/DXLog.pm b/perl/DXLog.pm index f73c3195..3a6e0e35 100644 --- a/perl/DXLog.pm +++ b/perl/DXLog.pm @@ -25,14 +25,21 @@ package DXLog; +require Exporter; +@ISA = qw(Exporter); +@EXPORT = qw(Log); + use FileHandle; use DXVars; -use DXDebug; +use DXDebug (); use DXUtil; use Julian; use Carp; use strict; +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 @@ -64,7 +71,8 @@ sub open delete $self->{mode}; } - $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing; + $self->{fn} = sprintf "$self->{prefix}/$year/%02d", $thing if $self->{sort} eq 'm'; + $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing if $self->{sort} eq 'd'; $self->{fn} .= ".$self->{suffix}" if $self->{suffix}; $mode = 'r' if !$mode; @@ -76,7 +84,7 @@ sub open $self->{year} = $year; $self->{thing} = $thing; - dbg("dxlog", "opening $self->{fn}\n"); + DXDebug::dbg("dxlog", "opening $self->{fn}\n"); return $self->{fh}; } @@ -105,27 +113,47 @@ sub opennext return $self->open($self->{year}, $self->{thing}, @_); } +# 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::unixtojm(shift); + } elsif ($self->{sort} eq 'd') { + return Julian::unixtoj(shift); + } + confess "shouldn't get here"; +} + # write (actually append) to a file, opening new files as required 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} $!"; - - $self->{fh}->print("$line\n"); - return $self; + if (!$self->{fh} || + $self->{mode} ne ">>" || + $year != $self->{year} || + $thing != $self->{thing}) { + $self->open($year, $thing, ">>") or confess "can't open $self->{fn} $!"; + } + + return $self->{fh}->print("$line\n"); } # write (actually append) using the current date to a file, opening new files as required sub writenow { my ($self, $line) = @_; - my @date = unixtoj(time) if $self->{sort} = 'd'; - @date = unixtojm(time) if $self->{sort} = 'm'; - + 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); } @@ -138,10 +166,20 @@ sub close delete $self->{mode}; } -sub DESTROY # catch undefs and do what is required further do the tree +# 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 DESTROY # catch undefs and do what is required further down the tree { my $self = shift; - dbg("dxlog", "closing $self->{fn}\n"); + DXDebug::dbg("dxlog", "closing $self->{fn}\n"); undef $self->{fh} if defined $self->{fh}; } 1;