summaryrefslogtreecommitdiff
path: root/Echolot/Log.pm
blob: 15b5b8011ad2781a99b6f0ff0ba59ffcfaf37814 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package Echolot::Log;

#
# $Id$
#
# This file is part of Echolot - a Pinger for anonymous remailers.
#
# Copyright (c) 2002, 2003, 2004 Peter Palfrader <peter@palfrader.org>
#
# This program is free software. you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

=pod

=head1 Name

Echolot::Globals - echolot global variables

=head1 DESCRIPTION

=cut

use strict;
use Carp qw{};
#use Time::HiRes qw( gettimeofday );

my %LOGLEVELS = qw{
	trace		8
	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 Apr May Jun Jul Aug Sep Oct Nov Dec};
sub header_log($$) {
	my ($level, $msg) = @_;

	#my ($secs, $msecs) = gettimeofday();
	#my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( $secs );
	#my $time = sprintf("%s %02d %02d:%02d:%02d.%06d",
	#	$monnames[$mon],
	#	$mday,
	#	$hour, $min, $sec, $msecs);
	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 $prefix = $time.' ['.uc($level).'] ';
	my $logstring = $msg."\n";
	my $first = 0;
	$logstring =~ s/^/ $prefix . ($first++ ? '  ' : '' ) /emg;
	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 trace($) {
	log_message('trace', $_[0]);
};
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: