diff --git a/PBot/Class.pm b/PBot/Class.pm index dd4bf650..625bcc7d 100644 --- a/PBot/Class.pm +++ b/PBot/Class.pm @@ -24,6 +24,7 @@ sub new { } $self->{pbot} = $conf{pbot}; + $self->{pbot}->{logger}->log("Initializing $class\n"); $self->initialize(%conf); return $self; } diff --git a/PBot/DualIndexHashObject.pm b/PBot/DualIndexHashObject.pm index 806a667d..329e0037 100644 --- a/PBot/DualIndexHashObject.pm +++ b/PBot/DualIndexHashObject.pm @@ -12,7 +12,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::DualIndexHashObject; -use parent 'PBot::Class'; use warnings; use strict; use feature 'unicode_strings'; @@ -20,6 +19,16 @@ use feature 'unicode_strings'; use Text::Levenshtein qw(fastdistance); 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 { my ($self, %conf) = @_; $self->{name} = $conf{name} // 'Dual Index hash object'; diff --git a/PBot/HashObject.pm b/PBot/HashObject.pm index 3e876419..c9b29a6e 100644 --- a/PBot/HashObject.pm +++ b/PBot/HashObject.pm @@ -11,7 +11,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::HashObject; -use parent 'PBot::Class'; use warnings; use strict; use feature 'unicode_strings'; @@ -19,6 +18,16 @@ use feature 'unicode_strings'; use Text::Levenshtein qw(fastdistance); 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 { my ($self, %conf) = @_; $self->{name} = $conf{name} // 'hash object'; diff --git a/PBot/Logger.pm b/PBot/Logger.pm index 602795d5..55dcc49b 100644 --- a/PBot/Logger.pm +++ b/PBot/Logger.pm @@ -3,7 +3,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Logger; -use parent 'PBot::Class'; use warnings; use strict; use feature 'unicode_strings'; @@ -11,6 +10,17 @@ use feature 'unicode_strings'; use Scalar::Util qw/openhandle/; 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 { my ($self, %conf) = @_; $self->{logfile} = $conf{filename} // Carp::croak "Missing logfile parameter in " . __FILE__; @@ -18,7 +28,7 @@ sub initialize { my $path = dirname $self->{logfile}; 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"; } @@ -34,7 +44,7 @@ sub log { my $time = localtime; $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 "$time :: $text"; + print "$time :: $text" unless $self->{pbot}->{overrides}->{'general.daemon'}; } sub rotate_log { @@ -42,6 +52,7 @@ sub rotate_log { my $time = localtime $self->{start}; $time =~ s/\s+/_/g; + $self->log("Rotating log to $self->{logfile}-$time\n"); # logfile has to be closed first for maximum compatibility with `rename` close LOGFILE; rename $self->{logfile}, $self->{logfile} . '-' . $time; diff --git a/PBot/PBot.pm b/PBot/PBot.pm index e16406a0..b49ddfc7 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -60,9 +60,6 @@ sub initialize { my ($self, %conf) = @_; $self->{startup_timestamp} = time; - $self->{atexit} = PBot::Registerable->new(%conf, pbot => $self); - $self->register_signal_handlers; - my $data_dir = $conf{data_dir}; my $module_dir = $conf{module_dir}; my $plugin_dir = $conf{plugin_dir}; @@ -78,8 +75,32 @@ sub initialize { } } - # logger created first to allow other modules to log things - $self->{logger} = PBot::Logger->new(pbot => $self, filename => "$data_dir/log/log", %conf); + # 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); # make sure the environment is sane if (not -d $data_dir) { @@ -165,24 +186,9 @@ sub initialize { $self->{registry}->set('general', 'plugin_dir', 'value', $plugin_dir, 0, 1); # override registry entries with command-line arguments, if any - 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) { - $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 - + foreach my $override (keys %{$self->{overrides}}) { + my ($section, $key) = split /\./, $override; + my $value = $self->{overrides}->{$override}; $self->{logger}->log("Overriding $section.$key to $value\n"); $self->{registry}->set($section, $key, 'value', $value, 0, 1); } diff --git a/PBot/Registerable.pm b/PBot/Registerable.pm index 79a289eb..7416183a 100644 --- a/PBot/Registerable.pm +++ b/PBot/Registerable.pm @@ -8,11 +8,20 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. package PBot::Registerable; -use parent 'PBot::Class'; use warnings; use strict; 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 { my $self = shift; $self->{handlers} = [];