]> git.donarmstrong.com Git - infobot.git/blob - src/logger.pl
stupid typo (carelessness) on my behalf
[infobot.git] / src / logger.pl
1 #
2 # logger.pl: logger functions!
3 #    Author: dms
4 #   Version: v0.3 (20000731)
5 #  FVersion: 19991205
6 #      NOTE: Based on code by Kevin Lenzo & Patrick Cole  (c) 1997
7 #
8
9 if (&IsParam("useStrict")) { use strict; }
10
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);
15
16 require 5.001;
17
18 $logtime        = time();
19 $logcount       = 0;
20
21 my %attributes = (
22         'clear'      => 0,
23         'reset'      => 0,
24         'bold'       => 1,
25         'underline'  => 4,
26         'underscore' => 4,
27         'blink'      => 5,
28         'reverse'    => 7,
29         'concealed'  => 8,
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
38 );
39
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);
43
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');
54
55 ############################################################################
56 # Implementation (attribute string form)
57 ############################################################################
58
59 # Return the escape code for a given set of color attributes.
60 sub cl {
61     my @codes = map { split } @_;
62     my $attribute = '';
63     foreach (@codes) {
64         $_ = lc $_;
65         unless (defined $attributes{$_}) { die "Invalid attribute name $_" }
66         $attribute .= $attributes{$_} . ';';
67     }
68     chop $attribute;
69     ($attribute ne '') ? "\e[${attribute}m" : undef;
70 }
71
72 # logging support.
73 sub openLog {
74     return unless (&IsParam("logfile"));
75     $file{log} = $param{'logfile'};
76
77     my $error = 0;
78     my $path = &getPath($file{log});
79     while (! -d $path) {
80         if ($error) {
81             &ERROR("openLog: failed opening log to $file{log}; disabling.");
82             delete $param{'logfile'};
83             return;
84         }
85
86         &status("openLog: making $path.");
87         last if (mkdir $path, 0755);
88         $error++;
89     }
90
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;
95     }
96
97     if (open(LOG, ">>$file{log}")) {
98         &status("Opened logfile $file{log}.");
99         LOG->autoflush(1);
100         $loggingstatus = 1;
101     } else {
102         &status("cannot open logfile $file{log}; disabling.");
103         $loggingstatus = 0;
104     }
105 }
106
107 sub closeLog {
108     # lame fix for paramlogfile.
109     return unless (&IsParam("logfile"));
110     return unless ($loggingstatus);
111
112     $loggingstatus = 0;
113     &status("Closed logfile ($file{log}).");
114     close LOG;
115 }
116
117 #####
118 # Usage: &compress($file);
119 sub compress {
120     my ($file) = @_;
121     my @compress = ("/usr/bin/bzip2","/bin/gzip");
122     my $okay = 0;
123
124     if (! -f $file) {
125         # ironically this does not get logged :)
126         &WARN("compress: file ($file) does not exist.");
127         return 0;
128     }
129
130     if (-f "$file.gz" or -f "$file.bz2") {
131         &WARN("compress: file.(gz|bz2) already exists.");
132         return 0;
133     }
134
135     foreach (@compress) {
136         next unless ( -x $_);
137
138         &status("Compressing '$file' with $_.");
139         system("$_ $file &");
140         $okay++;
141         last;
142     }
143
144     if (!$okay) {
145         &ERROR("no compress program found.");
146         return 0;
147     }
148
149     return 1;
150 }
151
152 sub DEBUG {
153     return unless (&IsParam("DEBUG"));
154
155     &status("${b_green}!DEBUG!$ob $_[0]");
156 }
157
158 sub ERROR {
159     &status("${b_red}!ERROR!$ob $_[0]");
160 }
161
162 sub WARN {
163     return unless (&IsParam("WARN"));
164
165     &status("${b_yellow}!WARN!$ob $_[0]");
166 }
167
168 sub FIXME {
169     &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
170 }
171
172 sub TODO {
173     &status("${b_cyan}!TODO!$ob $_[0]");
174 }
175
176 sub VERB {
177     if (!&IsParam("VERBOSITY")) {
178         # NOTHING.
179     } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
180         &status($_[0]);
181     } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
182         &status($_[0]);
183     }
184 }
185
186 sub status {
187     my($input) = @_;
188     my $status;
189
190     # return if input is null'ish.
191     return '' if ($input =~ /^\s*$/);
192     $input =~ s/\n+$//;
193     $input =~ s/\002|037//g;    # bold,video,underline => remove.
194
195     # pump up the stats (or loglinenum).
196     $statcount++;
197
198     # fix style of output if process is child.
199     if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
200         $statcount      = 1;
201         $statcountfix   = 1;
202     }
203
204     ### LOG THROTTLING.
205     ### TODO: move this _after_ printing?
206     my $time = time();
207     my $reset = 0;
208     if ($logtime != $time) {
209         $reset++;
210     } elsif ($logtime == $time) {
211         if ($logcount < 25) {           # too high?
212             $logcount++;
213         } else {
214             sleep 1;
215             &status("LOG: Throttling.");        # recursive?
216             $reset++;
217         }
218     }
219     if ($reset) {
220         $logtime        = $time;
221         $logcount       = 0;
222     }
223
224     # Log differently for forked/non-forked output.
225     if ($statcountfix) {
226         $status = "!$statcount! ".$input;
227         if ($statcount > 1000) {
228             print LOG "ERROR: FORKED PROCESS RAN AWAY; KILLING.\n";
229             print LOG "VERB: ".(&Time2String(time() - $forkedtime))."\n";
230             exit 0;
231         }
232     } else {
233         $status = "[$statcount] ".$input;
234     }
235
236     if (&IsParam("backlog")) {
237         push(@backlog, $status);        # append to end.
238         shift(@backlog) if (scalar @backlog > $param{'backlog'});
239     }
240
241     if (&IsParam("VERBOSITY")) {
242         if ($statcountfix) {
243             printf $_red."!%5d!".$ob." ", $statcount;
244         } else {
245             printf $_green."[%5d]".$ob." ", $statcount;
246         }
247
248         # three uberstabs to Derek Moeller.
249         my $printable = $input;
250
251         if ($printable =~ s/^(<\/\S+>) //) {
252             # it's me saying something on a channel
253             my $name = $1;
254             print "$b_yellow$name $printable$ob\n";
255         } elsif ($printable =~ s/^(<\S+>) //) {
256             # public message on channel.
257             my $name = $1;
258
259             if ($addressed) {
260                 print "$b_red$name $printable$ob\n";
261             } else {
262                 print "$b_cyan$name$ob $printable$ob\n";
263             }
264
265         } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
266             # public action.
267             print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
268         } elsif ($printable =~ s/^(-\S+-) //) {
269             # notice
270             print "$_green$1 $printable$ob\n";
271         } elsif ($printable =~ s/^(\* )?(\[\S+\]) //) {
272             # message/private action from someone
273             print "$b_white$1$ob" if (defined $1);
274             print "$b_red$2 $printable$ob\n";
275         } elsif ($printable =~ s/^(>\S+<) //) {
276             # i'm messaging someone
277             print "$b_magenta$1 $printable$ob\n";
278         } elsif ($printable =~ s/^(enter:|update:|forget:) //) {
279             # something that should be SEEN
280             print "$b_green$1 $printable$ob\n";
281         } else {
282             print "$printable\n";
283         }
284     } else {
285         print "VERBOSITY IS OFF?\n";
286     }
287
288     # log the line into a file.
289     return unless (&IsParam("logfile"));
290     return unless ($loggingstatus);
291
292     # remove control characters from logging.
293     $input =~ s/\e\[[0-9;]+m//g;
294     $input =~ s/[\cA-\c_]//g;
295     $input = "FORK($$) ".$input if ($statcountfix);
296
297     my $date;
298     if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
299         $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
300
301         my ($day,$month,$year) = (localtime(time()))[3,4,5];
302         my $newlogDate = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
303         if (defined $logDate and $newlogDate != $logDate) {
304             &closeLog();
305             &compress($file{log});
306             &openLog();
307         }
308     } else {
309         $date = time();
310     }
311
312     print LOG sprintf("%s %s\n", $date, $input);
313 }
314
315 1;