]> git.donarmstrong.com Git - infobot.git/blob - src/logger.pl
was opening sql debug file for read, not write. typo
[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 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     return if ($_[0] =~ /^PERL: Subroutine \S+ redefined at/);
166
167     &status("${b_yellow}!WARN!$ob $_[0]");
168 }
169
170 sub FIXME {
171     &status("${b_cyan}!FIXME!$ob $_[0] (SHOULD NOT HAPPEN?)");
172 }
173
174 sub TODO {
175     &status("${b_cyan}!TODO!$ob $_[0]");
176 }
177
178 sub VERB {
179     if (!&IsParam("VERBOSITY")) {
180         # NOTHING.
181     } elsif ($param{'VERBOSITY'} eq "1" and $_[1] <= 1) {
182         &status($_[0]);
183     } elsif ($param{'VERBOSITY'} eq "2" and $_[1] <= 2) {
184         &status($_[0]);
185     }
186 }
187
188 sub status {
189     my($input) = @_;
190     my $status;
191
192     # return if input is null'ish.
193     return '' if ($input =~ /^\s*$/);
194     $input =~ s/\n+$//;
195     $input =~ s/\002|037//g;    # bold,video,underline => remove.
196
197     # pump up the stats (or loglinenum).
198     $statcount++;
199
200     # fix style of output if process is child.
201     if (defined $bot_pid and $$ != $bot_pid and !defined $statcountfix) {
202         $statcount      = 1;
203         $statcountfix   = 1;
204     }
205
206     ### LOG THROTTLING.
207     ### TODO: move this _after_ printing?
208     my $time = time();
209     my $reset = 0;
210     if ($logtime != $time) {
211         $reset++;
212     } elsif ($logtime == $time) {
213         if ($logcount < 25) {           # too high?
214             $logcount++;
215         } else {
216             sleep 1;
217             &status("LOG: Throttling.");        # recursive?
218             $reset++;
219         }
220     }
221     if ($reset) {
222         $logtime        = $time;
223         $logcount       = 0;
224     }
225
226     # Log differently for forked/non-forked output.
227     if ($statcountfix) {
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";
232             exit 0;
233         }
234     } else {
235         $status = "[$statcount] ".$input;
236     }
237
238     if (&IsParam("backlog")) {
239         push(@backlog, $status);        # append to end.
240         shift(@backlog) if (scalar @backlog > $param{'backlog'});
241     }
242
243     if (&IsParam("VERBOSITY")) {
244         if ($statcountfix) {
245             printf $_red."!%5d!".$ob." ", $statcount;
246         } else {
247             printf $_green."[%5d]".$ob." ", $statcount;
248         }
249
250         # three uberstabs to Derek Moeller.
251         my $printable = $input;
252
253         if ($printable =~ s/^(<\/\S+>) //) {
254             # it's me saying something on a channel
255             my $name = $1;
256             print "$b_yellow$name $printable$ob\n";
257         } elsif ($printable =~ s/^(<\S+>) //) {
258             # public message on channel.
259             my $name = $1;
260
261             if ($addressed) {
262                 print "$b_red$name $printable$ob\n";
263             } else {
264                 print "$b_cyan$name$ob $printable$ob\n";
265             }
266
267         } elsif ($printable =~ s/^\* (\S+)\/(\S+) //) {
268             # public action.
269             print "$b_white*$ob $b_cyan$1$ob/$b_blue$2$ob $printable\n";
270         } elsif ($printable =~ s/^(-\S+-) //) {
271             # notice
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";
283         } else {
284             print "$printable\n";
285         }
286     } else {
287         print "VERBOSITY IS OFF?\n";
288     }
289
290     # log the line into a file.
291     return unless (&IsParam("logfile"));
292     return unless ($loggingstatus);
293
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);
298
299     my $date;
300     if (&IsParam("logType") and $param{'logType'} =~ /DAILY/i) {
301         $date = sprintf("%02d:%02d.%02d", (localtime(time()))[2,1,0]);
302
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) {
306             &closeLog();
307             &compress($file{log});
308             &openLog();
309         }
310     } else {
311         $date = time();
312     }
313
314     print LOG sprintf("%s %s\n", $date, $input);
315 }
316
317 sub openSQLDebug {
318     if (!open(SQLDEBUG, ">>$param{'SQLDebug'}")) {
319         &ERROR("cannot open $param{'SQLDebug'}...");
320         delete $param{'SQLDebug'};
321         return 0;
322     }
323
324     &status("Opened SQL Debug file: $param{'SQLDebug'}");
325     return 1;
326 }
327
328 sub closeSQLDebug {
329     close SQLDEBUG;
330
331     &status("Closed SQL Debug file: $param{'SQLDebug'}");
332 }
333
334 1;