1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package Echolot::Log;
# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: Log.pm,v 1.5 2003/01/14 07:36:56 weasel Exp $
#
=pod
=head1 Name
Echolot::Globals - echolot global variables
=head1 DESCRIPTION
=cut
use strict;
use Carp qw{};
my %LOGLEVELS = qw{
debug 7
info 6
notice 5
warn 4
warning 4
error 3
critical 2
alert 1
emergency 0
};
my $LOGLEVEL;
my $LOGFILE;
my $LOGFH;
my @monnames = qw{Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec};
sub header_log($$) {
my ($level, $msg) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my $time = sprintf("%s %02d %02d:%02d:%02d",
$monnames[$mon],
$mday,
$hour, $min, $sec);
my $logstring = $time.' '.
'['.uc($level).']'. ' '.
$msg."\n";
$logstring =~ s/\n/\n /mg;
return $logstring;
};
sub reopen() {
$LOGFH->close() if ($LOGFH->opened());
open($LOGFH, ">>".$LOGFILE) or
warn("Cannot open logfile $LOGFILE: $!");
};
sub init() {
$LOGFILE = Echolot::Config::get()->{'logfile'};
$LOGLEVEL = Echolot::Config::get()->{'loglevel'};
$LOGFH = new IO::Handle;
die ("Logfile not defined") unless defined ($LOGFILE);
die ("Loglevel not defined") unless defined ($LOGLEVEL);
die ("Loglevel $LOGLEVEL unkown") unless defined ($LOGLEVELS{$LOGLEVEL});
$LOGLEVEL = $LOGLEVELS{$LOGLEVEL};
reopen();
};
sub log_message($$) {
my ($level, $msg) = @_;
die("Loglevel $level unkown.") unless defined $LOGLEVELS{$level};
return if $LOGLEVELS{$level} > $LOGLEVEL;
$msg = header_log($level, $msg);
print $LOGFH $msg;
$LOGFH->flush();
};
sub debug($) {
log_message('debug', $_[0]);
};
sub info($) {
log_message('info', $_[0]);
};
sub notice($) {
log_message('notice', $_[0]);
};
sub warn($) {
log_message('warn', $_[0]);
};
sub warning($) {
log_message('warning', $_[0]);
};
sub error($) {
log_message('error', $_[0]);
};
sub critical($) {
log_message('critical', $_[0]);
};
sub alert($) {
log_message('alert', $_[0]);
};
sub emergency($) {
log_message('emergency', $_[0]);
};
sub logdie($) {
my ($msg) = @_;
critical($msg);
die($msg);
};
sub cluck($) {
my ($msg) = @_;
my $longmess = Carp::longmess();
$longmess =~ s/^/ /mg;
$msg .= "\n".$longmess;
warning($msg);
};
sub confess($) {
my ($msg) = @_;
my $longmess = Carp::longmess();
$longmess =~ s/^/ /mg;
$msg .= "\n".$longmess;
error($msg);
die($msg);
};
1;
# vim: set ts=4 shiftwidth=4:
|