Merge pull request #2116 from connortechnology/fix_perl_logging

when detecting a down db connection, need to clear the stored sth as …
pull/2122/head
Andrew Bauer 2018-06-08 08:44:22 -05:00 committed by GitHub
commit 74a44869cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 22 deletions

View File

@ -279,6 +279,9 @@ sub initialise( @ ) {
$this->{initialised} = !undef; $this->{initialised} = !undef;
# this function can get called on a previously initialized log Object, so clean any sth's
$this->{sth} = undef;
Debug( 'LogOpts: level='.$codes{$this->{level}} Debug( 'LogOpts: level='.$codes{$this->{level}}
.'/'.$codes{$this->{effectiveLevel}} .'/'.$codes{$this->{effectiveLevel}}
.', screen='.$codes{$this->{termLevel}} .', screen='.$codes{$this->{termLevel}}
@ -320,6 +323,8 @@ sub reinitialise {
my $screenLevel = $this->termLevel(); my $screenLevel = $this->termLevel();
$this->termLevel(NOLOG); $this->termLevel(NOLOG);
$this->termLevel($screenLevel) if $screenLevel > NOLOG; $this->termLevel($screenLevel) if $screenLevel > NOLOG;
$this->{sth} = undef;
} }
# Prevents undefined logging levels # Prevents undefined logging levels
@ -549,15 +554,23 @@ sub logPrint {
print(STDERR $message) if $level <= $this->{termLevel}; print(STDERR $message) if $level <= $this->{termLevel};
if ( $level <= $this->{databaseLevel} ) { if ( $level <= $this->{databaseLevel} ) {
if ( ( $this->{dbh} and $this->{dbh}->ping() ) or ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) { if ( ! ( $this->{dbh} and $this->{dbh}->ping() ) ) {
$this->{sth} = undef;
if ( ! ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) {
print(STDERR "Can't log to database: ");
$this->{databaseLevel} = NOLOG;
return;
}
}
my $sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, NULL )'; my $sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, NULL )';
$this->{sth} = $this->{dbh}->prepare_cached($sql) if ! $this->{sth}; $this->{sth} = $this->{dbh}->prepare_cached($sql) if ! $this->{sth};
if ( !$this->{sth} ) { if ( !$this->{sth} ) {
$this->{databaseLevel} = NOLOG; $this->{databaseLevel} = NOLOG;
Error("Can't prepare log entry '$sql': ".$this->{dbh}->errstr()); Error("Can't prepare log entry '$sql': ".$this->{dbh}->errstr());
} else { return;
}
my $res = $this->{sth}->execute($seconds+($microseconds/1000000.0) my $res = $this->{sth}->execute($seconds+($microseconds/1000000.0)
, $this->{id} , $this->{id}
, $$ , $$
@ -570,10 +583,6 @@ sub logPrint {
$this->{databaseLevel} = NOLOG; $this->{databaseLevel} = NOLOG;
Error("Can't execute log entry '$sql': ".$this->{dbh}->errstr()); Error("Can't execute log entry '$sql': ".$this->{dbh}->errstr());
} }
}
} else {
print(STDERR "Can't log to database: ");
}
} # end if doing db logging } # end if doing db logging
} # end if level < effectivelevel } # end if level < effectivelevel
} }