From 6e8c880d464b6b846771c2c3b4aed82acfb9e772 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 8 Dec 2014 10:44:31 +0100 Subject: [PATCH] added global try-catch to main --- src/main.cpp | 64 ++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1f7e139..33772e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -353,35 +353,61 @@ int main( int argc, const char *argv[] ) init_logger(); GlobalLogger.notice() << "started"; - ConfigurationItem configuration = get_configuration( argc, argv ); - if ( configuration.get() != NULL ) + PingSchedulerList scheduler_list; + int ret_code = 0; + try { - int log_level = configuration->get_log_level(); - I2n::Logger::set_log_level( log_level ); //lint !e534 - GlobalLogger.info() << "Set LogLevel to " << I2n::Logger::get_log_level_string() << endl; + ConfigurationItem configuration = get_configuration( argc, argv ); + if ( configuration.get() != NULL ) + { + int log_level = configuration->get_log_level(); + I2n::Logger::set_log_level( log_level ); //lint !e534 + GlobalLogger.info() << "Set LogLevel to " << I2n::Logger::get_log_level_string() << endl; - set_log_output( configuration ); + set_log_output( configuration ); - bool daemon_mode = configuration->get_daemon(); - if ( daemon_mode ) - { - I2n::Daemon::daemonize(); - } + bool daemon_mode = configuration->get_daemon(); + if ( daemon_mode ) + { + I2n::Daemon::daemonize(); + } - LinkStatusItem status_notifier = get_status_notifier( configuration ); + LinkStatusItem status_notifier = get_status_notifier( configuration ); - PingSchedulerList scheduler_list; - init_pingers( configuration, status_notifier, &scheduler_list ); + init_pingers( configuration, status_notifier, &scheduler_list ); - start_pingers( scheduler_list ); + start_pingers( scheduler_list ); - // React to signals (main thread will sleep) - handle_signals( log_level ); + // React to signals (main thread will sleep) + handle_signals( log_level ); + } + } + catch ( const exception &ex ) + { + GlobalLogger.error() << "Error: uncaught exception. " << ex.what() << endl; + ret_code = 1; + } + catch (...) { + GlobalLogger.error() << "Error: caught unknown exception!" << endl; + ret_code = 2; + } + // clean up + try + { + GlobalLogger.info() << "Cleaning up" << endl; stop_pingers( scheduler_list ); - GlobalLogger.notice() << "exiting"; } + catch ( const exception &ex ) + { + GlobalLogger.error() << "Error: uncaught exception while cleaning up " << ex.what() << endl; + ret_code += 4; + } + catch (...) { + GlobalLogger.error() << "Error: caught unknown exception while cleaning up!" << endl; + ret_code += 8; + } - return 0; + return ret_code; } -- 1.7.1