From eba23340502fda67ccc9a769a5f47d6668f53025 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Tue, 18 Jan 2022 17:20:51 +0100 Subject: [PATCH] #5 better error handling, do not leave the program in a dangling state --- APRSHandlerThread.cpp | 2 ++ DPlusAuthenticator.cpp | 2 ++ DStarGatewayApp.cpp | 23 ++++++++++++++--------- DStarGatewayApp.h | 2 +- DStarGatewayThread.cpp | 2 ++ IcomRepeaterProtocolHandler.cpp | 2 ++ 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/APRSHandlerThread.cpp b/APRSHandlerThread.cpp index 9745341..1d6b099 100644 --- a/APRSHandlerThread.cpp +++ b/APRSHandlerThread.cpp @@ -201,9 +201,11 @@ void* CAPRSHandlerThread::Entry() catch (std::exception& e) { std::string message(e.what()); CLog::logInfo("Exception raised in the APRS Writer thread - \"%s\"", message.c_str()); + throw; } catch (...) { CLog::logInfo("Unknown exception raised in the APRS Writer thread"); + throw; } #endif diff --git a/DPlusAuthenticator.cpp b/DPlusAuthenticator.cpp index b9d1768..e67ccf7 100644 --- a/DPlusAuthenticator.cpp +++ b/DPlusAuthenticator.cpp @@ -90,9 +90,11 @@ void* CDPlusAuthenticator::Entry() catch (std::exception& e) { std::string message(e.what()); CLog::logError("Exception raised in the D-Plus Authenticator thread - \"%s\"", message.c_str()); + throw; } catch (...) { CLog::logError("Unknown exception raised in the D-Plus Authenticator thread"); + throw; } #endif diff --git a/DStarGatewayApp.cpp b/DStarGatewayApp.cpp index 45f2c8a..7c498d3 100644 --- a/DStarGatewayApp.cpp +++ b/DStarGatewayApp.cpp @@ -54,11 +54,10 @@ int main(int argc, char *argv[]) { std::set_terminate(CDStarGatewayApp::terminateHandler); - // TODO 2022-01-17 handle SIGTERM etc .... - // signal(SIGSEGV, CDStarGatewayApp::sigHandler); - // signal(SIGILL, CDStarGatewayApp::sigHandler); - // signal(SIGFPE, CDStarGatewayApp::sigHandler); - // signal(SIGABRT, CDStarGatewayApp::sigHandler); + signal(SIGSEGV, CDStarGatewayApp::sigHandlerFatal); + signal(SIGILL, CDStarGatewayApp::sigHandlerFatal); + signal(SIGFPE, CDStarGatewayApp::sigHandlerFatal); + signal(SIGABRT, CDStarGatewayApp::sigHandlerFatal); // signal(SIGTERM, CDStarGatewayApp::sigHandler); setbuf(stdout, NULL); @@ -276,9 +275,15 @@ bool CDStarGatewayApp::createThread() return true; } -void CDStarGatewayApp::sigHandler(int /*sig*/) +void CDStarGatewayApp::sigHandlerFatal(int sig) { - // TODO 2022-01-17 handle SIGTERM etc .... + CLog::logFatal("Caught signal : %s", strsignal(sig)); +#ifdef DEBUG_DSTARGW + std::stringstream stackTrace; + stackTrace << boost::stacktrace::stacktrace(); + CLog::logFatal("Stack Trace : \n%s", stackTrace.str().c_str()); +#endif + exit(1); } void CDStarGatewayApp::terminateHandler() @@ -297,14 +302,14 @@ void CDStarGatewayApp::terminateHandler() std::rethrow_exception(eptr); } else { - CLog::logFatal("Unhandled unkown exception occured"); + CLog::logFatal("Unhandled unknown exception occured"); } } catch(const std::exception& e) { CLog::logFatal("Unhandled exception occured %s", e.what()); } #ifdef DEBUG_DSTARGW - CLog::logFatal("%s", stackTrace.str().c_str()); + CLog::logFatal("Stack Trace : \n%s", stackTrace.str().c_str()); #endif exit(1); } \ No newline at end of file diff --git a/DStarGatewayApp.h b/DStarGatewayApp.h index 4929018..08c67cb 100644 --- a/DStarGatewayApp.h +++ b/DStarGatewayApp.h @@ -37,6 +37,6 @@ public: bool init(); void run(); - static void sigHandler(int sig); + static void sigHandlerFatal(int sig); static void terminateHandler(); }; diff --git a/DStarGatewayThread.cpp b/DStarGatewayThread.cpp index fce9403..1db50f4 100644 --- a/DStarGatewayThread.cpp +++ b/DStarGatewayThread.cpp @@ -418,9 +418,11 @@ void* CDStarGatewayThread::Entry() catch (std::exception& e) { std::string message(e.what()); CLog::logFatal("Exception raised in the main thread - \"%s\"", message.c_str()); + throw; } catch (...) { CLog::logFatal("Unknown exception raised in the main thread"); + throw; } #endif diff --git a/IcomRepeaterProtocolHandler.cpp b/IcomRepeaterProtocolHandler.cpp index 51f30cb..d6e20f8 100644 --- a/IcomRepeaterProtocolHandler.cpp +++ b/IcomRepeaterProtocolHandler.cpp @@ -152,9 +152,11 @@ void* CIcomRepeaterProtocolHandler::Entry() catch (std::exception& e) { std::string message(e.what()); CLog::logError("Exception raised in the Icom Controller thread - \"%s\"", message.c_str()); + throw; } catch (...) { CLog::logError("Unknown exception raised in the Icom Controller thread"); + throw; } #endif