Implement logging via Log::Any, cleanup, remove logger()
This commit is contained in:
@@ -6,7 +6,6 @@ use warnings;
|
||||
use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
|
||||
use version; our $VERSION = qw(1.0);
|
||||
|
||||
my $workdir;
|
||||
@@ -26,18 +25,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 jabberbot qw(run_jabberbot);
|
||||
|
||||
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) {
|
||||
@@ -54,15 +58,21 @@ if ($UID == 0) {
|
||||
);
|
||||
}
|
||||
|
||||
if ($C->{'debug_log'}) {
|
||||
unless (-d dirname($C->{'debug_log'})) {
|
||||
die "Unable to create $piddir" unless (-d $piddir); ## no critic (ErrorHandling::RequireCarping)
|
||||
|
||||
if (defined $logfile) {
|
||||
my $logdir = dirname ($logfile);
|
||||
|
||||
unless (-d $logdir) {
|
||||
make_path (
|
||||
dirname($C->{'debug_log'}), {
|
||||
$logdir, {
|
||||
uid => $uid,
|
||||
group => $gid
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
die "Unable to create $logdir" unless (-d $logdir); ## no critic (ErrorHandling::RequireCarping)
|
||||
}
|
||||
|
||||
# drop privs
|
||||
@@ -72,27 +82,31 @@ 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 ();
|
||||
|
||||
# write pidfile
|
||||
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 "Unable to get abs_path: $OS_ERROR\n" if (! defined $apath);
|
||||
|
||||
my $pidfile = sprintf ('%s/%s.pid', $piddir, basename ($apath));
|
||||
logger ("Creating pidfile at $pidfile");
|
||||
warn "Creating pidfile at $pidfile\n";
|
||||
open (my $pid, '>', $pidfile) or die "Error opening $pidfile: $OS_ERROR\n";
|
||||
print $pid $PID or die "Error writing to $pidfile: $OS_ERROR\n";
|
||||
close $pid; ## 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)
|
||||
|
||||
# run bot, at last
|
||||
run_jabberbot ();
|
||||
unlink ($pidfile) if (-f $pidfile);
|
||||
exit 0;
|
||||
@@ -101,8 +115,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;
|
||||
@@ -115,5 +130,10 @@ sub __fork () {
|
||||
return;
|
||||
}
|
||||
|
||||
sub __signal_handler ($) {
|
||||
warn "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:
|
||||
|
||||
4
cpanfile
4
cpanfile
@@ -13,7 +13,9 @@ requires 'JSON::PP';
|
||||
requires 'POSIX';
|
||||
requires 'URI::URL';
|
||||
requires 'local::lib';
|
||||
|
||||
requires 'Log::Any';
|
||||
requires 'Log::Any::Adapter';
|
||||
requires 'Log::Any::For::Std';
|
||||
|
||||
# Bundled Net::Jabber::Bot deps
|
||||
# requires 'Net::Jabber::Bot';
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
"cachedata": "data/cache/openweathermap.Id-Data.db",
|
||||
"appid": "get_it_on_openweathermap.org"
|
||||
},
|
||||
"debug_log" : "/var/log/aleesa-telegram-bot/debug.log",
|
||||
"log" : "/var/log/aleesa-jabber-bot/aleesa-jabber-bot.log",
|
||||
"lat" : {
|
||||
"brain" : "data/lat/brain.sqlite",
|
||||
"src" : "lat.txt"
|
||||
|
||||
@@ -12,14 +12,11 @@ use URI::URL;
|
||||
use JSON::XS qw(decode_json encode_json);
|
||||
use Digest::MD5 qw(md5_base64);
|
||||
use DB_File;
|
||||
|
||||
use conf qw(loadConf);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(weather logger trim randomCommonPhrase);
|
||||
|
||||
our @EXPORT_OK = qw(weather trim randomCommonPhrase);
|
||||
$VERSION = '1.0';
|
||||
|
||||
my $c = loadConf ();
|
||||
@@ -189,22 +186,6 @@ sub __urlencode($) {
|
||||
return $str;
|
||||
}
|
||||
|
||||
sub logger {
|
||||
my $msg = shift;
|
||||
|
||||
if ($c->{debug_log}) {
|
||||
my $mode = '>';
|
||||
$mode = '>>' if (-f $c->{debug_log});
|
||||
|
||||
if (open (my $LOG, $mode, $c->{debug_log})) {
|
||||
print $LOG $msg . "\n"; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
close $LOG; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub trim ($) {
|
||||
my $str = shift;
|
||||
|
||||
|
||||
@@ -7,13 +7,11 @@ use warnings;
|
||||
use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use JSON::XS;
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(loadConf);
|
||||
|
||||
$VERSION = '1.0';
|
||||
|
||||
sub loadConf {
|
||||
|
||||
@@ -8,18 +8,16 @@ use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
use Carp qw(cluck croak);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(seed fortune);
|
||||
|
||||
$VERSION = '1.0';
|
||||
|
||||
use SQLite_File;
|
||||
use MIME::Base64;
|
||||
use File::Path qw(mkpath);
|
||||
use conf qw(loadConf);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(seed fortune);
|
||||
$VERSION = '1.0';
|
||||
|
||||
my $c = loadConf();
|
||||
my $dir = $c->{fortune}->{dir};
|
||||
my $srcdir = $c->{fortune}->{srcdir};
|
||||
|
||||
@@ -8,18 +8,16 @@ use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
use Carp qw(cluck croak);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(seed friday);
|
||||
|
||||
$VERSION = '1.0';
|
||||
|
||||
use SQLite_File;
|
||||
use MIME::Base64;
|
||||
use File::Path qw(mkpath);
|
||||
use conf qw(loadConf);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(seed friday);
|
||||
$VERSION = '1.0';
|
||||
|
||||
my $c = loadConf();
|
||||
my $dir = $c->{friday}->{dir};
|
||||
my $srcfile = $c->{friday}->{src};
|
||||
|
||||
@@ -8,12 +8,10 @@ use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use open qw(:std :utf8);
|
||||
|
||||
use Net::Jabber::Bot;
|
||||
use Hailo;
|
||||
|
||||
use conf qw(loadConf);
|
||||
use botlib qw(weather logger trim randomCommonPhrase);
|
||||
use botlib qw(weather trim randomCommonPhrase);
|
||||
use lat qw(latAnswer);
|
||||
use karma qw(karmaSet karmaGet);
|
||||
use friday qw(friday);
|
||||
@@ -131,8 +129,8 @@ EOL
|
||||
my $mytext = substr ($text, 7);
|
||||
|
||||
if ($mytext ne '') {
|
||||
chomp($mytext);
|
||||
$mytext = trim($mytext);
|
||||
chomp $mytext;
|
||||
$mytext = trim $mytext;
|
||||
}
|
||||
|
||||
$bot->SendGroupMessage ($hash{'reply_to'}, karmaGet ($hash{'reply_to'}, $mytext));
|
||||
|
||||
@@ -9,15 +9,13 @@ use English qw( -no_match_vars );
|
||||
use Digest::SHA qw(sha1_base64);
|
||||
use DB_File;
|
||||
use Carp qw(cluck);
|
||||
use conf qw(loadConf);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(karmaSet karmaGet);
|
||||
|
||||
$VERSION = '1.0';
|
||||
|
||||
use conf qw(loadConf);
|
||||
|
||||
my $c = loadConf ();
|
||||
my $karmadir = $c->{karma}->{dir};
|
||||
my $max = 5;
|
||||
|
||||
13
lib/lat.pm
13
lib/lat.pm
@@ -8,18 +8,15 @@ use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
use Carp qw(cluck);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(train latAnswer);
|
||||
|
||||
$VERSION = '1.0';
|
||||
|
||||
use Hailo;
|
||||
use File::Basename qw(dirname);
|
||||
use File::Path qw(mkpath);
|
||||
use conf qw(loadConf);
|
||||
use botlib qw(logger);
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(train latAnswer);
|
||||
$VERSION = '1.0';
|
||||
|
||||
my $c = loadConf();
|
||||
my $brain = $c->{lat}->{brain};
|
||||
|
||||
Reference in New Issue
Block a user