Logfile now lives in data_dir

This commit is contained in:
Pragmatic Software 2019-12-29 10:44:05 -08:00
parent 0f513f9581
commit 4fbdcd948f
4 changed files with 32 additions and 28 deletions

View File

@ -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";

View File

@ -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");

3
pbot
View File

@ -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