X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Flogger.pl;h=f550962a6041a6b428fdc0543856d2d865ce55a3;hb=3192b5c2d5d0f4218b2e4a41f81827bca445ee0c;hp=402c46c079bf1090e79c811a050d8168f85a5273;hpb=10e28f9bfeec2a46fb434f8165f419981054693d;p=infobot.git diff --git a/src/logger.pl b/src/logger.pl index 402c46c..f550962 100644 --- a/src/logger.pl +++ b/src/logger.pl @@ -1,21 +1,26 @@ # # logger.pl: logger functions! # Author: dms -# Version: v0.3 (20000731) +# Version: v0.4 (20000923) # FVersion: 19991205 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997 # use strict; -use vars qw($logDate $loggingstatus $statcount $bot_pid - $statcountfix $addressed $logcount $logtime); +use vars qw($statcount $bot_pid $forkedtime $statcountfix $addressed); +use vars qw($logDate $logold $logcount $logtime $logrepeat); use vars qw(@backlog); use vars qw(%param %file); require 5.001; $logtime = time(); +$logcount = 0; +$logrepeat = 0; +$logold = ""; + +$param{VEBOSITY} ||= 1; # lame fix for preload my %attributes = ( 'clear' => 0, @@ -88,7 +93,7 @@ sub openLog { } if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) { - my ($day,$month,$year) = (localtime(time()))[3,4,5]; + my ($day,$month,$year) = (localtime time())[3,4,5]; $logDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day); $file{log} .= "-".$logDate; } @@ -96,21 +101,18 @@ sub openLog { if (open(LOG, ">>$file{log}")) { &status("Opened logfile $file{log}."); LOG->autoflush(1); - $loggingstatus = 1; } else { - &status("cannot open logfile $file{log}; disabling."); - $loggingstatus = 0; + &status("cannot open logfile $file{log}; not logging."); } } sub closeLog { # lame fix for paramlogfile. return unless (&IsParam("logfile")); - return unless ($loggingstatus); + return unless (defined fileno LOG); - $loggingstatus = 0; - &status("Closed logfile ($file{log})."); close LOG; + &status("Closed logfile ($file{log})."); } ##### @@ -121,12 +123,11 @@ sub compress { my $okay = 0; if (! -f $file) { - # ironically this does not get logged :) &WARN("compress: file ($file) does not exist."); return 0; } - if (-f "$file.gz" or -f "$file.bz2") { + if ( -f "$file.gz" or -f "$file.bz2" ) { &WARN("compress: file.(gz|bz2) already exists."); return 0; } @@ -161,11 +162,13 @@ sub ERROR { sub WARN { return unless (&IsParam("WARN")); + return if ($_[0] =~ /^PERL: Subroutine \S+ redefined at/); + &status("${b_yellow}!WARN!$ob $_[0]"); } sub FIXME { - &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)"); + &status("${b_cyan}!FIXME!$ob $_[0]"); } sub TODO { @@ -186,12 +189,50 @@ sub status { my($input) = @_; my $status; - # return if input is null'ish. - return '' if ($input =~ /^\s*$/); - $input =~ s/\n+$//; - $input =~ s/\002|037//g; # bold,video,underline => remove. + if ($input eq $logold) { + # allow perl flooding + $logrepeat++ unless ($input =~ /PERL: Use of uninitialized/); + + # todo: prevent massive repetitive throttling. + if ($logrepeat >= 3) { + $logrepeat = 0; + &status("LOG: repeat throttle."); + sleep 1; + } + } else { + $logold = $input; + } + + # if it's not a scalar, attempt to warn and fix. + my $ref = ref $input; + if (defined $ref and $ref ne "") { + &status("status: 'input' is not scalar ($ref)."); - # pump up the stats (or loglinenum). + if ($ref eq "ARRAY") { + foreach (@$input) { + &WARN("status: '$_'."); + } + } + } + + # Something is using this w/ NULL. + if (!defined $input or $input =~ /^\s*$/) { + $input = "Blank status call? HELP HELP HELP"; + } + + for ($input) { + s/\n+$//; + s/\002|037//g; # bold,video,underline => remove. + } + + # does this work? + if ($input =~ /\n/) { + foreach (split(/\n/, $input)) { + &status($_); + } + } + + # pump up the stats. $statcount++; # fix style of output if process is child. @@ -202,19 +243,21 @@ sub status { ### LOG THROTTLING. ### TODO: move this _after_ printing? - my $time = time(); - my $reset = 0; - if ($logtime != $time) { - $reset++; - } elsif ($logtime == $time) { - if ($logcount < 25) { # too high? + my $time = time(); + my $reset = 0; + + if ($logtime == $time) { + if ($logcount < 25) { # too high? $logcount++; } else { sleep 1; &status("LOG: Throttling."); # recursive? $reset++; } + } else { # $logtime != $time. + $reset++; } + if ($reset) { $logtime = $time; $logcount = 0; @@ -225,7 +268,7 @@ sub status { $status = "!$statcount! ".$input; if ($statcount > 1000) { print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n"; - print LOG "VERB: ".(&Time2String(time() - $forkedtime))."\n"; + print LOG "VERB: ".(&Time2String($time - $forkedtime))."\n"; exit 0; } } else { @@ -239,9 +282,9 @@ sub status { if (&IsParam("VERBOSITY")) { if ($statcountfix) { - printf $_red."!%5d!".$ob." ", $statcount; + printf $_red."!%6d!".$ob." ", $statcount; } else { - printf $_green."[%5d]".$ob." ", $statcount; + printf $_green."[%6d]".$ob." ", $statcount; } # three uberstabs to Derek Moeller. @@ -286,18 +329,20 @@ sub status { # log the line into a file. return unless (&IsParam("logfile")); - return unless ($loggingstatus); + return unless (defined fileno LOG); # remove control characters from logging. - $input =~ s/\e\[[0-9;]+m//g; - $input =~ s/[\cA-\c_]//g; + for ($input) { + s/\e\[[0-9;]+m//g; # escape codes. + s/[\cA-\c_]//g; # control chars. + } $input = "FORK($$) ".$input if ($statcountfix); my $date; if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) { - $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]); + $date = sprintf("%02d:%02d.%02d", (localtime $time)[2,1,0]); - my ($day,$month,$year) = (localtime(time()))[3,4,5]; + my ($day,$month,$year) = (localtime $time)[3,4,5]; my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day); if (defined $logDate and $newlogDate != $logDate) { &closeLog(); @@ -305,10 +350,35 @@ sub status { &openLog(); } } else { - $date = time(); + $date = $time; } print LOG sprintf("%s %s\n", $date, $input); } +sub openSQLDebug { + if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) { + &ERROR("cannot open $param{'SQLDebug'}..."); + delete $param{'SQLDebug'}; + return 0; + } + + &status("Opened SQL Debug file: $param{'SQLDebug'}"); + return 1; +} + +sub closeSQLDebug { + close SQLDEBUG; + + &status("Closed SQL Debug file: $param{'SQLDebug'}"); +} + +sub SQLDebug { + return unless (&IsParam("SQLDebug")); + + return unless (fileno SQLDEBUG); + + print SQLDEBUG $_[0]."\n"; +} + 1;