bugfixes because i never test anything before releasing

This commit is contained in:
Matthew Connelly 2015-03-06 23:26:33 +00:00
parent 616e175faa
commit c9ad9c50cd
1 changed files with 7 additions and 6 deletions

View File

@ -43,15 +43,15 @@ sub timefmt2str {
sub init {
#Expects that it will be launched either in an environment where $HOME is an exported env variable or that it will be launched by an initscript with the current working directory being the user's homedir. conf file location can be manually set by passing it in the LOBORC env variable.
$base = $ENV{HOME} || cwd();
my $confp = $ENV{LOBORC} || "$base/.${ME}rc";
my $loaded = (-e $confp and -f $confp and -r $confp and not -z $confp)? init_conf($confp) : -1;
my $confp = $ENV{LOBORC} || "$base/.${ME}rc"; my $loaded = 0;
$loaded = init_conf($confp) if -e $confp and -f $confp and -r $confp and not -z $confp;
if ($Conf->{general}->{storelogs} or not $loaded) {
open(STDOUT, ">>".$Conf->{general}->{logfile}) if length $Conf->{general}->{logfile};
open(STDERR, ">>".$Conf->{general}->{errlogfile}) if length $Conf->{general}->{errlogfile};
select((select(STDOUT), $|=1)[0]);
}
$loaded == 0 and logger(9,"Configuration file at $confp exists but could not be loaded. Please check it is fully-valid YAML.");
$loaded == -1 and logger(9,"Configuration file at $confp either doesn't exist or is unreadable/empty.");
$loaded == 0 and logger(9,"Configuration file at $confp either doesn't exist or is unreadable/empty.");
$loaded == -1 and logger(9,"Configuration file at $confp exists but could not be loaded. Please check it is fully-valid YAML.");
$loaded == -2 and logger(9,"Configuration file at $confp loaded but did not contain any enabled monitors.");
$loaded == -3 and logger(9,"Configuration file at $confp loaded but was missing a required configuration parameter.");
#then we run the kernel once, I forget why
@ -73,10 +73,11 @@ sub init {
logger(1, "$ME version $VERSION started.");
}
sub init_conf {
$Conf = YAML::LoadFile($_) or return 0;
$Conf = YAML::LoadFile(shift) or return -1;
$base = $Conf->{general}->{home} if defined $Conf->{general}->{home} and length $Conf->{general}->{home};
return -3 unless defined $Conf->{general}->{tmp} and length $Conf->{general}->{tmp};
$Conf->{general}->{tmp} .= '/' unless $Conf->{general}->{tmp} =~ /\/$/;
mkdir $Conf->{general}->{tmp} or logger(9,"Error creating work directory $Conf->{general}->{tmp}: $!") unless -d $Conf->{general}->{tmp};
$Conf->{general}->{storelogs} = 1 unless defined $Conf->{general}->{storelogs} and $Conf->{general}->{storelogs} =~ /^[01]$/;
$Conf->{general}->{pidfile} = "$base/.$ME.pid" unless exists $Conf->{general}->{pidfile};
$Conf->{general}->{logfile} = "$base/.$ME.log" unless exists $Conf->{general}->{logfile};
@ -113,7 +114,7 @@ sub sigtrap {
}
sub logger {
my ($pri,$msg) = @_;
print timefmt2str('%e %B %T')." $HOSTNAME $ME\[$$] ($pri): $msg\n";
print timefmt2str('%e %B %T')." $HOSTNAME $ME\[$$] ($pri): $msg\n" unless $pri =~ /^[29]$/;
print STDERR timefmt2str('%e %B %T')." $HOSTNAME $ME\[$$] ($pri): $msg\n" if $pri =~ /^[29]$/;
notify($ME,$msg) if $pri == 3 and $Conf->{general}->{notify}->{on}->{error} == 1;
exit 0 if $pri == 8;