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);
13 use vars qw(@backlog);
14 use vars qw(%param %file);
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 if ($input eq $logold) {
193 # allow perl flooding
194 $logrepeat++ unless ($input =~ /PERL: Use of uninitialized/);
196 # todo: prevent massive repetitive throttling.
197 if ($logrepeat >= 3) {
199 &status("LOG: repeat throttle.");
206 # if it's not a scalar, attempt to warn and fix.
207 my $ref = ref $input;
208 if (defined $ref and $ref ne "") {
209 &status("status: 'input' is not scalar ($ref).");
211 if ($ref eq "ARRAY") {
213 &WARN("status: '$_'.");
218 # Something is using this w/ NULL.
219 if (!defined $input or $input =~ /^\s*$/) {
220 $input = "Blank status call? HELP HELP HELP";
225 s/\002|037//g; # bold,video,underline => remove.
229 if ($input =~ /\n/) {
230 foreach (split(/\n/, $input)) {
238 # fix style of output if process is child.
239 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
245 ### TODO: move this _after_ printing?
249 if ($logtime == $time) {
250 if ($logcount < 25) { # too high?
254 &status("LOG: Throttling."); # recursive?
257 } else { # $logtime != $time.
266 # Log differently for forked/non-forked output.
268 $status = "!$statcount! ".$input;
269 if ($statcount > 1000) {
270 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
271 print LOG "VERB: ".(&Time2String($time - $forkedtime))."\n";
275 $status = "[$statcount] ".$input;
278 if (&IsParam("backlog")) {
279 push(@backlog, $status); # append to end.
280 shift(@backlog) if (scalar @backlog > $param{'backlog'});
283 if (&IsParam("VERBOSITY")) {
285 printf $_red."!%5d!".$ob." ", $statcount;
287 printf $_green."[%5d]".$ob." ", $statcount;
290 # three uberstabs to Derek Moeller.
291 my $printable = $input;
293 if ($printable =~ s/^(<\/\S+>) //) {
294 # it's me saying something on a channel
296 print "$b_yellow$name $printable$ob\n";
297 } elsif ($printable =~ s/^(<\S+>) //) {
298 # public message on channel.
302 print "$b_red$name $printable$ob\n";
304 print "$b_cyan$name$ob $printable$ob\n";
307 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
309 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
310 } elsif ($printable =~ s/^(-\S+-) //) {
312 print "$_green$1 $printable$ob\n";
313 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
314 # message/private action from someone
315 print "$b_white$1$ob" if (defined $1);
316 print "$b_red$2 $printable$ob\n";
317 } elsif ($printable =~ s/^(>\S+<) //) {
318 # i'm messaging someone
319 print "$b_magenta$1 $printable$ob\n";
320 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
321 # something that should be SEEN
322 print "$b_green$1 $printable$ob\n";
324 print "$printable\n";
327 print "VERBOSITY IS OFF?\n";
330 # log the line into a file.
331 return unless (&IsParam("logfile"));
332 return unless (defined fileno LOG);
334 # remove control characters from logging.
336 s/\e\[[0-9;]+m//g; # escape codes.
337 s/[\cA-\c_]//g; # control chars.
339 $input = "FORK($$) ".$input if ($statcountfix);
342 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
343 $date = sprintf("%02d:%02d.%02d", (localtime $time)[2,1,0]);
345 my ($day,$month,$year) = (localtime $time)[3,4,5];
346 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
347 if (defined $logDate and $newlogDate != $logDate) {
349 &compress($file{log});
356 print LOG sprintf("%s %s\n", $date, $input);
360 if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) {
361 &ERROR("cannot open $param{'SQLDebug'}...");
362 delete $param{'SQLDebug'};
366 &status("Opened SQL Debug file: $param{'SQLDebug'}");
373 &status("Closed SQL Debug file: $param{'SQLDebug'}");