# Log.pm
#
# print string into log file
#
# Ralf Peine, 2004-05-26 Tue

use strict;

package Log;

use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $globalLog);

use Exporter;
use FileHandle;

@ISA         = qw(Exporter);
@EXPORT      = qw(LOG);
@EXPORT_OK   = qw();
%EXPORT_TAGS = (
		Functions => [qw(LOG)]
		);

$globalLog = undef;

sub new {
    my $self = shift;

    my $type = ref($self)  ||  $self;
    $self = bless {}, $type;
    $self->{'state'} = 'undef';
    return $self;
}

sub getFileName { 
    @_ = _getLogRef(@_); # check, if class call or instance call
    return $_[0]->{'file'};
}

sub getFileHandle { 
    @_ = _getLogRef(@_); # check, if class call or instance call
    return $_[0]->{'fh'};
}

sub setFileName {
    @_ = _getLogRef(@_); # check, if class call or instance call
    my $self = shift;
    my $file = shift;

    return unless (defined($file) && ref($self) && $self->{'state'} ne 'open');
    $self->{'file'} = $file;    
}

sub setFileNameWithTime {
    @_ = _getLogRef(@_); # check, if class call or instance call
    my $self = shift;
    my $file = shift;

    return unless (ref($self) && $file);
    return $self->setFileName(sprintf($file, &getDateString()));
}

sub getDateString {

    my @arr = localtime();
    @arr = reverse(@arr[0..5]);
    $arr[0] += 1900;
    $arr[1]++;
    return sprintf ('%d-%02d-%02d_%02d-%02d-%02d', @arr);
}

sub _getLogRef {
    my $self = shift;

    if (!ref($self)  ||  !$self->isa('Log')) {
	# called as class method
	splice (@_, 0, 0, $self); # 1. Arg was not self, put it back into @_
	unless (ref($globalLog)) {
	    $globalLog = new Log;
	}
	$self = $globalLog;
    }

    return ($self, @_);

}

sub open {
    @_ = _getLogRef(@_); # check, if class call or instance call

    my $self = shift;
    my $file = shift;

    $self->close();
    
    unless ($file) {
	unless ($self->{'file'}) {
	    $file = sprintf ('LOG_%s.txt', $self->getDateString());
	} else {
	    $file = $self->{'file'};
	}
    }

    $self->{'file'} = $file;
    $self->{'fh'} = new FileHandle (">$file");
    unless ($self->{'fh'}) {
	print "Can't open file $file for writing: $!\n";
	return undef;
    }
    $self->{'state'} = 'open';
    return $self;
}

sub close {
    @_ = _getLogRef(@_); # check, if class call or instance call
    my $self = shift;

    return undef unless ($self->{'fh'});
    $self->{'state'} = 'closed';
    return $self->{'fh'}->close();
}

sub LOG {
    @_ = _getLogRef(@_); # check, if class call or instance call
    my $self = shift;

    foreach my $msg (@_) {
	print $msg;
	$self->{'fh'}->print($msg) if ($self  &&  ref($self->{'fh'}));
    }
}

sub test {
    LOG ('123');
    Log::open();
    LOG ('456');
    print "\n  ",Log::getFileName(), "\n";
    Log::setFileName('file_1.txt');
    print "u ", Log::getFileName(), "\n";
    Log::close();
    Log::setFileName('file_2.txt');
    print "c ", Log::getFileName(), "\n";
    Log::setFileNameWithTime('file__%s.txt');
    print "c ", Log::getFileName(), "\n";
    my $log = new Log;
    $log->setFileName('file1.txt');
    print "\n  ", $log->getFileName(), "\n";
    $log->open('file2.txt');
    print "c ", $log->getFileName(), "\n";
    $log->LOG ('789');
    $log->setFileName('file3.txt');
    print "\nu ", $log->getFileName(), "\n";
    $log->close();
    $log->setFileNameWithTime('file4.txt');
    print "c ", $log->getFileName(), "\n";
    $log->setFileNameWithTime('file_%s.txt');
    print "c ", $log->getFileName(), "\n";
    $log->open();
    $log->LOG ('abcd');
    $log->setFileNameWithTime('file2_%s.txt');
    print "\nu ", $log->getFileName(), "\n";
    $log->close();
    $log->setFileNameWithTime('file2_%s.txt');
    print "c ", $log->getFileName(), "\n";

    print "press return\n";
    <STDIN>
}

# $|='1';
# test();

1;
