summaryrefslogtreecommitdiff
path: root/PerlDaemon/Logger.pm
diff options
context:
space:
mode:
Diffstat (limited to 'PerlDaemon/Logger.pm')
-rw-r--r--PerlDaemon/Logger.pm40
1 files changed, 40 insertions, 0 deletions
diff --git a/PerlDaemon/Logger.pm b/PerlDaemon/Logger.pm
new file mode 100644
index 0000000..fccc274
--- /dev/null
+++ b/PerlDaemon/Logger.pm
@@ -0,0 +1,40 @@
+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'};
+
+ 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;