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;
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2019-12-30 23:50:36 +01:00
|
|
|
use Scalar::Util qw/openhandle/;
|
2019-12-29 19:44:05 +01:00
|
|
|
use File::Basename;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-02-14 22:32:12 +01:00
|
|
|
sub new {
|
|
|
|
my ($proto, %conf) = @_;
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $conf{pbot};
|
|
|
|
$self->{pbot} = $conf{pbot};
|
|
|
|
print "Initializing " . __PACKAGE__ . "\n" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
2020-02-04 02:19:04 +01:00
|
|
|
$self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__;
|
|
|
|
$self->{start} = time;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2020-02-04 02:19:04 +01:00
|
|
|
my $path = dirname $self->{logfile};
|
2019-12-29 20:06:52 +01:00
|
|
|
if (not -d $path) {
|
2020-02-14 22:32:12 +01:00
|
|
|
print "Creating new logfile path: $path\n" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
2019-12-29 20:06:52 +01:00
|
|
|
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 02:19:04 +01:00
|
|
|
open LOGFILE, ">>$self->{logfile}" or Carp::croak "Couldn't open logfile $self->{logfile}: $!\n";
|
2019-12-29 20:06:52 +01:00
|
|
|
LOGFILE->autoflush(1);
|
|
|
|
|
|
|
|
$self->{pbot}->{atexit}->register(sub { $self->rotate_log; return; });
|
2010-03-17 07:36:54 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub log {
|
|
|
|
my ($self, $text) = @_;
|
|
|
|
my $time = localtime;
|
2017-09-05 09:18:35 +02:00
|
|
|
$text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
|
2020-02-04 02:19:04 +01:00
|
|
|
print LOGFILE "$time :: $text" if openhandle *LOGFILE;
|
2020-02-14 22:32:12 +01:00
|
|
|
print "$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 {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $time = localtime $self->{start};
|
|
|
|
$time =~ s/\s+/_/g;
|
2019-12-30 23:50:36 +01:00
|
|
|
|
2020-02-14 22:32:12 +01:00
|
|
|
$self->log("Rotating log to $self->{logfile}-$time\n");
|
2019-12-30 23:50:36 +01:00
|
|
|
# logfile has to be closed first for maximum compatibility with `rename`
|
2019-12-29 20:06:52 +01:00
|
|
|
close LOGFILE;
|
|
|
|
rename $self->{logfile}, $self->{logfile} . '-' . $time;
|
2019-12-30 23:50:36 +01:00
|
|
|
|
|
|
|
# reopen renamed logfile to resume any needed logging
|
|
|
|
open LOGFILE, ">>$self->{logfile}-$time" or Carp::carp "Couldn't re-open logfile $self->{logfile}-$time: $!\n";
|
|
|
|
LOGFILE->autoflush(1) if openhandle *LOGFILE;
|
2019-12-29 20:06:52 +01:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
1;
|