0

Add logging via Log::Any, fork() to daemonize, fix misc bugs

This commit is contained in:
eleksir
2021-08-15 12:40:41 +03:00
parent 012274b9c1
commit f7ebc1a739
19 changed files with 388 additions and 104 deletions

View File

@@ -6,11 +6,11 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use Encode;
use HTML::TokeParser;
use HTTP::Tiny;
use JSON::XS;
use Log::Any qw ($log);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -58,14 +58,14 @@ sub Anek {
my @anek = split (/<br>/, $anek);
$ret = join ("\n", @anek);
} else {
carp (sprintf "[WARN] anekdot.ru server returns incorrect json, full response text message: %s\n", $response_text);
$log->warn (sprintf '[WARN] anekdot.ru server returns incorrect json, full response text message: %s', $response_text);
}
} else {
carp (sprintf "[WARN] anekdot.ru server returns unexpected response text: %s\n", $response_text);
$log->warn (sprintf '[WARN] anekdot.ru server returns unexpected response text: %s', $response_text);
}
} else {
carp (sprintf "[WARN] anekdot.ru server return status %s with message: %s\n", $r->code, $r->message);
$log->warn (sprintf '[WARN] anekdot.ru server return status %s with message: %s', $r->code, $r->message);
}
return $ret;

View File

@@ -6,9 +6,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use HTTP::Tiny;
use HTML::TokeParser;
use Log::Any qw ($log);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -47,7 +47,7 @@ sub Buni {
} while ($#{$a[0]} > 1);
}
} else {
carp (sprintf "[WARN] Server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason});
}
return $ret;

View File

@@ -7,8 +7,8 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use JSON::XS;
use Log::Any qw ($log);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -23,12 +23,12 @@ sub LoadConf {
unless ($readlen) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
carp ("[FATA] Unable to read $c: $OS_ERROR\n");
$log->fata ("[FATA] Unable to read $c: $OS_ERROR");
}
if ($readlen != $len) {
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
carp ("[FATA] File $c is $len bytes on disk, but we read only $readlen bytes\n");
$log->fata ("[FATA] File $c is $len bytes on disk, but we read only $readlen bytes");
}
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls

View File

@@ -6,13 +6,13 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use Encode;
use CHI;
use CHI::Driver::BerkeleyDB;
use DateTime;
use HTTP::Tiny;
use HTML::TokeParser;
use Log::Any qw ($log);
use POSIX qw (strftime);
use BotLib::Conf qw (LoadConf);
@@ -66,7 +66,7 @@ sub Drink {
$ret = join "\n", @holiday;
}
} else {
carp (sprintf "[WARN] Server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason});
return undef;
}

View File

@@ -7,8 +7,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (croak carp);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use MIME::Base64;
use SQLite_File;
@@ -69,7 +70,7 @@ sub Fortune () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir;
tie my @array, 'SQLite_File', $backingfile || do {
carp ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
$log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR");
return '';
};

View File

@@ -6,9 +6,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use JSON::XS;
use HTTP::Tiny;
use Log::Any qw ($log);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -35,7 +35,7 @@ sub Fox {
};
unless (defined $jfox) {
carp ("[WARN] Unable to decode JSON: $EVAL_ERROR\n");
$log->warn ("[WARN] Unable to decode JSON: $EVAL_ERROR");
} else {
if ($jfox->{image}) {
$jfox->{image} =~ s/\\//xmsg;
@@ -43,7 +43,7 @@ sub Fox {
}
}
} else {
carp (sprintf "[WARN] Server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason});
}
return $ret;

View File

@@ -7,8 +7,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (croak carp);
use Carp qw (croak);
use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use SQLite_File;
use BotLib::Conf qw (LoadConf);
@@ -80,7 +81,7 @@ sub Friday () {
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today];
tie my @array, 'SQLite_File', $backingfile || do {
carp ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
$log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR");
return '';
};

View File

@@ -6,11 +6,11 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use Digest::HMAC_SHA1 qw (hmac_sha1);
use File::Path qw (make_path);
use JSON::XS;
use HTTP::Tiny;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use MIME::Base64;
use SQLite_File;
@@ -338,19 +338,19 @@ sub flickrSearchByText {
if (defined $response) {
return $response;
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] HTTP status code not 200: $r->{status}, $r->{content}\n");
$log->warn ("[WARN] HTTP status code not 200: $r->{status}, $r->{content}");
return undef;
}
}
@@ -447,19 +447,19 @@ sub flickrSearchByTags {
if (defined $response) {
return $response;
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] Flickr api returns incorrect json: $r->{content}\n");
$log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}");
return undef;
}
} else {
carp ("[WARN] HTTP status code not 200: $r->{status}, $r>{content}\n");
$log->warn ("[WARN] HTTP status code not 200: $r->{status}, $r>{content}");
return undef;
}
}
@@ -560,13 +560,13 @@ sub FlickrByTags {
unless (-d $dir) {
make_path ($dir) or do {
carp ("[ERROR] Unable to create $dir: $OS_ERROR\n");
$log->error ("[ERROR] Unable to create $dir: $OS_ERROR");
return 0;
};
}
tie my %secret, 'SQLite_File', $backingfile || do {
carp ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
$log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR");
return 0;
};
@@ -587,7 +587,7 @@ sub FlickrByTags {
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item;
} else {
carp ("[NOTICE] Flickr api returns empty search result list\n");
$log->notice ('[NOTICE] Flickr api returns empty search result list');
return;
}
} else {
@@ -601,13 +601,13 @@ sub FlickrByText {
unless (-d $dir) {
make_path ($dir) or do {
carp ("[ERROR] Unable to create $dir: $OS_ERROR\n");
$log->error ("[ERROR] Unable to create $dir: $OS_ERROR");
return 0;
};
}
tie my %secret, 'SQLite_File', $backingfile || do {
carp ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
$log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR");
return 0;
};
@@ -628,7 +628,7 @@ sub FlickrByText {
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item;
} else {
carp ("[NOTICE] Flickr api returns empty search result list\n");
$log->notice ('[NOTICE] Flickr api returns empty search result list');
return;
}
} else {

View File

@@ -6,9 +6,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use DB_File;
use File::Path qw (make_path);
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (utf2sha1 trim);
@@ -34,14 +34,14 @@ sub KarmaSet (@) {
unless (-d $karmadir) {
make_path ($karmadir) or do {
carp ("[ERROR] Unable to create $karmadir: $OS_ERROR\n");
$log->error ("[ERROR] Unable to create $karmadir: $OS_ERROR");
return sprintf 'Карма %s составляет 0', $phrase;
};
}
# init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile || do {
carp ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR\n");
$log->error ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR");
return sprintf 'Карма %s составляет 0', $phrase;
};
@@ -94,14 +94,14 @@ sub KarmaGet (@) {
unless (-d $karmadir) {
make_path ($karmadir) or do {
carp ("Unable to create $karmadir: $OS_ERROR\n");
$log->error ("Unable to create $karmadir: $OS_ERROR");
return sprintf 'Карма %s составляет 0', $phrase;
};
}
# init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile || do {
carp ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR\n");
$log->error ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR");
return sprintf 'Карма %s составляет 0', $phrase;
};

View File

@@ -6,9 +6,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use JSON::XS;
use HTTP::Tiny;
use Log::Any qw ($log);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -35,14 +35,14 @@ sub Kitty {
};
unless (defined $jcat) {
carp ("[ERROR] Unable to decode JSON: $EVAL_ERROR\n");
$log->error ("[ERROR] Unable to decode JSON: $EVAL_ERROR");
} else {
if ($jcat->[0]->{url}) {
$ret = $jcat->[0]->{url};
}
}
} else {
carp (sprintf "Server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->error (sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason});
}
return $ret;

View File

@@ -11,6 +11,7 @@ use Carp qw (carp);
use File::Basename qw (dirname);
use File::Path qw (make_path);
use Hailo;
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0);
@@ -49,12 +50,12 @@ sub Lat () {
my $braindir = dirname $brain;
unless (-d $braindir) {
carp ("[ERROR] No lat module data: $braindir is absent! Train lat first.\n");
$log->error ("[ERROR] No lat module data: $braindir is absent! Train lat first.");
return '';
}
unless (-f $brain) {
carp ("[ERROR] No lat module data: $brain is absent! Train lat first.\n");
$log->error ("[ERROR] No lat module data: $brain is absent! Train lat first.");
return '';
}

View File

@@ -6,9 +6,9 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp);
use HTTP::Tiny;
use HTML::TokeParser;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use version; our $VERSION = qw (1.0);
@@ -51,7 +51,7 @@ sub Monkeyuser {
$ret = sprintf 'https://www.monkeyuser.com%s', $link[irand (1 + $#link)];
}
} else {
carp (sprintf "[WARN] Monkeyuser server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->warn (sprintf '[WARN] Monkeyuser server return status %s with message: %s', $r->{status}, $r->{reason});
}
return $ret;

View File

@@ -6,11 +6,11 @@ use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (carp cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use HTTP::Tiny;
use JSON::XS;
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf);
use BotLib::Util qw (trim urlencode);
@@ -33,7 +33,7 @@ sub Weather {
my $appid = $c->{openweathermap}->{appid};
unless (defined $appid) {
carp ("[WARN] No appid specified for openweathermap\n");
$log->warn ('[WARN] No appid specified for openweathermap');
return undef;
}
@@ -60,11 +60,11 @@ sub Weather {
};
unless (defined $fc) {
carp ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR\n");
$log->warn ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR");
return undef;
};
} else {
carp (sprintf "Server return status %s with message: %s\n", $r->{status}, $r->{reason});
$log->warn (sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason});
return undef;
}