added global try-catch to main
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 8 Dec 2014 09:44:31 +0000 (10:44 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 8 Dec 2014 09:44:31 +0000 (10:44 +0100)
src/main.cpp

index 1f7e139..33772e5 100644 (file)
@@ -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;
 }