1 # This file is part of SurrealServices.
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.
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.
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
22 use SrSv
::Timer
qw(add_timer);
24 use SrSv
::Process
::InParent
qw(write_log open_log close_log rotate_logs close_all_logs);
26 use SrSv
::Text
::Codes
qw( strip_codes );
28 use SrSv
::Conf2Consts
qw(main);
30 use Exporter
'import';
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
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
) );
51 levels
=> [keys(%constants)],
52 all
=> [@EXPORT, @EXPORT_OK],
57 our @levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'FATAL', 'PANIC');
59 open_log
('diag', 'services.log');
60 open_log
('netdump', 'netdump.log') if main
::NETDUMP
();
63 my ($service, $level, $text) = @_;
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");
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
();
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";
91 my ($handle, $filename) = @_;
92 if (defined($log_handles{lc $handle})) {
93 ircd
::debug_nolog
("duplicate log-handle $handle, aborting open()");
96 my ($year, $month, undef, $mday) = gmt_date
();
97 my $filename2 = $filename.'-'.sprintf('%04d-%02d-%02d', $year, $month, $mday);
99 open $log_handles{lc $handle}, '>>', $path.'/'.$filename2;
100 $file_handles{lc $handle} = { BASENAME
=> $filename, FILENAME
=> $filename2 };
101 $log_handles{lc $handle}->autoflush(1);
106 unless (defined($log_handles{lc $handle})) {
107 ircd
::debug_nolog
("undefined log-handle $handle, aborting close()");
110 close $log_handles{lc $handle};
111 delete($log_handles{lc $handle});
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
};
122 add_timer
('', get_nextday_time
()-time(), __PACKAGE__
, 'SrSv::Log::rotate_logs');
125 sub close_all_logs
() {
126 foreach my $handle (keys(%file_handles)) {
127 close $log_handles{$handle};
128 $file_handles{lc $handle} = undef;
132 # set a timer to rotate logs on day-change
133 add_timer
('', get_nextday_time
()-time(), __PACKAGE__
, 'SrSv::Log::rotate_logs');