diff options
| author | Paul Buetow <paul@buetow.org> | 2011-03-07 20:47:27 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2011-03-07 20:47:27 +0000 |
| commit | 85d5f0d033a8c63e0ea0d7197296ed94f6b7cd7c (patch) | |
| tree | 78b6f80c5d4527f724ea3ab46d02f9598e071246 /Logger.pm | |
| parent | 894a911a52a7689c5e19b3b5f9e70a793b625a55 (diff) | |
Diffstat (limited to 'Logger.pm')
| -rw-r--r-- | Logger.pm | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Logger.pm b/Logger.pm new file mode 100644 index 0000000..f6c16e8 --- /dev/null +++ b/Logger.pm @@ -0,0 +1,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; |
