fixed duplicate spot, always make clean ending
[spider.git] / perl / DXLog.pm
index f96ab44aa092863d036060b3462d1e4aecea3c2f..db7dffdd430a0977e306fa83d0afcff4ee671e95 100644 (file)
 # 
 # Copyright (c) - 1998 Dirk Koopman G1TLH
 #
-# $Id$
+#
 #
 
 package DXLog;
 
 require Exporter;
 @ISA = qw(Exporter);
-@EXPORT = qw(Log Logclose);
+@EXPORT = qw(Log LogDbg Logclose);
 
 use IO::File;
 use DXVars;
 use DXUtil;
 use Julian;
 
-use Carp;
+use Carp qw(confess cluck);
 
 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);
+use vars qw($log %logs);
 
 $log = new('log', 'dat', 'm');
 
@@ -54,14 +48,17 @@ $log = new('log', 'dat', 'm');
 sub new
 {
        my ($prefix, $suffix, $sort) = @_;
-       my $ref = {};
+       my $ref = bless {}, __PACKAGE__;
        $ref->{prefix} = "$main::data/$prefix";
        $ref->{suffix} = $suffix if $suffix;
        $ref->{sort} = $sort;
        
        # make sure the directory exists
        mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
-       return bless $ref;
+       $logs{$ref} = $ref;
+       $ref->{jdate} = $ref->unixtoj($main::systime);
+
+       return $ref;
 }
 
 sub _genfn
@@ -96,7 +93,7 @@ sub open
        
        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
+       $fh->autoflush(0);                      # autofluahing off
        $self->{fh} = $fh;
 
 #      print "opening $self->{fn}\n";
@@ -152,10 +149,14 @@ sub unixtoj($$)
 sub write($$$)
 {
        my ($self, $jdate, $line) = @_;
+       cluck("Log::write \$jdate undefined") unless $jdate;
+#      cluck("Log::write \$self->jdate undefined") unless $self->{jdate};
        if (!$self->{fh} || 
-               $self->{mode} ne ">>" || 
-               $jdate->year != $self->{jdate}->year || 
-               $jdate->thing != $self->{jdate}->thing) {
+               $self->{mode} ne ">>" ||
+               $jdate->year !=
+               $self->{jdate}->year ||
+               $jdate->thing
+               != $self->{jdate}->thing) {
                $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
        }
 
@@ -187,11 +188,11 @@ sub close
        delete $self->{fh};     
 }
 
-sub DESTROY
+sub flushall
 {
-       my $self = shift;
-       undef $self->{fh};                      # close the filehandle
-       delete $self->{fh} if $self->{fh};
+       foreach my $l (values %logs) {
+               $l->{fh}->flush if exists $l->{fh};
+       }
 }
 
 # log something in the system log 
@@ -200,10 +201,16 @@ sub DESTROY
 # The user is responsible for making sense of this!
 sub Log
 {
-       my $t = time;
+       my $t = $main::systime;
        $log->writeunix($t, join('^', $t, @_) );
 }
 
+sub LogDbg
+{
+       DXDebug::dbg($_) for @_;
+       Log(@_);
+}
+
 sub Logclose
 {
        $log->close();