2 # logger.pl: logger functions!
4 # Version: v0.3 (20000731)
6 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 use vars qw($logDate $loggingstatus $statcount $bot_pid $forkedtime
12 $statcountfix $addressed $logcount $logtime);
13 use vars qw(@backlog);
14 use vars qw(%param %file);
30 'black' => 30, 'on_black' => 40,
31 'red' => 31, 'on_red' => 41,
32 'green' => 32, 'on_green' => 42,
33 'yellow' => 33, 'on_yellow' => 43,
34 'blue' => 34, 'on_blue' => 44,
35 'magenta' => 35, 'on_magenta' => 45,
36 'cyan' => 36, 'on_cyan' => 46,
37 'white' => 37, 'on_white' => 47
40 use vars qw($b_black $_black $b_red $_red $b_green $_green
41 $b_yellow $_yellow $b_blue $_blue $b_magenta $_magenta
42 $b_cyan $_cyan $b_white $_white $_reset $_bold $ob $b);
44 $b_black = cl('bold black'); $_black = cl('black');
45 $b_red = cl('bold red'); $_red = cl('red');
46 $b_green = cl('bold green'); $_green = cl('green');
47 $b_yellow = cl('bold yellow'); $_yellow = cl('yellow');
48 $b_blue = cl('bold blue'); $_blue = cl('blue');
49 $b_magenta = cl('bold magenta'); $_magenta = cl('magenta');
50 $b_cyan = cl('bold cyan'); $_cyan = cl('cyan');
51 $b_white = cl('bold white'); $_white = cl('white');
52 $_reset = cl('reset'); $_bold = cl('bold');
53 $ob = cl('reset'); $b = cl('bold');
55 ############################################################################
56 # Implementation (attribute string form)
57 ############################################################################
59 # Return the escape code for a given set of color attributes.
61 my @codes = map { split } @_;
65 unless (defined $attributes{$_}) { die "Invalid attribute name $_" }
66 $attribute .= $attributes{$_} . ';';
69 ($attribute ne '') ? "\e[${attribute}m" : undef;
74 return unless (&IsParam("logfile"));
75 $file{log} = $param{'logfile'};
78 my $path = &getPath($file{log});
81 &ERROR("openLog: failed opening log to $file{log}; disabling.");
82 delete $param{'logfile'};
86 &status("openLog: making $path.");
87 last if (mkdir $path, 0755);
91 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
92 my ($day,$month,$year) = (localtime(time()))[3,4,5];
93 $logDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
94 $file{log} .= "-".$logDate;
97 if (open(LOG, ">>$file{log}")) {
98 &status("Opened logfile $file{log}.");
102 &status("cannot open logfile $file{log}; disabling.");
108 # lame fix for paramlogfile.
109 return unless (&IsParam("logfile"));
110 return unless ($loggingstatus);
113 &status("Closed logfile ($file{log}).");
118 # Usage: &compress($file);
121 my @compress = ("/usr/bin/bzip2","/bin/gzip");
125 # ironically this does not get logged :)
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] (SHOULD NOT HAPPEN?)");
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 # return if input is null'ish.
193 return '' if ($input =~ /^\s*$/);
195 $input =~ s/\002|037//g; # bold,video,underline => remove.
197 # pump up the stats (or loglinenum).
200 # fix style of output if process is child.
201 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
207 ### TODO: move this _after_ printing?
210 if ($logtime != $time) {
212 } elsif ($logtime == $time) {
213 if ($logcount < 25) { # too high?
217 &status("LOG: Throttling."); # recursive?
226 # Log differently for forked/non-forked output.
228 $status = "!$statcount! ".$input;
229 if ($statcount > 1000) {
230 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
231 print LOG "VERB: ".(&Time2String(time() - $forkedtime))."\n";
235 $status = "[$statcount] ".$input;
238 if (&IsParam("backlog")) {
239 push(@backlog, $status); # append to end.
240 shift(@backlog) if (scalar @backlog > $param{'backlog'});
243 if (&IsParam("VERBOSITY")) {
245 printf $_red."!%5d!".$ob." ", $statcount;
247 printf $_green."[%5d]".$ob." ", $statcount;
250 # three uberstabs to Derek Moeller.
251 my $printable = $input;
253 if ($printable =~ s/^(<\/\S+>) //) {
254 # it's me saying something on a channel
256 print "$b_yellow$name $printable$ob\n";
257 } elsif ($printable =~ s/^(<\S+>) //) {
258 # public message on channel.
262 print "$b_red$name $printable$ob\n";
264 print "$b_cyan$name$ob $printable$ob\n";
267 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
269 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
270 } elsif ($printable =~ s/^(-\S+-) //) {
272 print "$_green$1 $printable$ob\n";
273 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
274 # message/private action from someone
275 print "$b_white$1$ob" if (defined $1);
276 print "$b_red$2 $printable$ob\n";
277 } elsif ($printable =~ s/^(>\S+<) //) {
278 # i'm messaging someone
279 print "$b_magenta$1 $printable$ob\n";
280 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
281 # something that should be SEEN
282 print "$b_green$1 $printable$ob\n";
284 print "$printable\n";
287 print "VERBOSITY IS OFF?\n";
290 # log the line into a file.
291 return unless (&IsParam("logfile"));
292 return unless ($loggingstatus);
294 # remove control characters from logging.
295 $input =~ s/\e\[[0-9;]+m//g;
296 $input =~ s/[\cA-\c_]//g;
297 $input = "FORK($$) ".$input if ($statcountfix);
300 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
301 $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
303 my ($day,$month,$year) = (localtime(time()))[3,4,5];
304 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
305 if (defined $logDate and $newlogDate != $logDate) {
307 &compress($file{log});
314 print LOG sprintf("%s %s\n", $date, $input);
318 if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) {
319 &ERROR("cannot open $param{'SQLDebug'}...");
320 delete $param{'SQLDebug'};
324 &status("Opened SQL Debug file: $param{'SQLDebug'}");
331 &status("Closed SQL Debug file: $param{'SQLDebug'}");