summaryrefslogtreecommitdiff
path: root/Echolot/Log.pm
blob: 4c24aa0a7bbd389c438fd572a2b94b5e31e12b9f (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
package Echolot::Log;

# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: Log.pm,v 1.3 2003/01/14 06:32:22 weasel Exp $
#

=pod

=head1 Name

Echolot::Globals - echolot global variables

=head1 DESCRIPTION

=cut

use strict;
use Carp qw{};
use Log::Dispatch::File;
use Log::Dispatch;

my $LOG;

my @monnames = qw{Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec};
sub header_log(%) {
	my (%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($msg{'level'}).']'. ' '.
		$msg{'message'}."\n";
	$logstring =~ s/(?<=.)^/	/mg;
	return $logstring;
};

sub reopen() {
	$LOG->remove( 'file1' );
	$LOG->add( Log::Dispatch::File->new(
		name       => 'file1',
		min_level  => Echolot::Config::get()->{'loglevel'},
		filename   => Echolot::Config::get()->{'logfile'},
		mode       => 'append',
	));
};

sub init(%) {
	my (%args) = @_;

	$LOG = Log::Dispatch->new( callbacks => \&header_log );
	reopen();
};

sub debug($) {
	$LOG->debug(@_);
};
sub info($) {
	$LOG->info(@_);
};
sub notice($) {
	$LOG->notice(@_);
};
sub warn($) {
	$LOG->warning(@_);
};
sub warning($) {
	$LOG->warning(@_);
};
sub error($) {
	$LOG->error(@_);
};
sub critical($) {
	$LOG->critical(@_);
};
sub alert($) {
	$LOG->alert(@_);
};
sub emergency($) {
	$LOG->emergency(@_);
};

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: