]> jfr.im git - irc/SurrealServices/srsv.git/blob - branches/erry-devel/SrSv/Log.pm
initial commit of erry's Insp work.
[irc/SurrealServices/srsv.git] / branches / erry-devel / SrSv / Log.pm
1 # This file is part of SurrealServices.
2 #
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 package SrSv::Log;
17
18 use strict;
19 use IO::Handle;
20
21 use SrSv::Debug;
22 use SrSv::Timer qw(add_timer);
23 use SrSv::Time;
24 use SrSv::Process::InParent qw(write_log open_log close_log rotate_logs close_all_logs);
25
26 use SrSv::Text::Codes qw( strip_codes );
27
28 use SrSv::Conf2Consts qw(main);
29
30 use Exporter 'import';
31 BEGIN {
32 my %constants = (
33 LOG_DEBUG => 0,
34 LOG_INFO => 1,
35 LOG_WARNING => 2, # A bad thing might happen
36 LOG_ERROR => 3, # A bad thing happened
37 LOG_CRITICAL => 4, # One module is going down
38 LOG_FATAL => 5, # One thread is going down
39 LOG_PANIC => 6, # The entire server is going down
40
41 LOG_OPEN => 1,
42 LOG_CLOSE => 2,
43 LOG_WRITE => 3,
44 LOG_ROTATE => 4,
45 );
46
47 require constant; import constant (\%constants);
48 our @EXPORT = ( qw( wlog write_log open_log close_log ), keys(%constants) );
49 our @EXPORT_OK = ( qw ( rotate_logs close_all_logs ) );
50 our %EXPORT_TAGS = (
51 levels => [keys(%constants)],
52 all => [@EXPORT, @EXPORT_OK],
53 );
54 }
55
56 our $path = './logs';
57 our @levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'FATAL', 'PANIC');
58
59 open_log('diag', 'services.log');
60 open_log('netdump', 'netdump.log') if main::NETDUMP();
61
62 sub wlog($$$) {
63 my ($service, $level, $text) = @_;
64
65 my $prefix;
66 $prefix = "\002\00304" if($level > LOG_INFO);
67 $prefix .= $levels[$level];
68 ircd::privmsg($main::rsnick, main_conf_diag, "$prefix\: ($service) $text");
69 write_log('diag', '<'.$main::rsnick.'>', "$prefix\: ($service) $text");
70 }
71
72 my %log_handles;
73 my %file_handles;
74
75 sub write_log($$@) {
76 my ($handle, $prefix, @payloads) = @_;
77 unless (defined($log_handles{lc $handle})) {
78 ircd::debug_nolog("undefined log-handle $handle, aborting write()") if main::DEBUG();
79 return undef;
80 }
81 foreach (@payloads) {
82 $_ = strip_codes($_);
83 }
84 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime();
85 my $time = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
86 my $payload = $time.$prefix.' '.join("\n".$time.$prefix.' ', @payloads);
87 print {$log_handles{lc $handle}} "$payload\n";
88 }
89
90 sub open_log($$) {
91 my ($handle, $filename) = @_;
92 if (defined($log_handles{lc $handle})) {
93 ircd::debug_nolog("duplicate log-handle $handle, aborting open()");
94 return undef;
95 }
96 my ($year, $month, undef, $mday) = gmt_date();
97 my $filename2 = $filename.'-'.sprintf('%04d-%02d-%02d', $year, $month, $mday);
98
99 open $log_handles{lc $handle}, '>>', $path.'/'.$filename2;
100 $file_handles{lc $handle} = { BASENAME => $filename, FILENAME => $filename2 };
101 $log_handles{lc $handle}->autoflush(1);
102 }
103
104 sub close_log($) {
105 my ($handle) = @_;
106 unless (defined($log_handles{lc $handle})) {
107 ircd::debug_nolog("undefined log-handle $handle, aborting close()");
108 return undef;
109 }
110 close $log_handles{lc $handle};
111 delete($log_handles{lc $handle});
112 }
113
114 sub rotate_logs() {
115 foreach my $handle (keys(%file_handles)) {
116 close $log_handles{$handle};
117 my ($year, $month, undef, $mday) = gmt_date();
118 $file_handles{lc $handle}{FILENAME} = $file_handles{lc $handle}{BASENAME}.'-'.sprintf('%04d-%02d-%02d', $year, $month, $mday);
119 open $log_handles{$handle}, '>>', $path.'/'.$file_handles{lc $handle}{FILENAME};
120 }
121
122 add_timer('', get_nextday_time()-time(), __PACKAGE__, 'SrSv::Log::rotate_logs');
123 }
124
125 sub close_all_logs() {
126 foreach my $handle (keys(%file_handles)) {
127 close $log_handles{$handle};
128 $file_handles{lc $handle} = undef;
129 }
130 }
131
132 # set a timer to rotate logs on day-change
133 add_timer('', get_nextday_time()-time(), __PACKAGE__, 'SrSv::Log::rotate_logs');
134
135 1;