X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXLog.pm;h=287b766e8bdf95ad72614e49b6d997939eebb8ff;hb=refs%2Fheads%2Fnewdisc;hp=c13e26c1d8ffac4b8c641802c1af75f1ede6643f;hpb=2b58ccdf81685a1167a43c38705a0d84b9d8d661;p=spider.git diff --git a/perl/DXLog.pm b/perl/DXLog.pm index c13e26c1..287b766e 100644 --- a/perl/DXLog.pm +++ b/perl/DXLog.pm @@ -20,14 +20,14 @@ # # 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; @@ -38,13 +38,7 @@ 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; -$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 = {}; - $ref->{prefix} = "$main::data/$prefix"; + my $ref = bless {}, __PACKAGE__; + localdata_mv($prefix); + $ref->{prefix} = "$main::local_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; + + return $ref; } sub _genfn @@ -92,15 +89,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); # autofluahing off $self->{fh} = $fh; - $self->{jdate} = $jdate; - -# DXDebug::dbg("opening $self->{fn}\n") if isdbg("dxlog"); +# print "opening $self->{fn}\n"; return $self->{fh}; } @@ -153,10 +149,11 @@ sub unixtoj($$) sub write($$$) { my ($self, $jdate, $line) = @_; + return unless $self && $jdate; 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} $!"; } @@ -191,10 +188,18 @@ sub close sub DESTROY { my $self = shift; + delete $logs{$self}; undef $self->{fh}; # close the filehandle delete $self->{fh} if $self->{fh}; } +sub flushall +{ + foreach my $l (values %logs) { + $l->{fh}->flush if exists $l->{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 @@ -205,6 +210,13 @@ sub Log $log->writeunix($t, join('^', $t, @_) ); } +sub LogDbg +{ + my $cat = shift; + DXDebug::dbg($_) for @_; + Log($cat, @_); +} + sub Logclose { $log->close();