added Log Parsing
[spider.git] / perl / DXLog.pm
index 6c2eeee99202992b4b8adc7c8ebaaed45afcdfa0..3a6e0e3589ec86b3b53ff00cb665e804b80299e0 100644 (file)
 
 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
+# sort is 'm' for monthly, 'd' for daily 
+sub new
+{
+       my ($prefix, $suffix, $sort) = @_;
+       my $ref = {};
+       $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};
+       return bless $ref;
+}
+
+# open the appropriate data file
+sub open
+{
+       my ($self, $year, $thing, $mode) = @_;
+       
+       # if we are writing, check that the directory exists
+       if (defined $mode) {
+               my $dir = "$self->{prefix}/$year";
+               mkdir($dir, 0777) if ! -e $dir;
+               $self->{mode} = $mode;
+       } else {
+               delete $self->{mode};
+       }
+       
+       $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;
+       my $fh = new FileHandle $self->{fn}, $mode;
+       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;
+       
+       DXDebug::dbg("dxlog", "opening $self->{fn}\n");
+       
+       return $self->{fh};
+}
+
+# open the previous log file in sequence
+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}, @_);
+}
+
+# open the next log file in sequence
+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);
+       }
+       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) = @_;
+       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 $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
+sub close
+{
+       my $self = shift;
+       undef $self->{fh};                      # close the filehandle
+       delete $self->{fh};
+       delete $self->{mode};
+}
+
+# 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;
+       DXDebug::dbg("dxlog", "closing $self->{fn}\n");
+       undef $self->{fh} if defined $self->{fh};
+} 
+1;