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') );
175 my (undef,undef,$line,$subroutine,undef) = caller(1);
177 &status("${b_green}!DEBUG!$ob ".$subroutine.'['.$line."] ".quotemeta($_[0]));
181 my (undef,undef,$line,$subroutine,undef) = caller(1);
183 &status("${b_red}!ERROR!$ob ".$subroutine.'['.$line."] ".quotemeta($_[0]));
187 return unless ( &IsParam('WARN') );
189 return if ( $_[0] =~ /^PERL: Subroutine \S+ redefined at/ );
191 my ($package,$filename,$line,$subroutine,undef) = caller(1);
193 &status("${b_yellow}!WARN!$ob ".$subroutine.'['.$line."] ".quotemeta($_[0]));
197 my ($package,$filename,$line,$subroutine,undef) = caller(1);
199 &status("${b_cyan}!FIXME!$ob ".$subroutine.'['.$line."] ".quotemeta($_[0]));
203 my ($package,$filename,$line,$subroutine,undef) = caller(1);
205 &status("${b_cyan}!TODO!$ob ".$subroutine.'['.$line."] ".quotemeta($_[0]));
209 if ( !&IsParam('VERBOSITY') ) {
213 elsif ( $param{'VERBOSITY'} eq '1' and $_[1] <= 1 ) {
216 elsif ( $param{'VERBOSITY'} eq '2' and $_[1] <= 2 ) {
225 if ( $input =~ /PERL: Use of uninitialized/ ) {
230 if ( $input eq $logold ) {
237 # if only I had followed how sysklogd does it, heh. lame me. -xk
238 if ( $logrepeat >= 3 ) {
239 &status("LOG: last message repeated $logrepeat times");
243 # if it's not a scalar, attempt to warn and fix.
244 my $ref = ref $input;
245 if ( defined $ref and $ref ne '' ) {
246 &WARN("status: 'input' is not scalar ($ref).");
248 if ( $ref eq 'ARRAY' ) {
250 &WARN("status: '$_'.");
255 # Something is using this w/ NULL.
256 if ( !defined $input or $input =~ /^\s*$/ ) {
257 $input = 'ERROR: Blank status call? HELP HELP HELP';
262 s/\002|\037//g; # bold,video,underline => remove.
266 if ( $input =~ /\n/ ) {
267 foreach ( split /\n/, $input ) {
275 # fix style of output if process is child.
276 if ( defined $bot_pid and $$ != $bot_pid and !defined $statcountfix ) {
282 ### TODO: move this _after_ printing?
286 # hrm... what is this supposed to achieve? nothing I guess.
287 if ( $logtime == $time ) {
288 if ( $logcount < 25 ) { # too high?
293 &status('LOG: Throttling.');
297 else { # $logtime != $time.
306 # Log differently for forked/non-forked output.
308 $status = "!$statcount! " . $input;
309 if ( $statcount > 1000 ) {
310 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
311 print LOG 'VERB: ' . ( &Time2String( $time - $forkedtime ) ) . "\n";
316 $status = "[$statcount] " . $input;
319 if ( &IsParam('backlog') ) {
320 push( @backlog, $status ); # append to end.
321 shift(@backlog) if ( scalar @backlog > $param{'backlog'} );
324 if ( &IsParam('VERBOSITY') ) {
326 printf $_red. '!%6d!' . $ob . ' ', $statcount;
329 printf $_green. '[%6d]' . $ob . ' ', $statcount;
332 # three uberstabs to Derek Moeller. I don't remember why but he
334 my $printable = $input;
336 if ( $printable =~ s/^(<\/\S+>) // ) {
338 # it's me saying something on a channel
340 print "$b_yellow$name $printable$ob\n";
342 elsif ( $printable =~ s/^(<\S+>) // ) {
344 # public message on channel.
348 print "$b_red$name $printable$ob\n";
351 print "$b_cyan$name$ob $printable$ob\n";
355 elsif ( $printable =~ s/^\* (\S+)\/(\S+) // ) {
358 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
361 elsif ( $printable =~ s/^(-\S+-) // ) {
364 print "$_green$1 $printable$ob\n";
367 elsif ( $printable =~ s/^(\* )?(\[\S+\]) // ) {
369 # message/private action from someone
370 print "$b_white$1$ob" if ( defined $1 );
371 print "$b_red$2 $printable$ob\n";
374 elsif ( $printable =~ s/^(>\S+<) // ) {
376 # i'm messaging someone
377 print "$b_magenta$1 $printable$ob\n";
380 elsif ( $printable =~ s/^(enter:|update:|forget:) // ) {
382 # something that should be SEEN
383 print "$b_green$1 $printable$ob\n";
387 print "$printable\n";
393 #print "VERBOSITY IS OFF?\n";
396 # log the line into a file.
397 return unless ( &IsParam('logfile') );
398 return unless ( defined fileno LOG );
400 # remove control characters from logging to LOGFILE.
402 last if ( &IsParam('logColors') );
403 s/\e\[[0-9;]+m//g; # escape codes.
404 s/[\cA-\c_]//g; # control chars.
406 $input = "FORK($$) " . $input if ($statcountfix);
409 if ( &IsParam('logType') and $param{'logType'} =~ /DAILY/i ) {
410 $date = sprintf( '%02d:%02d.%02d', ( gmtime $time )[ 2, 1, 0 ] );
412 my ( $day, $month, $year ) = ( gmtime $time )[ 3, 4, 5 ];
414 sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
415 if ( defined $logDate and $newlogDate != $logDate ) {
417 &compress( $file{log} );
425 printf LOG "%s %s\n", $date, $input;
433 $str =~ /^WARN: Use of uninitialized value .* at (\S+) line (\d+)/ );
434 my ( $file, $line ) = ( $1, $2 );
435 if ( !open( IN, $file ) ) {
436 &status("WARN: cannot open $file: $!");
439 binmode( IN, ':encoding(UTF-8)' );
441 # TODO: better filename.
442 open( OUT, '>>debug.log' );
443 binmode( OUT, ':encoding(UTF-8)' );
444 print OUT "DEBUG: $str\n";
446 # note: cannot call external functions because SIG{} does not allow us to.
452 # bleh. this tries to duplicate status().
454 # TODO: rename to log_*someshit*
456 my $msg = "$file: $i:!$_";
457 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
458 print OUT "DEBUG: $msg\n";
462 if ( $i + 3 > $line && $i - 3 < $line ) {
463 my $msg = "$file: $i: $_";
464 printf "%s[%6d]%s %s\n", $_green, $statcount, $ob, $msg;
465 print OUT "DEBUG: $msg\n";
474 if ( !open( SQLDEBUG, ">>$param{'SQLDebug'}" ) ) {
475 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
476 delete $param{'SQLDebug'};
479 binmode( SQLDEBUG, ':encoding(UTF-8)' );
481 &status("Opened SQL Debug file: $param{'SQLDebug'}");
488 &status("Closed SQL Debug file: $param{'SQLDebug'}");
492 return unless ( &IsParam('SQLDebug') );
494 return unless ( fileno SQLDEBUG );
496 print SQLDEBUG $_[0] . "\n";
501 # vim:ts=4:sw=4:expandtab:tw=80