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 | |
| parent | 894a911a52a7689c5e19b3b5f9e70a793b625a55 (diff) | |
| -rw-r--r-- | DaemonLogic.pm | 17 | ||||
| -rw-r--r-- | Logger.pm | 40 | ||||
| -rw-r--r-- | PerlDaemon.pl (renamed from perldaemon.pl) | 41 | ||||
| -rw-r--r-- | control.sh | 2 |
4 files changed, 60 insertions, 40 deletions
diff --git a/DaemonLogic.pm b/DaemonLogic.pm new file mode 100644 index 0000000..49583ed --- /dev/null +++ b/DaemonLogic.pm @@ -0,0 +1,17 @@ +package DaemonLogic; + +sub new ($$$) { + my ($class, $conf) = @_; + + return bless { conf => $conf }, $class; +} + +sub do ($) { + my $self = shift; + my $conf = $self->{conf}; + my $logger = $conf->{logger}; + + $logger->logmsg('Test'); +} + +1; 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; diff --git a/perldaemon.pl b/PerlDaemon.pl index 6aa6def..a6129f2 100644 --- a/perldaemon.pl +++ b/PerlDaemon.pl @@ -5,47 +5,10 @@ use strict; use warnings; -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"); -} - -package PerlDaemon; +use POSIX qw(setsid strftime); -use POSIX qw(setsid); +use Logger; use DaemonLogic; $| = 1; @@ -2,7 +2,7 @@ case $1 in start) - perl perldaemon.pl perldaemon.conf + perl PerlDaemon.pl perldaemon.conf ;; stop) |
