summaryrefslogtreecommitdiff
path: root/Logger.pm
blob: f6c16e82f1e1dd396522c507e57467b3d6c83180 (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
package 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'};

	open my $fh, ">>$logfile" or die "Can't write logfile $logfile: $!\n";
	print $fh localtime()." (PID $$): $msg\n";
	close $fh;
}

sub err ($$) {
	my ($self, $msg) = @_;
	$self->logmsg($msg);
	die "$msg\n";
}

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");	
}

1;