mirror of
https://github.com/pragma-/pbot.git
synced 2024-12-23 19:22:40 +01:00
Improve PBot start-up logging; improve handling of general.daemon
override
This commit is contained in:
parent
5ce1e69b49
commit
515c5c2c52
@ -24,6 +24,7 @@ sub new {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$self->{pbot} = $conf{pbot};
|
$self->{pbot} = $conf{pbot};
|
||||||
|
$self->{pbot}->{logger}->log("Initializing $class\n");
|
||||||
$self->initialize(%conf);
|
$self->initialize(%conf);
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
package PBot::DualIndexHashObject;
|
package PBot::DualIndexHashObject;
|
||||||
use parent 'PBot::Class';
|
|
||||||
|
|
||||||
use warnings; use strict;
|
use warnings; use strict;
|
||||||
use feature 'unicode_strings';
|
use feature 'unicode_strings';
|
||||||
@ -20,6 +19,16 @@ use feature 'unicode_strings';
|
|||||||
use Text::Levenshtein qw(fastdistance);
|
use Text::Levenshtein qw(fastdistance);
|
||||||
use JSON;
|
use JSON;
|
||||||
|
|
||||||
|
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};
|
||||||
|
$self->initialize(%conf);
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{name} = $conf{name} // 'Dual Index hash object';
|
$self->{name} = $conf{name} // 'Dual Index hash object';
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
package PBot::HashObject;
|
package PBot::HashObject;
|
||||||
use parent 'PBot::Class';
|
|
||||||
|
|
||||||
use warnings; use strict;
|
use warnings; use strict;
|
||||||
use feature 'unicode_strings';
|
use feature 'unicode_strings';
|
||||||
@ -19,6 +18,16 @@ use feature 'unicode_strings';
|
|||||||
use Text::Levenshtein qw(fastdistance);
|
use Text::Levenshtein qw(fastdistance);
|
||||||
use JSON;
|
use JSON;
|
||||||
|
|
||||||
|
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};
|
||||||
|
$self->initialize(%conf);
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{name} = $conf{name} // 'hash object';
|
$self->{name} = $conf{name} // 'hash object';
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
package PBot::Logger;
|
package PBot::Logger;
|
||||||
use parent 'PBot::Class';
|
|
||||||
|
|
||||||
use warnings; use strict;
|
use warnings; use strict;
|
||||||
use feature 'unicode_strings';
|
use feature 'unicode_strings';
|
||||||
@ -11,6 +10,17 @@ use feature 'unicode_strings';
|
|||||||
use Scalar::Util qw/openhandle/;
|
use Scalar::Util qw/openhandle/;
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__;
|
$self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__;
|
||||||
@ -18,7 +28,7 @@ sub initialize {
|
|||||||
|
|
||||||
my $path = dirname $self->{logfile};
|
my $path = dirname $self->{logfile};
|
||||||
if (not -d $path) {
|
if (not -d $path) {
|
||||||
print "Creating new logfile path: $path\n";
|
print "Creating new logfile path: $path\n" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
||||||
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
|
mkdir $path or Carp::croak "Couldn't create logfile path: $!\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +44,7 @@ sub log {
|
|||||||
my $time = localtime;
|
my $time = localtime;
|
||||||
$text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
|
$text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
|
||||||
print LOGFILE "$time :: $text" if openhandle *LOGFILE;
|
print LOGFILE "$time :: $text" if openhandle *LOGFILE;
|
||||||
print "$time :: $text";
|
print "$time :: $text" unless $self->{pbot}->{overrides}->{'general.daemon'};
|
||||||
}
|
}
|
||||||
|
|
||||||
sub rotate_log {
|
sub rotate_log {
|
||||||
@ -42,6 +52,7 @@ sub rotate_log {
|
|||||||
my $time = localtime $self->{start};
|
my $time = localtime $self->{start};
|
||||||
$time =~ s/\s+/_/g;
|
$time =~ s/\s+/_/g;
|
||||||
|
|
||||||
|
$self->log("Rotating log to $self->{logfile}-$time\n");
|
||||||
# logfile has to be closed first for maximum compatibility with `rename`
|
# logfile has to be closed first for maximum compatibility with `rename`
|
||||||
close LOGFILE;
|
close LOGFILE;
|
||||||
rename $self->{logfile}, $self->{logfile} . '-' . $time;
|
rename $self->{logfile}, $self->{logfile} . '-' . $time;
|
||||||
|
50
PBot/PBot.pm
50
PBot/PBot.pm
@ -60,9 +60,6 @@ sub initialize {
|
|||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{startup_timestamp} = time;
|
$self->{startup_timestamp} = time;
|
||||||
|
|
||||||
$self->{atexit} = PBot::Registerable->new(%conf, pbot => $self);
|
|
||||||
$self->register_signal_handlers;
|
|
||||||
|
|
||||||
my $data_dir = $conf{data_dir};
|
my $data_dir = $conf{data_dir};
|
||||||
my $module_dir = $conf{module_dir};
|
my $module_dir = $conf{module_dir};
|
||||||
my $plugin_dir = $conf{plugin_dir};
|
my $plugin_dir = $conf{plugin_dir};
|
||||||
@ -78,7 +75,31 @@ sub initialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# logger created first to allow other modules to log things
|
# check command-line arguments for registry overrides
|
||||||
|
foreach my $arg (@ARGV) {
|
||||||
|
next if $arg =~ m/^-?(?:general\.)?(?:config|data|module|plugin)_dir=.*$/; # already processed
|
||||||
|
my ($item, $value) = split /=/, $arg, 2;
|
||||||
|
|
||||||
|
if (not defined $item or not defined $value) {
|
||||||
|
print STDERR "Fatal error: unknown argument `$arg`; arguments must be in the form of `section.key=value` (e.g.: irc.botnick=newnick)\n";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
my ($section, $key) = split /\./, $item, 2;
|
||||||
|
if (not defined $section or not defined $key) {
|
||||||
|
print STDERR "Fatal error: bad argument `$arg`; registry entries must be in the form of section.key (e.g.: irc.botnick)\n";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$section =~ s/^-//; # remove a leading - to allow arguments like -irc.botnick due to habitual use of -args
|
||||||
|
$self->{overrides}->{"$section.$key"} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
# let modules register signal handlers
|
||||||
|
$self->{atexit} = PBot::Registerable->new(%conf, pbot => $self);
|
||||||
|
$self->register_signal_handlers;
|
||||||
|
|
||||||
|
# create logger
|
||||||
$self->{logger} = PBot::Logger->new(pbot => $self, filename => "$data_dir/log/log", %conf);
|
$self->{logger} = PBot::Logger->new(pbot => $self, filename => "$data_dir/log/log", %conf);
|
||||||
|
|
||||||
# make sure the environment is sane
|
# make sure the environment is sane
|
||||||
@ -165,24 +186,9 @@ sub initialize {
|
|||||||
$self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1);
|
$self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1);
|
||||||
|
|
||||||
# override registry entries with command-line arguments, if any
|
# override registry entries with command-line arguments, if any
|
||||||
foreach my $arg (@ARGV) {
|
foreach my $override (keys %{$self->{overrides}}) {
|
||||||
next if $arg =~ m/^-?(?:general\.)?(?:config|data|module|plugin)_dir=.*$/; # already processed
|
my ($section, $key) = split /\./, $override;
|
||||||
my ($item, $value) = split /=/, $arg, 2;
|
my $value = $self->{overrides}->{$override};
|
||||||
|
|
||||||
if (not defined $item or not defined $value) {
|
|
||||||
$self->{logger}->log("Fatal error: unknown argument `$arg`; arguments must be in the form of `section.key=value` (e.g.: irc.botnick=newnick)\n");
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
my ($section, $key) = split /\./, $item, 2;
|
|
||||||
|
|
||||||
if (not defined $section or not defined $key) {
|
|
||||||
$self->{logger}->log("Fatal error: bad argument `$arg`; registry entries must be in the form of section.key (e.g.: irc.botnick)\n");
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$section =~ s/^-//; # remove a leading - to allow arguments like -irc.botnick due to habitual use of -args
|
|
||||||
|
|
||||||
$self->{logger}->log("Overriding $section.$key to $value\n");
|
$self->{logger}->log("Overriding $section.$key to $value\n");
|
||||||
$self->{registry}->set($section, $key, 'value', $value, 0, 1);
|
$self->{registry}->set($section, $key, 'value', $value, 0, 1);
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,20 @@
|
|||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
package PBot::Registerable;
|
package PBot::Registerable;
|
||||||
use parent 'PBot::Class';
|
|
||||||
|
|
||||||
use warnings; use strict;
|
use warnings; use strict;
|
||||||
use feature 'unicode_strings';
|
use feature 'unicode_strings';
|
||||||
|
|
||||||
|
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};
|
||||||
|
$self->initialize(%conf);
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
$self->{handlers} = [];
|
$self->{handlers} = [];
|
||||||
|
Loading…
Reference in New Issue
Block a user