diff --git a/src/applib/examples/example_storage_detector.cpp b/src/applib/examples/example_storage_detector.cpp index 9eedc8c..2f0bc71 100644 --- a/src/applib/examples/example_storage_detector.cpp +++ b/src/applib/examples/example_storage_detector.cpp @@ -13,6 +13,7 @@ Copyright: #include "applib/storage_device.h" #include "applib/storage_detector.h" +#include "hz/main_tools.h" #include "gsc_settings.h" // in src directory @@ -20,30 +21,31 @@ Copyright: /// Main function of the test int main() { - // These settings contain device search paths, smartctl binary, etc... - init_default_settings(); + return hz::main_exception_wrapper([]() + { + // These settings contain device search paths, smartctl binary, etc... + init_default_settings(); - std::vector drives; -// std::vector match_patterns; - std::vector blacklist_patterns; // additional parameters + std::vector drives; + // std::vector match_patterns; + std::vector blacklist_patterns; // additional parameters - StorageDetector sd; -// sd.add_match_patterns(match_patterns); - sd.add_blacklist_patterns(blacklist_patterns); + StorageDetector sd; + // sd.add_match_patterns(match_patterns); + sd.add_blacklist_patterns(blacklist_patterns); - auto ex_factory = std::make_shared(false); - std::string error_msg = sd.detect_and_fetch_basic_data(drives, ex_factory); - if (!error_msg.empty()) { - std::cerr << error_msg << "\n"; + auto ex_factory = std::make_shared(false); + std::string error_msg = sd.detect_and_fetch_basic_data(drives, ex_factory); + if (!error_msg.empty()) { + std::cerr << error_msg << "\n"; - } else { - for (const auto& drive : drives) { - std::cerr << drive->get_device_with_type() << - " (" << StorageDevice::get_type_storable_name(drive->get_detected_type()) << ")\n"; + } else { + for (const auto& drive : drives) { + std::cerr << drive->get_device_with_type() << + " (" << StorageDevice::get_type_storable_name(drive->get_detected_type()) << ")\n"; + } } - } - - return 0; + }); } diff --git a/src/gsc_main.cpp b/src/gsc_main.cpp index d04af58..078095b 100644 --- a/src/gsc_main.cpp +++ b/src/gsc_main.cpp @@ -9,79 +9,44 @@ Copyright: /// \weakgroup gsc /// @{ -#include // cerr -#include // std::exception, std::set_terminate() #include // EXIT_* #include "hz/win32_tools.h" // hz::win32_* +#include "hz/main_tools.h" #include "gsc_init.h" // app_init_and_loop() -/// \def HAVE_VERBOSE_TERMINATE_HANDLER -/// Defined to 0 or 1. If 1, compiler supports __gnu_cxx::__verbose_terminate_handler (libstdc++). -#ifndef HAVE_VERBOSE_TERMINATE_HANDLER - #if defined __GLIBCXX__ - #define HAVE_VERBOSE_TERMINATE_HANDLER 1 - #else - #define HAVE_VERBOSE_TERMINATE_HANDLER 0 - #endif -#endif - - /// Application main function -int main(int argc, char* argv[]) +int main(int argc, char** argv) { - // we still leave __GNUC__ for autoconf-less setups. -#if defined HAVE_VERBOSE_TERMINATE_HANDLER && HAVE_VERBOSE_TERMINATE_HANDLER - // Verbose uncaught exception handler - std::set_terminate(__gnu_cxx::__verbose_terminate_handler); -#else - try { -#endif + return hz::main_exception_wrapper([&argc, &argv]() + { + // disable "Send to MS..." dialog box in non-debug builds + #if defined _WIN32 && !(defined DEBUG_BUILD && DEBUG_BUILD) + SetErrorMode(SEM_FAILCRITICALERRORS); + #endif - // disable "Send to MS..." dialog box in non-debug builds -#if defined _WIN32 && !(defined DEBUG_BUILD && DEBUG_BUILD) - SetErrorMode(SEM_FAILCRITICALERRORS); -#endif + // debug builds already have a console, no need to create one. + #if defined _WIN32 && !(defined DEBUG_BUILD && DEBUG_BUILD) + // if the console is not open, or unsupported (win2k), use files. + if (!hz::win32_redirect_stdio_to_console()) { // redirect stdout/stderr to console (if open and supported) + hz::win32_redirect_stdio_to_files(); // redirect stdout/stderr to output files + } + #endif - // debug builds already have a console, no need to create one. -#if defined _WIN32 && !(defined DEBUG_BUILD && DEBUG_BUILD) - // if the console is not open, or unsupported (win2k), use files. - if (!hz::win32_redirect_stdio_to_console()) { // redirect stdout/stderr to console (if open and supported) - hz::win32_redirect_stdio_to_files(); // redirect stdout/stderr to output files - } -#endif + // initialize stuff and enter the main loop + if (!app_init_and_loop(argc, argv)) { + return EXIT_FAILURE; + } - // initialize stuff and enter the main loop - if (!app_init_and_loop(argc, argv)) - return EXIT_FAILURE; - - -// print uncaught exceptions for non-gcc-compatible -#if !(defined HAVE_VERBOSE_TERMINATE_HANDLER && HAVE_VERBOSE_TERMINATE_HANDLER) - } - catch(std::exception& e) { - // don't use anything other than cerr here, it's the most safe option. - std::cerr << "main(): Unhandled exception: " << e.what() << std::endl; - - return EXIT_FAILURE; - } - catch(...) { // this guarantees proper unwinding in case of unhandled exceptions (win32 I think) - std::cerr << "main(): Unhandled unknown exception." << std::endl; - - return EXIT_FAILURE; - } -#endif - - return EXIT_SUCCESS; + return EXIT_SUCCESS; + }); } - - /// @} diff --git a/src/libdebug/examples/example_libdebug.cpp b/src/libdebug/examples/example_libdebug.cpp index 74f0c10..72a2196 100644 --- a/src/libdebug/examples/example_libdebug.cpp +++ b/src/libdebug/examples/example_libdebug.cpp @@ -46,7 +46,7 @@ namespace { template struct TestClassB { template - U func2([[maybe_unused]] V v, int) + U func2([[maybe_unused]] V v, [[maybe_unused]] int i) { debug_out_info("default", DBG_FUNC_PRNAME << "\n"); debug_out_info("default", DBG_FUNC_MSG << "function called.\n");