# Funktionen für das Loggen von Programmen # Jan Tappenbeck, 2015 # # # Historie # 2015-03-27 von Privat übernommen package EBL::ebl_reporting; use strict; use warnings; use IO::File; #use Time::localtime; require Time::localtime; use 5.14.0; sub new { bless({ log_on => 1, shell_on => 1 }, shift) } sub show_status { my $self=shift; print "Log: ", $self->log(), ", Shell: ", $self->shell()."\n"; } sub open { my $class=shift; my $log_file=shift; # Referenz auf STDOUT my $fh=\*STDOUT; # wenn object und kein File => neuer Name = alter Name if(ref $class && !$log_file) { $log_file=$class->{file}; $class->{fh}->close(); } # versuchen Fh zu öffnen if($log_file) { my $ofh=IO::File->new(); my $mode='>'; $mode='>>' if( ref $class ); if($ofh->open($log_file,$mode) ) { $fh=$ofh; } else { $fh->print("ERROR open $log_file ($!)\nwrite to STDOUT\n"); } } # objektwerte neu setzen wenn objekt if( ref $class ) { $class->{fh}=$fh; $class->{file}=$log_file; return $class; } # neues Objekt return bless({ fh=>$fh, file=>$log_file, on=>1 },$class); } # log(1) => logging an # log(0) => logging aus # log() => aktueller Status sub log { my $self=shift; $self->{log_on}=shift() if(@_); return $self->{log_on}; } # shell(1) => zusätzliche Shell-Ausgabe an # shell(0) => zusätzliche Shell-Ausgabe aus # shell() => aktueller Status sub shell { my $self=shift; $self->{shell_on}=shift() if(@_); return $self->{shell_on}; } # filename sub file { shift->{file}; } # filehandle sub fh { shift->{fh}; } # write to file mit optionaler Ausgabe auf die Shell sub write { my ($self, $msg) = @_; $self->fh->print($msg."\n") if $self->log(); print($msg."\n") if $self->shell(); }#end-write # close filehandle sub close { shift->fh->close(); } # wahr wenn Datei geöffnet sub opened { shift->fh->opened(); } 1;