diff --git a/PBot/Logger.pm b/PBot/Logger.pm index a338d069..064f21bc 100644 --- a/PBot/Logger.pm +++ b/PBot/Logger.pm @@ -9,6 +9,7 @@ use strict; use feature 'unicode_strings'; +use File::Basename; use Carp (); sub new { @@ -18,19 +19,24 @@ sub new { my ($class, %conf) = @_; - my $log_file = delete $conf{log_file}; + my $logfile = delete $conf{filename}; - if (defined $log_file) { - open PLOG_FILE, ">>$log_file" or Carp::croak "Couldn't open log file: $!\n" if defined $log_file; - PLOG_FILE->autoflush(1); + if (defined $logfile) { + my $path = dirname $logfile; + if (not -d $path) { + print "Creating new logfile path: $path\n"; + mkdir $path or Carp::croak "Couldn't create logfile path: $!\n"; + } + + open LOGFILE, ">>$logfile" or Carp::croak "Couldn't open logfile $logfile: $!\n"; + LOGFILE->autoflush(1); } my $self = { - log_file => $log_file, + logfile => $logfile, }; bless $self, $class; - return $self; } @@ -40,8 +46,8 @@ sub log { $text =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge; - if (defined $self->{log_file}) { - print PLOG_FILE "$time :: $text"; + if (defined $self->{logfile}) { + print LOGFILE "$time :: $text"; } print "$time :: $text"; diff --git a/PBot/PBot.pm b/PBot/PBot.pm index a544708b..940639b2 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -62,8 +62,25 @@ sub new { sub initialize { my ($self, %conf) = @_; + $self->{startup_timestamp} = time; + + my $data_dir = $conf{data_dir}; + my $module_dir = $conf{module_dir}; + my $plugin_dir = $conf{plugin_dir}; + + # check command-line arguments for directory overrides + foreach my $arg (@ARGV) { + if ($arg =~ m/^(?:general\.)?((?:data|module|plugin)_dir)=(.*)$/) { + my $override = $1; + my $value = $2; + $data_dir = $value if $override eq 'data_dir'; + $module_dir = $value if $override eq 'module_dir'; + $plugin_dir = $value if $override eq 'plugin_dir'; + } + } + # logger created first to allow other modules to log things - $self->{logger} = PBot::Logger->new(log_file => $conf{log_file}, %conf); + $self->{logger} = PBot::Logger->new(filename => "$data_dir/log/log", %conf); $self->{version} = PBot::VERSION->new(pbot => $self, %conf); $self->{logger}->log($self->{version}->version . "\n"); @@ -79,22 +96,6 @@ sub initialize { $self->{refresher} = PBot::Refresher->new(pbot => $self); - my $data_dir = $conf{data_dir}; - my $module_dir = $conf{module_dir}; - my $plugin_dir = $conf{plugin_dir}; - - # check command-line arguments for directory overrides - foreach my $arg (@ARGV) { - if ($arg =~ m/^(?:general\.)?((?:data|module|plugin)_dir)=(.*)$/) { - my $override = $1; - my $value = $2; - $self->{logger}->log("Overriding $override to $value\n"); - $data_dir = $value if $override eq 'data_dir'; - $module_dir = $value if $override eq 'module_dir'; - $plugin_dir = $value if $override eq 'plugin_dir'; - } - } - # make sure the environment is sane if (not -d $data_dir) { $self->{logger}->log("Data directory ($data_dir) does not exist; aborting...\n"); diff --git a/log/.gitignore b/data/log/.gitignore similarity index 100% rename from log/.gitignore rename to data/log/.gitignore diff --git a/pbot b/pbot index d3d1798a..1a090d9e 100755 --- a/pbot +++ b/pbot @@ -33,9 +33,6 @@ my %config = ( # Path to directory containing loadable plugins plugin_dir => "$bothome/Plugins", - # Path to log file - log_file => "$bothome/log/log", - # ----------------------------------------------------- # The bot can export the latest factoids and quotegrabs to an HTML # document. If you run a webserver or something similiar, you may