@@ -391,30 +391,39 @@ int main(const int argc, const char** const argv)
391391 setupLogging (pipe_write_fd, should_daemonize, argc, argv);
392392
393393 spdlog::info (" OCVSMD started (ver='{}.{}')." , VERSION_MAJOR, VERSION_MINOR);
394+ int result = EXIT_SUCCESS;
394395 {
395- Application application;
396- if (const auto failure_str = application.init ())
396+ try
397397 {
398- spdlog::critical (" Failed to init application: {}" , failure_str.value ());
399-
400- // Report the failure to the parent process (if daemonized; otherwise goes to stderr).
401- writeString (pipe_write_fd, " Failed to init application: " );
402- writeString (pipe_write_fd, failure_str.value ().c_str ());
403- ::exit (EXIT_FAILURE);
404- }
405- if (should_daemonize)
398+ Application application;
399+ if (const auto failure_str = application.init ())
400+ {
401+ spdlog::critical (" Failed to init application: {}" , failure_str.value ());
402+
403+ // Report the failure to the parent process (if daemonized; otherwise goes to stderr).
404+ writeString (pipe_write_fd, " Failed to init application: " );
405+ writeString (pipe_write_fd, failure_str.value ().c_str ());
406+ ::exit (EXIT_FAILURE);
407+ }
408+ if (should_daemonize)
409+ {
410+ step_14_notify_init_complete (pipe_write_fd);
411+ }
412+
413+ application.runWhile ([] { return g_running == 1 ; });
414+
415+ } catch (const std::exception& ex)
406416 {
407- step_14_notify_init_complete (pipe_write_fd);
417+ spdlog::critical (" Unhandled exception: {}" , ex.what ());
418+ result = EXIT_FAILURE;
408419 }
409420
410- application.runWhile ([] { return g_running == 1 ; });
411-
412421 if (g_running == 0 )
413422 {
414423 spdlog::debug (" Received termination signal." );
415424 }
416425 }
417426 spdlog::info (" OCVSMD daemon terminated." );
418427
419- return EXIT_SUCCESS ;
428+ return result ;
420429}
0 commit comments