#========== more robust logging ===============# sub OpenLog { my $Log = shift; # logfile to OPEN my $LogCurrSize; $LogCurrSize = -s $Log || 0; if( $LogCurrSize < $CONFIG{ LogMaxSz } ) { # below the max log file size, so just open it open( FILE, ">>$Log" ) or die "Unable to APPEND to log ($Log) : $!\n"; } else { # were over the max size. we need to open a new logfile # we need to rotate the logs before we create a new one # log moves to Log.1 # log.1 moves to log.2 # ..... # log.n is removed # first remove the oldest log if it exists if ( -e "$Log.${CONFIG{ LogMaxGens }}") { unlink( "$Log.${CONFIG{ LogMaxGens }}" ) or die "Unable to remove $Log.${CONFIG{ LogMaxGens }} : $!\n"; } my $CurrGeneration = $CONFIG{ LogMaxGens }; while ( --$CurrGeneration ) { # if the current generation exist the rename it if( -e "$Log.$CurrGeneration" ) { rename( "$Log.$CurrGeneration", ("$Log." . ( $CurrGeneration+1)) ) or die "Unable to move $Log.$CurrGeneration to $Log.",( $CurrGeneration+1)," : $!\n"; } } rename( "$Log", "$Log.1" ) or die "unable to move $Log to $Log.1 : $!\n"; open( FILE, ">$Log" ) or die "Unable to CREATE log ($Log) : $!\n"; } return( *FILE ); # return the filehandle } sub LogIt { my $fhLog = shift; # file handle to write the message to my $Message = shift; # message to write to the logfile my %caller_info; @caller_info{"package","filename","line","subroutine","hasargs","wantarray","evaltext","is_required","hints", "bitmask", "hinthash"} = caller(0); # @caller_info{ "subroutine" } = defined( ${[ caller(1) ]}[3]) ? ${[ caller(1) ]}[3] : (${[ caller(0) ]}[3] =~ s/::LogIt//); if ( defined( ${[ caller(1) ]}[3]) ) { $caller_info{ "subroutine" } = ${[ caller(1) ]}[3]; } else { my ($temp_sub) = ${[ caller(0) ]}[3]; $temp_sub =~ s/::LogIt//; $caller_info{ "subroutine" } = $temp_sub; } my ($strDate ) = strftime("%Y-%m-%d %H:%M:%S", localtime()); printf $fhLog ( "%-20s %-25s %-5s %-25s %s\n", $strDate, $caller_info{"filename"}, $caller_info{"line"}, $caller_info{"subroutine"}, $Message); }