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') );
173 my (undef,undef,$line,$subroutine,undef) = caller(0);
175 &status("${b_green}!DEBUG!$ob ".$subroutine.'['.$line."] $_[0]");
179 my (undef,undef,$line,$subroutine,undef) = caller(0);
181 &status("${b_red}!ERROR!$ob ".$subroutine.'['.$line."] $_[0]");
185 return unless ( &IsParam('WARN') );
187 return if ( $_[0] =~ /^PERL: Subroutine \S+ redefined at/ );
189 my ($package,$filename,$line,$subroutine,undef) = caller(0);
191 &status("${b_yellow}!WARN!$ob ".$subroutine.'['.$line."] $_[0]");
195 my ($package,$filename,$line,$subroutine,undef) = caller(0);
197 &status("${b_cyan}!FIXME!$ob ".$subroutine.'['.$line."] $_[0]");
201 my ($package,$filename,$line,$subroutine,undef) = caller(0);
203 &status("${b_cyan}!TODO!$ob ".$subroutine.'['.$line."] $_[0]");
207 if ( !&IsParam('VERBOSITY') ) {
211 elsif ( $param{'VERBOSITY'} eq '1' and $_[1] <= 1 ) {
214 elsif ( $param{'VERBOSITY'} eq '2' and $_[1] <= 2 ) {
223 if ( $input =~ /PERL: Use of uninitialized/ ) {
228 if ( $input eq $logold ) {
235 # if only I had followed how sysklogd does it, heh. lame me. -xk
236 if ( $logrepeat >= 3 ) {
237 &status("LOG: last message repeated $logrepeat times");
241 # if it's not a scalar, attempt to warn and fix.
242 my $ref = ref $input;
243 if ( defined $ref and $ref ne '' ) {
244 &WARN("status: 'input' is not scalar ($ref).");
246 if ( $ref eq 'ARRAY' ) {
248 &WARN("status: '$_'.");
253 # Something is using this w/ NULL.
254 if ( !defined $input or $input =~ /^\s*$/ ) {
255 $input = 'ERROR: Blank status call? HELP HELP HELP';
260 s/\002|\037//g; # bold,video,underline => remove.
264 if ( $input =~ /\n/ ) {
265 foreach ( split /\n/, $input ) {
273 # fix style of output if process is child.
274 if ( defined $bot_pid and $$ != $bot_pid and !defined $statcountfix ) {
280 ### TODO: move this _after_ printing?
284 # hrm... what is this supposed to achieve? nothing I guess.
285 if ( $logtime == $time ) {
286 if ( $logcount < 25 ) { # too high?
291 &status('LOG: Throttling.');
295 else { # $logtime != $time.
304 # Log differently for forked/non-forked output.
306 $status = "!$statcount! " . $input;
307 if ( $statcount > 1000 ) {
308 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
309 print LOG 'VERB: ' . ( &Time2String( $time - $forkedtime ) ) . "\n";
314 $status = "[$statcount] " . $input;
317 if ( &IsParam('backlog') ) {
318 push( @backlog, $status ); # append to end.
319 shift(@backlog) if ( scalar @backlog > $param{'backlog'} );
322 if ( &IsParam('VERBOSITY') ) {
324 printf $_red. '!%6d!' . $ob . ' ', $statcount;
327 printf $_green. '[%6d]' . $ob . ' ', $statcount;
330 # three uberstabs to Derek Moeller. I don't remember why but he
332 my $printable = $input;
334 if ( $printable =~ s/^(<\/\S+>) // ) {
336 # it's me saying something on a channel
338 print "$b_yellow$name $printable$ob\n";
340 elsif ( $printable =~ s/^(<\S+>) // ) {
342 # public message on channel.
346 print "$b_red$name $printable$ob\n";
349 print "$b_cyan$name$ob $printable$ob\n";
353 elsif ( $printable =~ s/^\* (\S+)\/(\S+) // ) {
356 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
359 elsif ( $printable =~ s/^(-\S+-) // ) {
362 print "$_green$1 $printable$ob\n";
365 elsif ( $printable =~ s/^(\* )?(\[\S+\]) // ) {
367 # message/private action from someone
368 print "$b_white$1$ob" if ( defined $1 );
369 print "$b_red$2 $printable$ob\n";
372 elsif ( $printable =~ s/^(>\S+<) // ) {
374 # i'm messaging someone
375 print "$b_magenta$1 $printable$ob\n";
378 elsif ( $printable =~ s/^(enter:|update:|forget:) // ) {
380 # something that should be SEEN
381 print "$b_green$1 $printable$ob\n";
385 print "$printable\n";
391 #print "VERBOSITY IS OFF?\n";
394 # log the line into a file.
395 return unless ( &IsParam('logfile') );
396 return unless ( defined fileno LOG );
398 # remove control characters from logging to LOGFILE.
400 last if ( &IsParam('logColors') );
401 s/\e\[[0-9;]+m//g; # escape codes.
402 s/[\cA-\c_]//g; # control chars.
404 $input = "FORK($$) " . $input if ($statcountfix);
407 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
408 $date = sprintf( '%02d:%02d.%02d', ( gmtime $time )[ 2, 1, 0 ] );
410 my ( $day, $month, $year ) = ( gmtime $time )[ 3, 4, 5 ];
412 sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
413 if ( defined $logDate and $newlogDate != $logDate ) {
415 &compress( $file{log} );
423 printf LOG "%s %s\n", $date, $input;
431 $str =~ /^WARN: Use of uninitialized value .* at (\S+) line (\d+)/ );
432 my ( $file, $line ) = ( $1, $2 );
433 if ( !open( IN, $file ) ) {
434 &status("WARN: cannot open $file: $!");
437 binmode( IN, ':encoding(UTF-8)' );
439 # TODO: better filename.
440 open( OUT, '>>debug.log' );
441 binmode( OUT, ':encoding(UTF-8)' );
442 print OUT "DEBUG: $str\n";
444 # note: cannot call external functions because SIG{} does not allow us to.
450 # bleh. this tries to duplicate status().
452 # TODO: rename to log_*someshit*
454 my $msg = "$file: $i:!$_";
455 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
456 print OUT "DEBUG: $msg\n";
460 if ( $i + 3 > $line && $i - 3 < $line ) {
461 my $msg = "$file: $i: $_";
462 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
463 print OUT "DEBUG: $msg\n";
472 if ( !open( SQLDEBUG, ">>$param{'SQLDebug'}" ) ) {
473 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
474 delete $param{'SQLDebug'};
477 binmode( SQLDEBUG, ':encoding(UTF-8)' );
479 &status("Opened SQL Debug file: $param{'SQLDebug'}");
486 &status("Closed SQL Debug file: $param{'SQLDebug'}");
490 return unless ( &IsParam('SQLDebug') );
492 return unless ( fileno SQLDEBUG );
494 print SQLDEBUG $_[0] . "\n";
499 # vim:ts=4:sw=4:expandtab:tw=80