blob: 41984d33feb5b080b8fb4f0d34300a4570bdd127 (
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
|
package PerlDaemon::Logger;
use Shell qw(mv);
use POSIX qw(strftime);
$| = 1;
sub new ($$) {
my ($class, $conf) = @_;
return bless { conf => $conf }, $class;
}
sub logmsg ($$) {
my ($self, $msg) = @_;
my $conf = $self->{conf};
my $logfile = $conf->{'daemon.logfile'};
my $logline = localtime()." (PID $$): $msg\n";
open my $fh, ">>$logfile" or die "Can't write logfile $logfile: $!\n";
print $fh $logline;
close $fh;
print $logline if $conf->{'daemon.daemonize'} ne 'yes';
return undef;
}
sub err ($$) {
my ($self, $msg) = @_;
$self->logmsg($msg);
die "$msg\n";
}
sub warn ($$) {
my ($self, $msg) = @_;
$self->logmsg("WARNING: $msg");
return undef;
}
sub rotatelog ($) {
my $self = shift;
my $conf = $self->{conf};
my $logfile = $conf->{'daemon.logfile'};
$self->logmsg('Rotating logfile');
my $timestr = strftime "%Y%m%d-%H%M%S", localtime();
mv($logfile, "$logfile.$timestr");
return undef;
}
1;
|