Just some tidying up
[spider.git] / perl / DXLog.pm
index 748df39c782990d8216583f4c39782e1da3d6beb..0327a7c7d251913200acc69ee2e015aad9975503 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;
@@ -37,8 +37,11 @@ use Julian;
 use Carp;
 
 use strict;
+
 use vars qw($log);
 
+our %logobj;
+
 $log = new('log', 'dat', 'm');
 
 # create a log object that contains all the useful info needed
@@ -54,7 +57,9 @@ sub new
        
        # make sure the directory exists
        mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
-       return bless $ref;
+       my $self = bless $ref;
+       $logobj{$self} = $self;
+       return $self;
 }
 
 sub _genfn
@@ -85,15 +90,14 @@ sub open
        
        $mode = 'r' if !$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
+       $fh->autoflush(0) if $mode ne 'r'; # make it (not) autoflushing if writable
        $self->{fh} = $fh;
 
-       $self->{jdate} = $jdate;
-       
-#      DXDebug::dbg("opening $self->{fn}\n") if isdbg("dxlog");
+#      print "opening $self->{fn}\n";
        
        return $self->{fh};
 }
@@ -149,7 +153,7 @@ sub write($$$)
        if (!$self->{fh} || 
                $self->{mode} ne ">>" || 
                $jdate->year != $self->{jdate}->year || 
-               $jdate->thing != $self->{jdate}->year) {
+               $jdate->thing != $self->{jdate}->thing) {
                $self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
        }
 
@@ -181,9 +185,17 @@ sub close
        delete $self->{fh};     
 }
 
+sub flush_all
+{
+       foreach my $l (values %logobj) {
+               $l->{fh}->flush if exists $l->{fh};
+       }
+}
+
 sub DESTROY
 {
        my $self = shift;
+       delete $logobj{$self};
        undef $self->{fh};                      # close the filehandle
        delete $self->{fh} if $self->{fh};
 }
@@ -194,12 +206,21 @@ sub DESTROY
 # The user is responsible for making sense of this!
 sub Log
 {
+       return unless $log;
+       
        my $t = time;
        $log->writeunix($t, join('^', $t, @_) );
 }
 
+sub LogDbg
+{
+       DXDebug::dbg($_) for @_;
+       Log(@_);
+}
+
 sub Logclose
 {
        $log->close();
+       undef $log;
 }
 1;