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
12 $statcountfix $addressed $logcount $logtime);
13 use vars qw(@backlog);
14 use vars qw(%param %file);
29 'black' => 30, 'on_black' => 40,
30 'red' => 31, 'on_red' => 41,
31 'green' => 32, 'on_green' => 42,
32 'yellow' => 33, 'on_yellow' => 43,
33 'blue' => 34, 'on_blue' => 44,
34 'magenta' => 35, 'on_magenta' => 45,
35 'cyan' => 36, 'on_cyan' => 46,
36 'white' => 37, 'on_white' => 47
39 use vars qw($b_black $_black $b_red $_red $b_green $_green
40 $b_yellow $_yellow $b_blue $_blue $b_magenta $_magenta
41 $b_cyan $_cyan $b_white $_white $_reset $_bold $ob $b);
43 $b_black = cl('bold black'); $_black = cl('black');
44 $b_red = cl('bold red'); $_red = cl('red');
45 $b_green = cl('bold green'); $_green = cl('green');
46 $b_yellow = cl('bold yellow'); $_yellow = cl('yellow');
47 $b_blue = cl('bold blue'); $_blue = cl('blue');
48 $b_magenta = cl('bold magenta'); $_magenta = cl('magenta');
49 $b_cyan = cl('bold cyan'); $_cyan = cl('cyan');
50 $b_white = cl('bold white'); $_white = cl('white');
51 $_reset = cl('reset'); $_bold = cl('bold');
52 $ob = cl('reset'); $b = cl('bold');
54 ############################################################################
55 # Implementation (attribute string form)
56 ############################################################################
58 # Return the escape code for a given set of color attributes.
60 my @codes = map { split } @_;
64 unless (defined $attributes{$_}) { die "Invalid attribute name $_" }
65 $attribute .= $attributes{$_} . ';';
68 ($attribute ne '') ? "\e[${attribute}m" : undef;
73 return unless (&IsParam("logfile"));
74 $file{log} = $param{'logfile'};
76 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
77 my ($day,$month,$year) = (localtime(time()))[3,4,5];
78 $logDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
79 $file{log} .= "-".$logDate;
82 if (open(LOG, ">>$file{log}")) {
83 &status("Opened logfile $file{log}.");
87 &status("cannot open logfile $file{log}; disabling.");
93 # lame fix for paramlogfile.
94 return unless (&IsParam("logfile"));
95 return unless ($loggingstatus);
98 &status("Closed logfile ($file{log}).");
103 # Usage: &compress($file);
106 my @compress = ("/usr/bin/bzip2","/bin/gzip");
110 # ironically this does not get logged :)
111 &WARN("compress: file ($file) does not exist.");
115 if (-f "$file.gz" or -f "$file.bz2") {
116 &WARN("compress: file.(gz|bz2) already exists.");
120 foreach (@compress) {
121 next unless ( -x $_);
123 &status("Compressing '$file' with $_.");
124 system("$_ $file &");
130 &ERROR("no compress program found.");
138 return unless (&IsParam("DEBUG"));
140 &status("${b_green}!DEBUG!$ob $_[0]");
144 &status("${b_red}!ERROR!$ob $_[0]");
148 return unless (&IsParam("WARN"));
150 &status("${b_yellow}!WARN!$ob $_[0]");
154 &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
158 &status("${b_cyan}!TODO!$ob $_[0]");
162 if (!&IsParam("VERBOSITY")) {
164 } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
166 } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
175 # return if input is null'ish.
176 return '' if ($input =~ /^\s*$/);
178 $input =~ s/\002|037//g; # bold,video,underline => remove.
180 # pump up the stats (or loglinenum).
183 # fix style of output if process is child.
184 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
190 ### TODO: move this _after_ printing?
193 if ($logtime != $time) {
195 } elsif ($logtime == $time) {
196 if ($logcount < 25) { # too high?
200 &status("LOG: Throttling."); # recursive?
209 # Log differently for forked/non-forked output.
211 $status = "!$statcount! ".$input;
212 if ($statcount > 1000) {
213 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
217 $status = "[$statcount] ".$input;
220 if (&IsParam("backlog")) {
221 push(@backlog, $status); # append to end.
222 shift(@backlog) if (scalar @backlog > $param{'backlog'});
225 if (&IsParam("VERBOSITY")) {
227 printf $_red."!%5d!".$ob." ", $statcount;
229 printf $_green."[%5d]".$ob." ", $statcount;
232 # three uberstabs to Derek Moeller.
233 my $printable = $input;
235 if ($printable =~ s/^(<\/\S+>) //) {
236 # it's me saying something on a channel
238 print "$b_yellow$name $printable$ob\n";
239 } elsif ($printable =~ s/^(<\S+>) //) {
240 # public message on channel.
244 print "$b_red$name $printable$ob\n";
246 print "$b_cyan$name$ob $printable$ob\n";
249 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
251 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
252 } elsif ($printable =~ s/^(-\S+-) //) {
254 print "$_green$1 $printable$ob\n";
255 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
256 # message/private action from someone
257 print "$b_white$1$ob" if (defined $1);
258 print "$b_red$2 $printable$ob\n";
259 } elsif ($printable =~ s/^(>\S+<) //) {
260 # i'm messaging someone
261 print "$b_magenta$1 $printable$ob\n";
262 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
263 # something that should be SEEN
264 print "$b_green$1 $printable$ob\n";
266 print "$printable\n";
269 print "VERBOSITY IS OFF?\n";
272 # log the line into a file.
273 return unless (&IsParam("logfile"));
274 return unless ($loggingstatus);
276 # remove control characters from logging.
277 $input =~ s/\e\[[0-9;]+m//g;
278 $input =~ s/[\cA-\c_]//g;
279 $input = "FORK($$) ".$input if ($statcountfix);
282 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
283 $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
285 my ($day,$month,$year) = (localtime(time()))[3,4,5];
286 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
287 if (defined $logDate and $newlogDate != $logDate) {
289 &compress($file{log});
296 print LOG sprintf("%s %s\n", $date, $input);