2 # logger.pl: logger functions!
4 # Version: v0.4 (20000923)
6 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
12 use vars qw($statcount $bot_pid $forkedtime $statcountfix $addressed);
13 use vars qw($logDate $logold $logcount $logtime $logrepeat $running);
14 use vars qw(@backlog);
15 use vars qw(%param %file %cache);
22 $param{VEBOSITY} ||= 1; # lame fix for preload
51 use vars qw($b_black $_black $b_red $_red $b_green $_green
52 $b_yellow $_yellow $b_blue $_blue $b_magenta $_magenta
53 $b_cyan $_cyan $b_white $_white $_reset $_bold $ob $b);
55 $b_black = cl('bold black');
56 $_black = cl('black');
57 $b_red = cl('bold red');
59 $b_green = cl('bold green');
60 $_green = cl('green');
61 $b_yellow = cl('bold yellow');
62 $_yellow = cl('yellow');
63 $b_blue = cl('bold blue');
65 $b_magenta = cl('bold magenta');
66 $_magenta = cl('magenta');
67 $b_cyan = cl('bold cyan');
69 $b_white = cl('bold white');
70 $_white = cl('white');
71 $_reset = cl('reset');
76 ############################################################################
77 # Implementation (attribute string form)
78 ############################################################################
80 # Return the escape code for a given set of color attributes.
82 my @codes = map { split } @_;
86 unless ( defined $attributes{$_} ) { die "Invalid attribute name $_" }
87 $attribute .= $attributes{$_} . ';';
90 ( $attribute ne '' ) ? "\e[${attribute}m" : undef;
95 binmode( STDOUT, ':encoding(UTF-8)' );
96 return unless ( &IsParam('logfile') );
97 $file{log} = $param{'logfile'};
100 my $path = &getPath( $file{log} );
101 while ( !-d $path ) {
103 &ERROR("openLog: failed opening log to $file{log}; disabling.");
104 delete $param{'logfile'};
108 &status("openLog: making $path.");
109 last if ( mkdir $path, 0755 );
113 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
114 my ( $day, $month, $year ) = ( gmtime time() )[ 3, 4, 5 ];
115 $logDate = sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
116 $file{log} .= $logDate;
119 if ( open( LOG, ">>$file{log}" ) ) {
120 binmode( LOG, ':encoding(UTF-8)' );
121 &status("Opened logfile $file{log}.");
125 &status("Cannot open logfile ($file{log}); not logging: $!");
131 # lame fix for paramlogfile.
132 return unless ( &IsParam('logfile') );
133 return unless ( defined fileno LOG );
136 &status("Closed logfile ($file{log}).");
140 # Usage: &compress($file);
143 my @compress = ( '/usr/bin/bzip2', '/bin/bzip2', '/bin/gzip' );
147 &WARN("compress: file ($file) does not exist.");
151 if ( -f "$file.gz" or -f "$file.bz2" ) {
152 &WARN('compress: file.(gz|bz2) already exists.');
156 foreach (@compress) {
157 next unless ( -x $_ );
159 &status("Compressing '$file' with $_.");
160 system("$_ $file &");
166 &ERROR('no compress program found.');
174 return unless ( &IsParam('DEBUG') );
176 &status("${b_green}!DEBUG!$ob $_[0]");
180 &status("${b_red}!ERROR!$ob $_[0]");
184 return unless ( &IsParam('WARN') );
186 return if ( $_[0] =~ /^PERL: Subroutine \S+ redefined at/ );
188 &status("${b_yellow}!WARN!$ob $_[0]");
192 &status("${b_cyan}!FIXME!$ob $_[0]");
196 &status("${b_cyan}!TODO!$ob $_[0]");
200 if ( !&IsParam('VERBOSITY') ) {
204 elsif ( $param{'VERBOSITY'} eq '1' and $_[1] <= 1 ) {
207 elsif ( $param{'VERBOSITY'} eq '2' and $_[1] <= 2 ) {
216 if ( $input =~ /PERL: Use of uninitialized/ ) {
221 if ( $input eq $logold ) {
228 # if only I had followed how sysklogd does it, heh. lame me. -xk
229 if ( $logrepeat >= 3 ) {
230 &status("LOG: last message repeated $logrepeat times");
234 # if it's not a scalar, attempt to warn and fix.
235 my $ref = ref $input;
236 if ( defined $ref and $ref ne '' ) {
237 &WARN("status: 'input' is not scalar ($ref).");
239 if ( $ref eq 'ARRAY' ) {
241 &WARN("status: '$_'.");
246 # Something is using this w/ NULL.
247 if ( !defined $input or $input =~ /^\s*$/ ) {
248 $input = 'ERROR: Blank status call? HELP HELP HELP';
253 s/\002|\037//g; # bold,video,underline => remove.
257 if ( $input =~ /\n/ ) {
258 foreach ( split /\n/, $input ) {
266 # fix style of output if process is child.
267 if ( defined $bot_pid and $$ != $bot_pid and !defined $statcountfix ) {
273 ### TODO: move this _after_ printing?
277 # hrm... what is this supposed to achieve? nothing I guess.
278 if ( $logtime == $time ) {
279 if ( $logcount < 25 ) { # too high?
284 &status('LOG: Throttling.');
288 else { # $logtime != $time.
297 # Log differently for forked/non-forked output.
299 $status = "!$statcount! " . $input;
300 if ( $statcount > 1000 ) {
301 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
302 print LOG 'VERB: ' . ( &Time2String( $time - $forkedtime ) ) . "\n";
307 $status = "[$statcount] " . $input;
310 if ( &IsParam('backlog') ) {
311 push( @backlog, $status ); # append to end.
312 shift(@backlog) if ( scalar @backlog > $param{'backlog'} );
315 if ( &IsParam('VERBOSITY') ) {
317 printf $_red. '!%6d!' . $ob . ' ', $statcount;
320 printf $_green. '[%6d]' . $ob . ' ', $statcount;
323 # three uberstabs to Derek Moeller. I don't remember why but he
325 my $printable = $input;
327 if ( $printable =~ s/^(<\/\S+>) // ) {
329 # it's me saying something on a channel
331 print "$b_yellow$name $printable$ob\n";
333 elsif ( $printable =~ s/^(<\S+>) // ) {
335 # public message on channel.
339 print "$b_red$name $printable$ob\n";
342 print "$b_cyan$name$ob $printable$ob\n";
346 elsif ( $printable =~ s/^\* (\S+)\/(\S+) // ) {
349 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
352 elsif ( $printable =~ s/^(-\S+-) // ) {
355 print "$_green$1 $printable$ob\n";
358 elsif ( $printable =~ s/^(\* )?(\[\S+\]) // ) {
360 # message/private action from someone
361 print "$b_white$1$ob" if ( defined $1 );
362 print "$b_red$2 $printable$ob\n";
365 elsif ( $printable =~ s/^(>\S+<) // ) {
367 # i'm messaging someone
368 print "$b_magenta$1 $printable$ob\n";
371 elsif ( $printable =~ s/^(enter:|update:|forget:) // ) {
373 # something that should be SEEN
374 print "$b_green$1 $printable$ob\n";
378 print "$printable\n";
384 #print "VERBOSITY IS OFF?\n";
387 # log the line into a file.
388 return unless ( &IsParam('logfile') );
389 return unless ( defined fileno LOG );
391 # remove control characters from logging to LOGFILE.
393 last if ( &IsParam('logColors') );
394 s/\e\[[0-9;]+m//g; # escape codes.
395 s/[\cA-\c_]//g; # control chars.
397 $input = "FORK($$) " . $input if ($statcountfix);
400 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
401 $date = sprintf( '%02d:%02d.%02d', ( gmtime $time )[ 2, 1, 0 ] );
403 my ( $day, $month, $year ) = ( gmtime $time )[ 3, 4, 5 ];
405 sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
406 if ( defined $logDate and $newlogDate != $logDate ) {
408 &compress( $file{log} );
416 printf LOG "%s %s\n", $date, $input;
424 $str =~ /^WARN: Use of uninitialized value .* at (\S+) line (\d+)/ );
425 my ( $file, $line ) = ( $1, $2 );
426 if ( !open( IN, $file ) ) {
427 &status("WARN: cannot open $file: $!");
430 binmode( IN, ':encoding(UTF-8)' );
432 # TODO: better filename.
433 open( OUT, '>>debug.log' );
434 binmode( OUT, ':encoding(UTF-8)' );
435 print OUT "DEBUG: $str\n";
437 # note: cannot call external functions because SIG{} does not allow us to.
443 # bleh. this tries to duplicate status().
445 # TODO: rename to log_*someshit*
447 my $msg = "$file: $i:!$_";
448 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
449 print OUT "DEBUG: $msg\n";
453 if ( $i + 3 > $line && $i - 3 < $line ) {
454 my $msg = "$file: $i: $_";
455 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
456 print OUT "DEBUG: $msg\n";
465 if ( !open( SQLDEBUG, ">>$param{'SQLDebug'}" ) ) {
466 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
467 delete $param{'SQLDebug'};
470 binmode( SQLDEBUG, ':encoding(UTF-8)' );
472 &status("Opened SQL Debug file: $param{'SQLDebug'}");
479 &status("Closed SQL Debug file: $param{'SQLDebug'}");
483 return unless ( &IsParam('SQLDebug') );
485 return unless ( fileno SQLDEBUG );
487 print SQLDEBUG $_[0] . "\n";
492 # vim:ts=4:sw=4:expandtab:tw=80