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 # ironically this does not get logged :)
127 &WARN("compress: file ($file) does not exist.");
131 if (-f "$file.gz" or -f "$file.bz2") {
132 &WARN("compress: file.(gz|bz2) already exists.");
136 foreach (@compress) {
137 next unless ( -x $_);
139 &status("Compressing '$file' with $_.");
140 system("$_ $file &");
146 &ERROR("no compress program found.");
154 return unless (&IsParam("DEBUG"));
156 &status("${b_green}!DEBUG!$ob $_[0]");
160 &status("${b_red}!ERROR!$ob $_[0]");
164 return unless (&IsParam("WARN"));
166 return if ($_[0] =~ /^PERL: Subroutine \S+ redefined at/);
168 &status("${b_yellow}!WARN!$ob $_[0]");
172 &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
176 &status("${b_cyan}!TODO!$ob $_[0]");
180 if (!&IsParam("VERBOSITY")) {
182 } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
184 } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
193 if ($input eq $logold) {
196 if ($logrepeat >= 3) {
198 &status("LOG: repeat throttle.");
204 # if it's not a scalar, attempt to warn and fix.
205 if (ref($input) ne "") {
206 &status("status: 'input' is not scalar (".ref($input).").");
207 if (ref($input) eq "ARRAY") {
209 &WARN("status: '$_'.");
214 # Something is using this w/ NULL.
215 if (!defined $input or $input =~ /^\s*$/) {
216 $input = "Blank status call?";
219 $input =~ s/\002|037//g; # bold,video,underline => remove.
221 # pump up the stats (or loglinenum).
224 # fix style of output if process is child.
225 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
231 ### TODO: move this _after_ printing?
234 if ($logtime != $time) {
236 } elsif ($logtime == $time) {
237 if ($logcount < 25) { # too high?
241 &status("LOG: Throttling."); # recursive?
250 # Log differently for forked/non-forked output.
252 $status = "!$statcount! ".$input;
253 if ($statcount > 1000) {
254 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
255 print LOG "VERB: ".(&Time2String(time() - $forkedtime))."\n";
259 $status = "[$statcount] ".$input;
262 if (&IsParam("backlog")) {
263 push(@backlog, $status); # append to end.
264 shift(@backlog) if (scalar @backlog > $param{'backlog'});
267 if (&IsParam("VERBOSITY")) {
269 printf $_red."!%5d!".$ob." ", $statcount;
271 printf $_green."[%5d]".$ob." ", $statcount;
274 # three uberstabs to Derek Moeller.
275 my $printable = $input;
277 if ($printable =~ s/^(<\/\S+>) //) {
278 # it's me saying something on a channel
280 print "$b_yellow$name $printable$ob\n";
281 } elsif ($printable =~ s/^(<\S+>) //) {
282 # public message on channel.
286 print "$b_red$name $printable$ob\n";
288 print "$b_cyan$name$ob $printable$ob\n";
291 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
293 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
294 } elsif ($printable =~ s/^(-\S+-) //) {
296 print "$_green$1 $printable$ob\n";
297 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
298 # message/private action from someone
299 print "$b_white$1$ob" if (defined $1);
300 print "$b_red$2 $printable$ob\n";
301 } elsif ($printable =~ s/^(>\S+<) //) {
302 # i'm messaging someone
303 print "$b_magenta$1 $printable$ob\n";
304 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
305 # something that should be SEEN
306 print "$b_green$1 $printable$ob\n";
308 print "$printable\n";
311 print "VERBOSITY IS OFF?\n";
314 # log the line into a file.
315 return unless (&IsParam("logfile"));
316 return unless (defined fileno LOG);
318 # remove control characters from logging.
319 $input =~ s/\e\[[0-9;]+m//g;
320 $input =~ s/[\cA-\c_]//g;
321 $input = "FORK($$) ".$input if ($statcountfix);
324 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
325 $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
327 my ($day,$month,$year) = (localtime(time()))[3,4,5];
328 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
329 if (defined $logDate and $newlogDate != $logDate) {
331 &compress($file{log});
338 print LOG sprintf("%s %s\n", $date, $input);
342 if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) {
343 &ERROR("cannot open $param{'SQLDebug'}...");
344 delete $param{'SQLDebug'};
348 &status("Opened SQL Debug file: $param{'SQLDebug'}");
355 &status("Closed SQL Debug file: $param{'SQLDebug'}");