2021-06-19 06:23:34 +02:00
|
|
|
# File: Logger.pm
|
|
|
|
#
|
|
|
|
# Purpose: Logs text to file and STDOUT.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::Logger;
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2019-12-30 23:50:36 +01:00
|
|
|
use Scalar::Util qw/openhandle/;
|
2019-12-29 19:44:05 +01:00
|
|
|
use File::Basename;
|
2020-09-13 01:28:44 +02:00
|
|
|
use File::Copy;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-02-14 22:32:12 +01:00
|
|
|
sub new {
|
2021-06-19 06:23:34 +02:00
|
|
|
my ($class, %args) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $args{pbot};
|
|
|
|
$self->{pbot} = delete $args{pbot};
|
2020-02-15 23:38:32 +01:00
|
|
|
print "Initializing " . __PACKAGE__ . "\n" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
2021-06-19 06:23:34 +02:00
|
|
|
$self->initialize(%args);
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self;
|
2020-02-14 22:32:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# ensure logfile path was provided
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# record start time for later logfile rename in rotation
|
|
|
|
$self->{start} = time;
|
|
|
|
|
|
|
|
# get directories leading to logfile
|
2020-02-15 23:38:32 +01:00
|
|
|
my $path = dirname $self->{logfile};
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# create log file path
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not -d $path) {
|
|
|
|
print "Creating new logfile path: $path\n" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
|
|
|
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# open log file with utf8 encoding
|
|
|
|
open LOGFILE, ">> :encoding(UTF-8)", $self->{logfile} or Carp::croak "Couldn't open logfile $self->{logfile}: $!\n";
|
2020-02-15 23:38:32 +01:00
|
|
|
LOGFILE->autoflush(1);
|
2019-12-29 20:06:52 +01:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# rename logfile to start-time at exit
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{atexit}->register(sub { $self->rotate_log; return; });
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub log {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $text) = @_;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# get current time
|
2020-02-15 23:38:32 +01:00
|
|
|
my $time = localtime;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# replace potentially log-corrupting characters (colors, gibberish, etc)
|
2020-02-15 23:38:32 +01:00
|
|
|
$text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# log to file
|
2020-02-15 23:38:32 +01:00
|
|
|
print LOGFILE "$time :: $text" if openhandle * LOGFILE;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# and print to stdout unless daemonized
|
|
|
|
print STDOUT "$time :: $text" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 20:06:52 +01:00
|
|
|
sub rotate_log {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# get start time
|
2020-02-15 23:38:32 +01:00
|
|
|
my $time = localtime $self->{start};
|
2021-06-07 04:12:14 +02:00
|
|
|
$time =~ s/\s+/_/g; # replace spaces with underscores
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->log("Rotating log to $self->{logfile}-$time\n");
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# rename log to start time
|
2020-09-13 01:28:44 +02:00
|
|
|
move($self->{logfile}, $self->{logfile} . '-' . $time);
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# set new start time for next rotation
|
|
|
|
$self->{start} = time;
|
2019-12-29 20:06:52 +01:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|