From b4732e475032e7d96f085ec20e67ba3ea3de0b86 Mon Sep 17 00:00:00 2001 From: manupap1 Date: Fri, 14 Nov 2014 22:17:44 +0100 Subject: [PATCH] Close logger and database on exit --- src/zm_db.cpp | 11 +++++++++++ src/zm_db.h | 2 ++ src/zm_logger.cpp | 15 ++++++++++++++- src/zm_logger.h | 2 ++ src/zma.cpp | 2 ++ src/zmc.cpp | 3 +++ src/zmf.cpp | 2 ++ src/zms.cpp | 10 ++++++++++ src/zmstreamer.cpp | 5 +++++ src/zmu.cpp | 3 +++ 10 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/zm_db.cpp b/src/zm_db.cpp index 26a33d6f5..4ebc2f11f 100644 --- a/src/zm_db.cpp +++ b/src/zm_db.cpp @@ -64,3 +64,14 @@ void zmDbConnect() zmDbConnected = true; } +void zmDbClose() +{ + if ( zmDbConnected ) + { + mysql_close( &dbconn ); + // mysql_init() call implicitly mysql_library_init() but + // mysql_close() does not call mysql_library_end() + mysql_library_end(); + zmDbConnected = false; + } +} diff --git a/src/zm_db.h b/src/zm_db.h index dda2dd30f..07fd188a2 100644 --- a/src/zm_db.h +++ b/src/zm_db.h @@ -30,6 +30,8 @@ extern MYSQL dbconn; extern int zmDbConnected; void zmDbConnect(); +void zmDbClose(); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/zm_logger.cpp b/src/zm_logger.cpp index 19886ef1c..c64f6e7c0 100644 --- a/src/zm_logger.cpp +++ b/src/zm_logger.cpp @@ -239,6 +239,9 @@ void Logger::terminate() if ( mSyslogLevel > NOLOG ) closeSyslog(); + + if ( mDatabaseLevel > NOLOG ) + closeDatabase(); } bool Logger::boolEnv( const std::string &name, bool defaultValue ) @@ -468,6 +471,15 @@ void Logger::closeFile() } } +void Logger::closeDatabase() +{ + if ( mDbConnected ) + { + mysql_close( &mDbConnection ); + mDbConnected = false; + } +} + void Logger::openSyslog() { (void) openlog( mId.c_str(), LOG_PID|LOG_NDELAY, LOG_LOCAL1 ); @@ -604,5 +616,6 @@ void logInit( const char *name, const Logger::Options &options ) void logTerm() { - Logger::fetch()->terminate(); + if ( Logger::smInstance ) + delete Logger::smInstance; } diff --git a/src/zm_logger.h b/src/zm_logger.h index 50dce970c..90238fd19 100644 --- a/src/zm_logger.h +++ b/src/zm_logger.h @@ -115,6 +115,7 @@ private: public: friend void logInit( const char *name, const Options &options ); + friend void logTerm(); static Logger *fetch() { @@ -182,6 +183,7 @@ private: void closeFile(); void openSyslog(); void closeSyslog(); + void closeDatabase(); public: void logPrint( bool hex, const char * const file, const int line, const int level, const char *fstring, ... ); diff --git a/src/zma.cpp b/src/zma.cpp index 9a6658e54..117ada241 100644 --- a/src/zma.cpp +++ b/src/zma.cpp @@ -137,5 +137,7 @@ int main( int argc, char *argv[] ) { fprintf( stderr, "Can't find monitor with id of %d\n", id ); } + logTerm(); + zmDbClose(); return( 0 ); } diff --git a/src/zmc.cpp b/src/zmc.cpp index d84d30505..47c021309 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -302,5 +302,8 @@ int main( int argc, char *argv[] ) delete [] next_delays; delete [] last_capture_times; + logTerm(); + zmDbClose(); + return( result ); } diff --git a/src/zmf.cpp b/src/zmf.cpp index b10092a5c..663e57076 100644 --- a/src/zmf.cpp +++ b/src/zmf.cpp @@ -307,4 +307,6 @@ int main( int argc, char *argv[] ) sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } + logTerm(); + zmDbClose(); } diff --git a/src/zms.cpp b/src/zms.cpp index 0eab29756..9527ae4db 100644 --- a/src/zms.cpp +++ b/src/zms.cpp @@ -223,6 +223,8 @@ int main( int argc, const char *argv[] ) if ( !user ) { Error( "Unable to authenticate user" ); + logTerm(); + zmDbClose(); return( -1 ); } ValidateAccess( user, monitor_id ); @@ -286,6 +288,8 @@ int main( int argc, const char *argv[] ) #else // HAVE_LIBAVCODEC Error( "MPEG streaming of '%s' attempted while disabled", query ); fprintf( stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n" ); + logTerm(); + zmDbClose(); return( -1 ); #endif // HAVE_LIBAVCODEC } @@ -320,10 +324,16 @@ int main( int argc, const char *argv[] ) #else // HAVE_LIBAVCODEC Error( "MPEG streaming of '%s' attempted while disabled", query ); fprintf( stderr, "MPEG streaming is disabled.\nYou should ensure the ffmpeg libraries are installed and detected and rebuild to use this functionality.\n" ); + logTerm(); + zmDbClose(); return( -1 ); #endif // HAVE_LIBAVCODEC } stream.runStream(); } + + logTerm(); + zmDbClose(); + return( 0 ); } diff --git a/src/zmstreamer.cpp b/src/zmstreamer.cpp index ea61c7ff9..873d2fcd5 100644 --- a/src/zmstreamer.cpp +++ b/src/zmstreamer.cpp @@ -169,6 +169,8 @@ int main(int argc, char** argv) { stream.setStreamBitrate(bitrate); // default = 100000 (bitrate) #else fprintf(stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n"); + logTerm(); + zmDbClose(); return EXIT_FAILURE; #endif } @@ -192,5 +194,8 @@ int main(int argc, char** argv) { } if (debug) printf("Done.\n"); + logTerm(); + zmDbClose(); + return (EXIT_SUCCESS); } diff --git a/src/zmu.cpp b/src/zmu.cpp index bc032639f..01ebb7f77 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -754,5 +754,8 @@ int main( int argc, char *argv[] ) } delete user; + logTerm(); + zmDbClose(); + return( 0 ); }