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'};
77 my $path = &getPath($file{log});
80 &ERROR("openLog: failed opening log to $file{log}; disabling.");
81 delete $param{'logfile'};
85 &status("openLog: making $path.");
86 last if (mkdir $path, 0755);
90 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
91 my ($day,$month,$year) = (localtime(time()))[3,4,5];
92 $logDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
93 $file{log} .= "-".$logDate;
96 if (open(LOG, ">>$file{log}")) {
97 &status("Opened logfile $file{log}.");
101 &status("cannot open logfile $file{log}; disabling.");
107 # lame fix for paramlogfile.
108 return unless (&IsParam("logfile"));
109 return unless ($loggingstatus);
112 &status("Closed logfile ($file{log}).");
117 # Usage: &compress($file);
120 my @compress = ("/usr/bin/bzip2","/bin/gzip");
124 # ironically this does not get logged :)
125 &WARN("compress: file ($file) does not exist.");
129 if (-f "$file.gz" or -f "$file.bz2") {
130 &WARN("compress: file.(gz|bz2) already exists.");
134 foreach (@compress) {
135 next unless ( -x $_);
137 &status("Compressing '$file' with $_.");
138 system("$_ $file &");
144 &ERROR("no compress program found.");
152 return unless (&IsParam("DEBUG"));
154 &status("${b_green}!DEBUG!$ob $_[0]");
158 &status("${b_red}!ERROR!$ob $_[0]");
162 return unless (&IsParam("WARN"));
164 &status("${b_yellow}!WARN!$ob $_[0]");
168 &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
172 &status("${b_cyan}!TODO!$ob $_[0]");
176 if (!&IsParam("VERBOSITY")) {
178 } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
180 } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
189 # return if input is null'ish.
190 return '' if ($input =~ /^\s*$/);
192 $input =~ s/\002|037//g; # bold,video,underline => remove.
194 # pump up the stats (or loglinenum).
197 # fix style of output if process is child.
198 if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
204 ### TODO: move this _after_ printing?
207 if ($logtime != $time) {
209 } elsif ($logtime == $time) {
210 if ($logcount < 25) { # too high?
214 &status("LOG: Throttling."); # recursive?
223 # Log differently for forked/non-forked output.
225 $status = "!$statcount! ".$input;
226 if ($statcount > 1000) {
227 print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
231 $status = "[$statcount] ".$input;
234 if (&IsParam("backlog")) {
235 push(@backlog, $status); # append to end.
236 shift(@backlog) if (scalar @backlog > $param{'backlog'});
239 if (&IsParam("VERBOSITY")) {
241 printf $_red."!%5d!".$ob." ", $statcount;
243 printf $_green."[%5d]".$ob." ", $statcount;
246 # three uberstabs to Derek Moeller.
247 my $printable = $input;
249 if ($printable =~ s/^(<\/\S+>) //) {
250 # it's me saying something on a channel
252 print "$b_yellow$name $printable$ob\n";
253 } elsif ($printable =~ s/^(<\S+>) //) {
254 # public message on channel.
258 print "$b_red$name $printable$ob\n";
260 print "$b_cyan$name$ob $printable$ob\n";
263 } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
265 print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
266 } elsif ($printable =~ s/^(-\S+-) //) {
268 print "$_green$1 $printable$ob\n";
269 } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
270 # message/private action from someone
271 print "$b_white$1$ob" if (defined $1);
272 print "$b_red$2 $printable$ob\n";
273 } elsif ($printable =~ s/^(>\S+<) //) {
274 # i'm messaging someone
275 print "$b_magenta$1 $printable$ob\n";
276 } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
277 # something that should be SEEN
278 print "$b_green$1 $printable$ob\n";
280 print "$printable\n";
283 print "VERBOSITY IS OFF?\n";
286 # log the line into a file.
287 return unless (&IsParam("logfile"));
288 return unless ($loggingstatus);
290 # remove control characters from logging.
291 $input =~ s/\e\[[0-9;]+m//g;
292 $input =~ s/[\cA-\c_]//g;
293 $input = "FORK($$) ".$input if ($statcountfix);
296 if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
297 $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
299 my ($day,$month,$year) = (localtime(time()))[3,4,5];
300 my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
301 if (defined $logDate and $newlogDate != $logDate) {
303 &compress($file{log});
310 print LOG sprintf("%s %s\n", $date, $input);