]> git.donarmstrong.com Git - infobot.git/blobdiff - src/logger.pl
- now support verstats for those who don't reply to "CTCP VERSION
[infobot.git] / src / logger.pl
index 245275601b6e8cb579a31adc61395794a7e4f10c..474188c6d88aca2b6cee7aba9c263cec48418f0c 100644 (file)
@@ -9,9 +9,9 @@
 use strict;
 
 use vars qw($statcount $bot_pid $forkedtime $statcountfix $addressed);
-use vars qw($logDate $logold $logcount $logtime $logrepeat);
+use vars qw($logDate $logold $logcount $logtime $logrepeat $running);
 use vars qw(@backlog);
-use vars qw(%param %file);
+use vars qw(%param %file %cache);
 
 require 5.001;
 
@@ -20,6 +20,8 @@ $logcount     = 0;
 $logrepeat     = 0;
 $logold                = "";
 
+$param{VEBOSITY} ||= 1;                # lame fix for preload
+
 my %attributes = (
        'clear'      => 0,
        'reset'      => 0,
@@ -91,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;
     }
@@ -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;
     }
@@ -167,7 +168,7 @@ sub WARN {
 }
 
 sub FIXME {
-    &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
+    &status("${b_cyan}!FIXME!$ob $_[0]");
 }
 
 sub TODO {
@@ -188,21 +189,34 @@ sub status {
     my($input) = @_;
     my $status;
 
+    # a way to hook onto status without looping.
+    # todo: find why $channels{undef} is created.
+    if (0 and $running and !$cache{statusSafe}) {
+       &ircCheck();
+    }
+
     if ($input eq $logold) {
-       $logrepeat++;
+       # allow perl flooding
+       $logrepeat++ unless ($input =~ /PERL: Use of uninitialized/);
 
+       # todo: prevent massive repetitive throttling.
        if ($logrepeat >= 3) {
            $logrepeat = 0;
            &status("LOG: repeat throttle.");
+           # we block it to ensure sequence of logging is intact.
+           # could go with $conn->schedule but that's evil :)
            sleep 1;
        }
+    } else {
+       $logold = $input;
     }
-    $logold = $input;
 
     # if it's not a scalar, attempt to warn and fix.
-    if (ref($input) ne "") {
-       &status("status: 'input' is not scalar (".ref($input).").");
-       if (ref($input) eq "ARRAY") {
+    my $ref = ref $input;
+    if (defined $ref and $ref ne "") {
+       &status("status: 'input' is not scalar ($ref).");
+
+       if ($ref eq "ARRAY") {
            foreach (@$input) {
                &WARN("status: '$_'.");
            }
@@ -211,12 +225,22 @@ sub status {
 
     # Something is using this w/ NULL.
     if (!defined $input or $input =~ /^\s*$/) {
-       $input = "Blank status call?";
+       $input = "ERROR: 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($_);
+       }
     }
-    $input =~ s/\n+$//;
-    $input =~ s/\002|037//g;   # bold,video,underline => remove.
 
-    # pump up the stats (or loglinenum).
+    # pump up the stats.
     $statcount++;
 
     # fix style of output if process is child.
@@ -227,19 +251,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;
@@ -250,7 +276,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 {
@@ -264,12 +290,13 @@ 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.
+       # three uberstabs to Derek Moeller. I don't remember why but he
+       # deserved it :)
        my $printable = $input;
 
        if ($printable =~ s/^(<\/\S+>) //) {
@@ -314,26 +341,28 @@ sub status {
     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();
-           &compress($file{log});
+           &compress( $file{log} );
            &openLog();
        }
     } else {
-       $date = time();
+       $date   = $time;
     }
 
-    print LOG sprintf("%s %s\n", $date, $input);
+    printf LOG "%s %s\n", $date, $input;
 }
 
 sub openSQLDebug {
@@ -353,4 +382,12 @@ sub closeSQLDebug {
     &status("Closed SQL Debug file: $param{'SQLDebug'}");
 }
 
+sub SQLDebug {
+    return unless (&IsParam("SQLDebug"));
+
+    return unless (fileno SQLDEBUG);
+
+    print SQLDEBUG $_[0]."\n";
+}
+
 1;