2 # logger.pl: logger functions!
4 # Version: v0.4 (20000923)
6 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 use vars qw($statcount $bot_pid $forkedtime $statcountfix $addressed);
12 use vars qw($logDate $logold $logcount $logtime $logrepeat $running);
13 use vars qw(@backlog);
14 use vars qw(%param %file %cache);
21 $param{VEBOSITY} ||= 1; # lame fix for preload
50 use vars qw($b_black $_black $b_red $_red $b_green $_green
51 $b_yellow $_yellow $b_blue $_blue $b_magenta $_magenta
52 $b_cyan $_cyan $b_white $_white $_reset $_bold $ob $b);
54 $b_black = cl('bold black');
55 $_black = cl('black');
56 $b_red = cl('bold red');
58 $b_green = cl('bold green');
59 $_green = cl('green');
60 $b_yellow = cl('bold yellow');
61 $_yellow = cl('yellow');
62 $b_blue = cl('bold blue');
64 $b_magenta = cl('bold magenta');
65 $_magenta = cl('magenta');
66 $b_cyan = cl('bold cyan');
68 $b_white = cl('bold white');
69 $_white = cl('white');
70 $_reset = cl('reset');
75 ############################################################################
76 # Implementation (attribute string form)
77 ############################################################################
79 # Return the escape code for a given set of color attributes.
81 my @codes = map { split } @_;
85 unless ( defined $attributes{$_} ) { die "Invalid attribute name $_" }
86 $attribute .= $attributes{$_} . ';';
89 ( $attribute ne '' ) ? "\e[${attribute}m" : undef;
94 return unless ( &IsParam('logfile') );
95 $file{log} = $param{'logfile'};
98 my $path = &getPath( $file{log} );
101 &ERROR("openLog: failed opening log to $file{log}; disabling.");
102 delete $param{'logfile'};
106 &status("openLog: making $path.");
107 last if ( mkdir $path, 0755 );
111 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
112 my ( $day, $month, $year ) = ( gmtime time() )[ 3, 4, 5 ];
113 $logDate = sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
114 $file{log} .= $logDate;
117 if ( open( LOG, ">>$file{log}" ) ) {
118 binmode( LOG, ':encoding(UTF-8)' );
119 &status("Opened logfile $file{log}.");
123 &status("Cannot open logfile ($file{log}); not logging: $!");
129 # lame fix for paramlogfile.
130 return unless ( &IsParam('logfile') );
131 return unless ( defined fileno LOG );
134 &status("Closed logfile ($file{log}).");
138 # Usage: &compress($file);
141 my @compress = ( '/usr/bin/bzip2', '/bin/bzip2', '/bin/gzip' );
145 &WARN("compress: file ($file) does not exist.");
149 if ( -f "$file.gz" or -f "$file.bz2" ) {
150 &WARN('compress: file.(gz|bz2) already exists.');
154 foreach (@compress) {
155 next unless ( -x $_ );
157 &status("Compressing '$file' with $_.");
158 system("$_ $file &");
164 &ERROR('no compress program found.');
172 return unless ( &IsParam('DEBUG') );
174 &status("${b_green}!DEBUG!$ob $_[0]");
178 &status("${b_red}!ERROR!$ob $_[0]");
182 return unless ( &IsParam('WARN') );
184 return if ( $_[0] =~ /^PERL: Subroutine \S+ redefined at/ );
186 &status("${b_yellow}!WARN!$ob $_[0]");
190 &status("${b_cyan}!FIXME!$ob $_[0]");
194 &status("${b_cyan}!TODO!$ob $_[0]");
198 if ( !&IsParam('VERBOSITY') ) {
202 elsif ( $param{'VERBOSITY'} eq '1' and $_[1] <= 1 ) {
205 elsif ( $param{'VERBOSITY'} eq '2' and $_[1] <= 2 ) {
214 if ( $input =~ /PERL: Use of uninitialized/ ) {
219 if ( $input eq $logold ) {
226 # if only I had followed how sysklogd does it, heh. lame me. -xk
227 if ( $logrepeat >= 3 ) {
228 &status("LOG: last message repeated $logrepeat times");
232 # if it's not a scalar, attempt to warn and fix.
233 my $ref = ref $input;
234 if ( defined $ref and $ref ne '' ) {
235 &WARN("status: 'input' is not scalar ($ref).");
237 if ( $ref eq 'ARRAY' ) {
239 &WARN("status: '$_'.");
244 # Something is using this w/ NULL.
245 if ( !defined $input or $input =~ /^\s*$/ ) {
246 $input = 'ERROR: Blank status call? HELP HELP HELP';
251 s/\002|\037//g; # bold,video,underline => remove.
255 if ( $input =~ /\n/ ) {
256 foreach ( split /\n/, $input ) {
264 # fix style of output if process is child.
265 if ( defined $bot_pid and $$ != $bot_pid and !defined $statcountfix ) {
271 ### TODO: move this _after_ printing?
275 # hrm... what is this supposed to achieve? nothing I guess.
276 if ( $logtime == $time ) {
277 if ( $logcount < 25 ) { # too high?
282 &status('LOG: Throttling.');
286 else { # $logtime != $time.
295 # Log differently for forked/non-forked output.
297 $status = "!$statcount! " . $input;
298 if ( $statcount > 1000 ) {
299 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
300 print LOG 'VERB: ' . ( &Time2String( $time - $forkedtime ) ) . "\n";
305 $status = "[$statcount] " . $input;
308 if ( &IsParam('backlog') ) {
309 push( @backlog, $status ); # append to end.
310 shift(@backlog) if ( scalar @backlog > $param{'backlog'} );
313 if ( &IsParam('VERBOSITY') ) {
315 printf $_red. '!%6d!' . $ob . ' ', $statcount;
318 printf $_green. '[%6d]' . $ob . ' ', $statcount;
321 # three uberstabs to Derek Moeller. I don't remember why but he
323 my $printable = $input;
325 if ( $printable =~ s/^(<\/\S+>) // ) {
327 # it's me saying something on a channel
329 print "$b_yellow$name $printable$ob\n";
331 elsif ( $printable =~ s/^(<\S+>) // ) {
333 # public message on channel.
337 print "$b_red$name $printable$ob\n";
340 print "$b_cyan$name$ob $printable$ob\n";
344 elsif ( $printable =~ s/^\* (\S+)\/(\S+) // ) {
347 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
350 elsif ( $printable =~ s/^(-\S+-) // ) {
353 print "$_green$1 $printable$ob\n";
356 elsif ( $printable =~ s/^(\* )?(\[\S+\]) // ) {
358 # message/private action from someone
359 print "$b_white$1$ob" if ( defined $1 );
360 print "$b_red$2 $printable$ob\n";
363 elsif ( $printable =~ s/^(>\S+<) // ) {
365 # i'm messaging someone
366 print "$b_magenta$1 $printable$ob\n";
369 elsif ( $printable =~ s/^(enter:|update:|forget:) // ) {
371 # something that should be SEEN
372 print "$b_green$1 $printable$ob\n";
376 print "$printable\n";
382 #print "VERBOSITY IS OFF?\n";
385 # log the line into a file.
386 return unless ( &IsParam('logfile') );
387 return unless ( defined fileno LOG );
389 # remove control characters from logging to LOGFILE.
391 last if ( &IsParam('logColors') );
392 s/\e\[[0-9;]+m//g; # escape codes.
393 s/[\cA-\c_]//g; # control chars.
395 $input = "FORK($$) " . $input if ($statcountfix);
398 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
399 $date = sprintf( '%02d:%02d.%02d', ( gmtime $time )[ 2, 1, 0 ] );
401 my ( $day, $month, $year ) = ( gmtime $time )[ 3, 4, 5 ];
403 sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
404 if ( defined $logDate and $newlogDate != $logDate ) {
406 &compress( $file{log} );
414 printf LOG "%s %s\n", $date, $input;
422 $str =~ /^WARN: Use of uninitialized value .* at (\S+) line (\d+)/ );
423 my ( $file, $line ) = ( $1, $2 );
424 if ( !open( IN, $file ) ) {
425 &status("WARN: cannot open $file: $!");
428 binmode( IN, ':encoding(UTF-8)' );
430 # TODO: better filename.
431 open( OUT, '>>debug.log' );
432 binmode( OUT, ':encoding(UTF-8)' );
433 print OUT "DEBUG: $str\n";
435 # note: cannot call external functions because SIG{} does not allow us to.
441 # bleh. this tries to duplicate status().
443 # TODO: rename to log_*someshit*
445 my $msg = "$file: $i:!$_";
446 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
447 print OUT "DEBUG: $msg\n";
451 if ( $i + 3 > $line && $i - 3 < $line ) {
452 my $msg = "$file: $i: $_";
453 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
454 print OUT "DEBUG: $msg\n";
463 if ( !open( SQLDEBUG, ">>$param{'SQLDebug'}" ) ) {
464 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
465 delete $param{'SQLDebug'};
468 binmode( SQLDEBUG, ':encoding(UTF-8)' );
470 &status("Opened SQL Debug file: $param{'SQLDebug'}");
477 &status("Closed SQL Debug file: $param{'SQLDebug'}");
481 return unless ( &IsParam('SQLDebug') );
483 return unless ( fileno SQLDEBUG );
485 print SQLDEBUG $_[0] . "\n";
490 # vim:ts=4:sw=4:expandtab:tw=80