Implement logging via Log::Any, cleanup, remove logger()
This commit is contained in:
@@ -26,18 +26,23 @@ use Cwd::utf8 qw(chdir abs_path);
|
||||
use POSIX qw(setgid setuid setsid);
|
||||
use File::Path qw(make_path);
|
||||
use File::Basename qw(basename dirname);
|
||||
use Log::Any::Adapter;
|
||||
use Log::Any::For::Std;
|
||||
use conf qw(loadConf);
|
||||
use botlib qw(logger);
|
||||
use telegrambot qw(run_telegrambot);
|
||||
|
||||
sub daemonize;
|
||||
sub __fork;
|
||||
sub __signal_handler;
|
||||
|
||||
local $OUTPUT_AUTOFLUSH = 1;
|
||||
my $runAsUid = $UID;
|
||||
|
||||
my $piddir = '/var/run/' . basename (abs_path ($PROGRAM_NAME));
|
||||
my $C = loadConf ();
|
||||
my $logfile;
|
||||
if (defined $C->{'log'}) { $logfile = $C->{'log'}; }
|
||||
elsif (defined $C->{'debug_log'}) {$logfile = $C->{'debug_log'}; }
|
||||
|
||||
# if we run under root, create pid dir and set correct owner for it, same for debug log if it is defined
|
||||
if ($UID == 0) {
|
||||
@@ -58,15 +63,19 @@ if ($UID == 0) {
|
||||
);
|
||||
}
|
||||
|
||||
if ($C->{'debug_log'}) {
|
||||
unless (-d dirname ($C->{'debug_log'})) {
|
||||
if (defined $logfile) {
|
||||
my $logdir = dirname ($logfile);
|
||||
|
||||
unless (-d $logdir) {
|
||||
make_path (
|
||||
dirname ($C->{'debug_log'}), {
|
||||
uid => $uid,
|
||||
group => $gid
|
||||
}
|
||||
$logdir, {
|
||||
uid => $uid,
|
||||
group => $gid
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
die "Unable to create $logdir" unless (-d $logdir); ## no critic (ErrorHandling::RequireCarping)
|
||||
}
|
||||
|
||||
# drop privs
|
||||
@@ -76,6 +85,12 @@ if ($UID == 0) {
|
||||
$piddir = $workdir;
|
||||
}
|
||||
|
||||
if (defined $logfile) {
|
||||
Log::Any::Adapter->set ('File', $logfile);
|
||||
} else {
|
||||
Log::Any::Adapter->set ('File', '/dev/null');
|
||||
}
|
||||
|
||||
# double fork attach to /dev/null all stdio and go background
|
||||
daemonize ();
|
||||
|
||||
@@ -83,17 +98,19 @@ daemonize ();
|
||||
my $apath = abs_path $PROGRAM_NAME;
|
||||
|
||||
unless (defined $apath) {
|
||||
logger "Unable to get abs_path: $OS_ERROR";
|
||||
die "Unable to get abs_path: $OS_ERROR\n";
|
||||
die "[FATAL] Unable to get abs_path: $OS_ERROR\n";
|
||||
};
|
||||
|
||||
# write pidfile
|
||||
my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath;
|
||||
logger "Creating pidfile at $pidfile";
|
||||
open my $MYPID, '>', $pidfile or die "Error opening $pidfile: $OS_ERROR\n";
|
||||
print {$MYPID} $PID or die "Error writing to $pidfile: $OS_ERROR\n";
|
||||
warn "[INFO] Creating pidfile at $pidfile\n";
|
||||
open my $MYPID, '>', $pidfile or die "[FATAL] Error opening $pidfile: $OS_ERROR\n";
|
||||
print {$MYPID} $PID or die "[FATAL] Error writing to $pidfile: $OS_ERROR\n";
|
||||
close $MYPID; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
|
||||
local $SIG{INT} = \&__signal_handler;
|
||||
local $SIG{TERM} = \&__signal_handler;
|
||||
|
||||
# set correct proctitle
|
||||
$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars)
|
||||
|
||||
@@ -110,8 +127,9 @@ sub daemonize {
|
||||
__fork ();
|
||||
POSIX::setsid ();
|
||||
open STDIN, '<', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
open STDOUT, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
open STDERR, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
# we handle STDOUT && STDERR via Log::Any::For::Std, so do not close them explicitly
|
||||
#open STDOUT, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
#open STDERR, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
__fork ();
|
||||
umask 0;
|
||||
return;
|
||||
@@ -119,10 +137,16 @@ sub daemonize {
|
||||
|
||||
sub __fork {
|
||||
my $pid = fork;
|
||||
unless (defined $pid) { die "Can't fork: $OS_ERROR\n"; }
|
||||
unless (defined $pid) { die "[FATAL] Can't fork: $OS_ERROR\n"; }
|
||||
exit if ($pid != 0);
|
||||
return;
|
||||
}
|
||||
|
||||
sub __signal_handler {
|
||||
warn "[INFO] Caught a signal $OS_ERROR\n";
|
||||
unlink $pidfile if (-f $pidfile);
|
||||
exit 0;
|
||||
}
|
||||
|
||||
__END__
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
|
||||
@@ -23,8 +23,7 @@ BEGIN {
|
||||
use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5");
|
||||
use fortune qw(seed fortune);
|
||||
use Carp;
|
||||
$SIG{__DIE__} = sub { Carp::confess @_ };
|
||||
local $SIG{__DIE__} = sub { Carp::confess @_ };
|
||||
|
||||
seed ();
|
||||
|
||||
exit 0;
|
||||
|
||||
@@ -24,5 +24,4 @@ use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5
|
||||
use friday qw(seed);
|
||||
|
||||
seed ();
|
||||
|
||||
exit 0;
|
||||
|
||||
Reference in New Issue
Block a user