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);
23 $param{VEBOSITY} ||= 1; # lame fix for preload
34 'black' => 30, 'on_black' => 40,
35 'red' => 31, 'on_red' => 41,
36 'green' => 32, 'on_green' => 42,
37 'yellow' => 33, 'on_yellow' => 43,
38 'blue' => 34, 'on_blue' => 44,
39 'magenta' => 35, 'on_magenta' => 45,
40 'cyan' => 36, 'on_cyan' => 46,
41 'white' => 37, 'on_white' => 47
44 use vars qw($b_black $_black $b_red $_red $b_green $_green
45 $b_yellow $_yellow $b_blue $_blue $b_magenta $_magenta
46 $b_cyan $_cyan $b_white $_white $_reset $_bold $ob $b);
48 $b_black = cl('bold black'); $_black = cl('black');
49 $b_red = cl('bold red'); $_red = cl('red');
50 $b_green = cl('bold green'); $_green = cl('green');
51 $b_yellow = cl('bold yellow'); $_yellow = cl('yellow');
52 $b_blue = cl('bold blue'); $_blue = cl('blue');
53 $b_magenta = cl('bold magenta'); $_magenta = cl('magenta');
54 $b_cyan = cl('bold cyan'); $_cyan = cl('cyan');
55 $b_white = cl('bold white'); $_white = cl('white');
56 $_reset = cl('reset'); $_bold = cl('bold');
57 $ob = cl('reset'); $b = cl('bold');
59 ############################################################################
60 # Implementation (attribute string form)
61 ############################################################################
63 # Return the escape code for a given set of color attributes.
65 my @codes = map { split } @_;
69 unless (defined $attributes{$_}) { die "Invalid attribute name $_" }
70 $attribute .= $attributes{$_} . ';';
73 ($attribute ne '') ? "\e[${attribute}m" : undef;
78 return unless (&IsParam("logfile"));
79 $file{log} = $param{'logfile'};
82 my $path = &getPath($file{log});
85 &ERROR("openLog: failed opening log to $file{log}; disabling.");
86 delete $param{'logfile'};
90 &status("openLog: making $path.");
91 last if (mkdir $path, 0755);
95 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
96 my ($day,$month,$year) = (localtime time())[3,4,5];
97 $logDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
98 $file{log} .= "-".$logDate;
101 if (open(LOG, ">>$file{log}")) {
102 &status("Opened logfile $file{log}.");
105 &status("Cannot open logfile ($file{log}); not logging: $!");
110 # lame fix for paramlogfile.
111 return unless (&IsParam("logfile"));
112 return unless (defined fileno LOG);
115 &status("Closed logfile ($file{log}).");
119 # Usage: &compress($file);
122 my @compress = ("/usr/bin/bzip2","/bin/gzip");
126 &WARN("compress: file ($file) does not exist.");
130 if ( -f "$file.gz" or -f "$file.bz2" ) {
131 &WARN("compress: file.(gz|bz2) already exists.");
135 foreach (@compress) {
136 next unless ( -x $_);
138 &status("Compressing '$file' with $_.");
139 system("$_ $file &");
145 &ERROR("no compress program found.");
153 return unless (&IsParam("DEBUG"));
155 &status("${b_green}!DEBUG!$ob $_[0]");
159 &status("${b_red}!ERROR!$ob $_[0]");
163 return unless (&IsParam("WARN"));
165 return if ($_[0] =~ /^PERL: Subroutine \S+ redefined at/);
167 &status("${b_yellow}!WARN!$ob $_[0]");
171 &status("${b_cyan}!FIXME!$ob $_[0]");
175 &status("${b_cyan}!TODO!$ob $_[0]");
179 if (!&IsParam("VERBOSITY")) {
181 } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
183 } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
192 # a way to hook onto status without looping.
193 # todo: find why $channels{undef} is created.
194 if (0 and $running and !$cache{statusSafe}) {
198 if ($input eq $logold) {
199 # allow perl flooding
200 $logrepeat++ unless ($input =~ /PERL: Use of uninitialized/);
202 # todo: prevent massive repetitive throttling.
203 if ($logrepeat >= 3) {
205 &status("LOG: repeat throttle.");
206 # we block it to ensure sequence of logging is intact.
207 # could go with $conn->schedule but that's evil :)
214 # if it's not a scalar, attempt to warn and fix.
215 my $ref = ref $input;
216 if (defined $ref and $ref ne "") {
217 &status("status: 'input' is not scalar ($ref).");
219 if ($ref eq "ARRAY") {
221 &WARN("status: '$_'.");
226 # Something is using this w/ NULL.
227 if (!defined $input or $input =~ /^\s*$/) {
228 $input = "ERROR: Blank status call? HELP HELP HELP";
233 s/\002|037//g; # bold,video,underline => remove.
237 if ($input =~ /\n/) {
238 foreach (split(/\n/, $input)) {
246 # fix style of output if process is child.
247 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
253 ### TODO: move this _after_ printing?
257 if ($logtime == $time) {
258 if ($logcount < 25) { # too high?
262 &status("LOG: Throttling."); # recursive?
265 } else { # $logtime != $time.
274 # Log differently for forked/non-forked output.
276 $status = "!$statcount! ".$input;
277 if ($statcount > 1000) {
278 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
279 print LOG "VERB: ".(&Time2String($time - $forkedtime))."\n";
283 $status = "[$statcount] ".$input;
286 if (&IsParam("backlog")) {
287 push(@backlog, $status); # append to end.
288 shift(@backlog) if (scalar @backlog > $param{'backlog'});
291 if (&IsParam("VERBOSITY")) {
293 printf $_red."!%6d!".$ob." ", $statcount;
295 printf $_green."[%6d]".$ob." ", $statcount;
298 # three uberstabs to Derek Moeller. I don't remember why but he
300 my $printable = $input;
302 if ($printable =~ s/^(<\/\S+>) //) {
303 # it's me saying something on a channel
305 print "$b_yellow$name $printable$ob\n";
306 } elsif ($printable =~ s/^(<\S+>) //) {
307 # public message on channel.
311 print "$b_red$name $printable$ob\n";
313 print "$b_cyan$name$ob $printable$ob\n";
316 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
318 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
319 } elsif ($printable =~ s/^(-\S+-) //) {
321 print "$_green$1 $printable$ob\n";
322 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
323 # message/private action from someone
324 print "$b_white$1$ob" if (defined $1);
325 print "$b_red$2 $printable$ob\n";
326 } elsif ($printable =~ s/^(>\S+<) //) {
327 # i'm messaging someone
328 print "$b_magenta$1 $printable$ob\n";
329 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
330 # something that should be SEEN
331 print "$b_green$1 $printable$ob\n";
333 print "$printable\n";
336 print "VERBOSITY IS OFF?\n";
339 # log the line into a file.
340 return unless (&IsParam("logfile"));
341 return unless (defined fileno LOG);
343 # remove control characters from logging.
345 s/\e\[[0-9;]+m//g; # escape codes.
346 s/[\cA-\c_]//g; # control chars.
348 $input = "FORK($$) ".$input if ($statcountfix);
351 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
352 $date = sprintf("%02d:%02d.%02d", (localtime $time)[2,1,0]);
354 my ($day,$month,$year) = (localtime $time)[3,4,5];
355 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
356 if (defined $logDate and $newlogDate != $logDate) {
358 &compress( $file{log} );
365 printf LOG "%s %s\n", $date, $input;
369 if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) {
370 &ERROR("Cannot open ($param{'SQLDebug'}): $!");
371 delete $param{'SQLDebug'};
375 &status("Opened SQL Debug file: $param{'SQLDebug'}");
382 &status("Closed SQL Debug file: $param{'SQLDebug'}");
386 return unless (&IsParam("SQLDebug"));
388 return unless (fileno SQLDEBUG);
390 print SQLDEBUG $_[0]."\n";