@ISA = qw(Exporter);
@EXPORT = qw(Log LogDbg);
+$VERSION = 1.20;
use strict;
$log = undef;
$path = './logs';
+my %open;
+
+init();
+
# make the Log() export use this default file
sub init
{
- my $default_dir = shift || 'sys_log';
- $log = __PACKAGE__->new($default_dir) unless $log;
+ $log = __PACKAGE__->new("sys_log");
}
# create a log object that contains all the useful info needed
mkpath($dir, 0, 0777) unless -d $dir;
die "cannot create or access $dir $!" unless -d $dir;
- return bless $ref, $pkg;
+ my $self = bless $ref, $pkg;
+ $open{$self} = $self;
+ return $self;
+}
+
+sub mode
+{
+ my $self = shift;
+ $self->{mode} = shift if @_;
+ return $self->{mode};
}
# open the appropriate data file
$self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
$self->{fn} .= ".$self->{suffix}" if $self->{suffix};
- $self->{mode} = $mode || 'r';
+ $self->{mode} = $mode || 'a+';
my $fh = new IO::File $self->{fn}, $mode, 0666;
return unless $fh;
- $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
+ $fh->autoflush(0) if $mode ne 'r'; # disable autoflushing if writable
$self->{fh} = $fh;
$self->{year} = $year;
sub write
{
my ($self, $dayno, $line) = @_;
- if (!$self->{fh} ||
- $self->{mode} ne ">>" ||
- $dayno != $self->{dayno}) {
- $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
+ if (!$self->{fh} || $self->{mode} ne "r" || $dayno != $self->{dayno}) {
+ $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
}
return $self->{fh}->print("$line\n");
sub DESTROY
{
my $self = shift;
+
+ delete $open{$self};
undef $self->{fh}; # close the filehandle
delete $self->{fh} if $self->{fh};
}
+sub flush
+{
+ $_[0]->{fh}->flush if $_[0]->{fh};
+}
+
+sub flushall
+{
+ foreach my $v (values %open) {
+ $v->flush;
+ }
+}
+
+sub flush_all { goto &flushall }
+
+
sub Log
{
my $l = ref $_[0] ? shift : $log;
sub LogDbg
{
Log(@_);
- DWeather::Debug::dbg(@_) if DWeather::Debug::isdbg('chan');
+ Debug::dbg(@_) if Debug::isdbg('chan');
}
-init();
-
1;