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;
117 my $logDir = $file{log} . sprintf('%04d', $year + 1900);
119 &status("openLog: making $logDir.");
120 mkdir $logDir, 0755 or &status("Cannot mkdir $logDir");;
125 if ( open( LOG, ">>$file{log}" ) ) {
126 binmode( LOG, ':encoding(UTF-8)' );
127 &status("Opened logfile $file{log}.");
131 &status("Cannot open logfile ($file{log}); not logging: $!");
137 # lame fix for paramlogfile.
138 return unless ( &IsParam('logfile') );
139 return unless ( defined fileno LOG );
142 &status("Closed logfile ($file{log}).");
146 # Usage: &processLog($file);
149 my @processLog = ( 'scripts/processlog', '/usr/bin/bzip2', '/bin/bzip2', '/bin/gzip' );
153 &WARN("processLog: file ($file) does not exist.");
157 if ( -f "$file.gz" or -f "$file.bz2" ) {
158 &WARN('processLog: file.(gz|bz2) already exists.');
162 foreach (@processLog) {
163 next unless ( -x $_ );
165 &status("Processing log '$file' with $_.");
166 system("$_ $file &");
172 &ERROR('no processLog program found.');
180 return unless ( &IsParam('DEBUG') );
182 &status("${b_green}!DEBUG!$ob $_[0]");
186 &status("${b_red}!ERROR!$ob $_[0]");
190 return unless ( &IsParam('WARN') );
192 return if ( $_[0] =~ /^PERL: Subroutine \S+ redefined at/ );
194 &status("${b_yellow}!WARN!$ob $_[0]");
198 &status("${b_cyan}!FIXME!$ob $_[0]");
202 &status("${b_cyan}!TODO!$ob $_[0]");
206 if ( !&IsParam('VERBOSITY') ) {
210 elsif ( $param{'VERBOSITY'} eq '1' and $_[1] <= 1 ) {
213 elsif ( $param{'VERBOSITY'} eq '2' and $_[1] <= 2 ) {
222 if ( $input =~ /PERL: Use of uninitialized/ ) {
227 if ( $input eq $logold ) {
234 # if only I had followed how sysklogd does it, heh. lame me. -xk
235 if ( $logrepeat >= 3 ) {
236 &status("LOG: last message repeated $logrepeat times");
240 # if it's not a scalar, attempt to warn and fix.
241 my $ref = ref $input;
242 if ( defined $ref and $ref ne '' ) {
243 &WARN("status: 'input' is not scalar ($ref).");
245 if ( $ref eq 'ARRAY' ) {
247 &WARN("status: '$_'.");
252 # Something is using this w/ NULL.
253 if ( !defined $input or $input =~ /^\s*$/ ) {
254 $input = 'ERROR: Blank status call? HELP HELP HELP';
259 s/\002|\037//g; # bold,video,underline => remove.
263 if ( $input =~ /\n/ ) {
264 foreach ( split /\n/, $input ) {
272 # fix style of output if process is child.
273 if ( defined $bot_pid and $$ != $bot_pid and !defined $statcountfix ) {
279 ### TODO: move this _after_ printing?
283 # hrm... what is this supposed to achieve? nothing I guess.
284 if ( $logtime == $time ) {
285 if ( $logcount < 25 ) { # too high?
290 &status('LOG: Throttling.');
294 else { # $logtime != $time.
303 # Log differently for forked/non-forked output.
305 $status = "!$statcount! " . $input;
306 if ( $statcount > 1000 ) {
307 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
308 print LOG 'VERB: ' . ( &Time2String( $time - $forkedtime ) ) . "\n";
313 $status = "[$statcount] " . $input;
316 if ( &IsParam('backlog') ) {
317 push( @backlog, $status ); # append to end.
318 shift(@backlog) if ( scalar @backlog > $param{'backlog'} );
321 if ( &IsParam('VERBOSITY') ) {
323 printf $_red. '!%6d!' . $ob . ' ', $statcount;
326 printf $_green. '[%6d]' . $ob . ' ', $statcount;
329 # three uberstabs to Derek Moeller. I don't remember why but he
331 my $printable = $input;
333 if ( $printable =~ s/^(<\/\S+>) // ) {
335 # it's me saying something on a channel
337 print "$b_yellow$name $printable$ob\n";
339 elsif ( $printable =~ s/^(<\S+>) // ) {
341 # public message on channel.
345 print "$b_red$name $printable$ob\n";
348 print "$b_cyan$name$ob $printable$ob\n";
352 elsif ( $printable =~ s/^\* (\S+)\/(\S+) // ) {
355 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
358 elsif ( $printable =~ s/^(-\S+-) // ) {
361 print "$_green$1 $printable$ob\n";
364 elsif ( $printable =~ s/^(\* )?(\[\S+\]) // ) {
366 # message/private action from someone
367 print "$b_white$1$ob" if ( defined $1 );
368 print "$b_red$2 $printable$ob\n";
371 elsif ( $printable =~ s/^(>\S+<) // ) {
373 # i'm messaging someone
374 print "$b_magenta$1 $printable$ob\n";
377 elsif ( $printable =~ s/^(enter:|update:|forget:) // ) {
379 # something that should be SEEN
380 print "$b_green$1 $printable$ob\n";
384 print "$printable\n";
390 #print "VERBOSITY IS OFF?\n";
393 # log the line into a file.
394 return unless ( &IsParam('logfile') );
395 return unless ( defined fileno LOG );
397 # remove control characters from logging to LOGFILE.
399 last if ( &IsParam('logColors') );
400 s/\e\[[0-9;]+m//g; # escape codes.
401 s/[\cA-\c_]//g; # control chars.
403 $input = "FORK($$) " . $input if ($statcountfix);
406 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
407 $date = sprintf( '%02d:%02d.%02d', ( gmtime $time )[ 2, 1, 0 ] );
409 my ( $day, $month, $year ) = ( gmtime $time )[ 3, 4, 5 ];
411 sprintf( '%04d/%02d%02d', $year + 1900, $month + 1, $day );
412 if ( defined $logDate and $newlogDate ne $logDate ) {
414 &processLog( $file{log} );
422 printf LOG "%s %s\n", $date, $input;
430 $str =~ /^WARN: Use of uninitialized value .* at (\S+) line (\d+)/ );
431 my ( $file, $line ) = ( $1, $2 );
432 if ( !open( IN, $file ) ) {
433 &status("WARN: cannot open $file: $!");
436 binmode( IN, ':encoding(UTF-8)' );
438 # TODO: better filename.
439 open( OUT, '>>debug.log' );
440 binmode( OUT, ':encoding(UTF-8)' );
441 print OUT "DEBUG: $str\n";
443 # note: cannot call external functions because SIG{} does not allow us to.
449 # bleh. this tries to duplicate status().
451 # TODO: rename to log_*someshit*
453 my $msg = "$file: $i:!$_";
454 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
455 print OUT "DEBUG: $msg\n";
459 if ( $i + 3 > $line && $i - 3 < $line ) {
460 my $msg = "$file: $i: $_";
461 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
462 print OUT "DEBUG: $msg\n";
471 if ( !open( SQLDEBUG, ">>$param{'SQLDebug'}" ) ) {
472 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
473 delete $param{'SQLDebug'};
476 binmode( SQLDEBUG, ':encoding(UTF-8)' );
478 &status("Opened SQL Debug file: $param{'SQLDebug'}");
485 &status("Closed SQL Debug file: $param{'SQLDebug'}");
489 return unless ( &IsParam('SQLDebug') );
491 return unless ( fileno SQLDEBUG );
493 print SQLDEBUG $_[0] . "\n";
498 # vim:ts=4:sw=4:expandtab:tw=80