#20 add banner and signal handlers

pull/32/head
Geoffrey Merck 4 years ago
parent 8f5f878d23
commit 318d1b7c62

@ -20,17 +20,42 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <sstream>
#ifdef DEBUG_DSTARGW
#include <boost/stacktrace.hpp>
#endif
#include "DGWTimeServerApp.h" #include "DGWTimeServerApp.h"
#include "Version.h"
#include "Log.h"
CDGWTimeServerApp * CDGWTimeServerApp::g_app = nullptr;
const std::string BANNER_1 = CStringUtils::string_format("%s %s Copyright (C) %s\n", APPLICATION_NAME.c_str(), VENDOR_NAME.c_str());
const std::string BANNER_2 = "DGWTimeServer comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n";
const std::string BANNER_3 = "This is free software, and you are welcome to distribute it under certain conditions that are discussed in the LICENSE file.\n\n";
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
std::set_terminate(CDGWTimeServerApp::terminateHandler);
signal(SIGSEGV, CDGWTimeServerApp::sigHandlerFatal);
signal(SIGILL, CDGWTimeServerApp::sigHandlerFatal);
signal(SIGFPE, CDGWTimeServerApp::sigHandlerFatal);
signal(SIGABRT, CDGWTimeServerApp::sigHandlerFatal);
signal(SIGTERM, CDGWTimeServerApp::sigHandler);
signal(SIGINT, CDGWTimeServerApp::sigHandler);
if (2 != argc) { if (2 != argc) {
printf("usage: %s path_to_config_file\n", argv[0]); printf("usage: %s path_to_config_file\n", argv[0]);
printf(" %s --version\n", argv[0]); printf(" %s --version\n", argv[0]);
return 1; return 1;
} }
std::cout << std::endl << BANNER_1 << BANNER_2 << BANNER_3;
if(argv[1][0] == '-') {
return 0;
}
std::string configfile(argv[1]); std::string configfile(argv[1]);
CTimeServerConfig config(configfile); CTimeServerConfig config(configfile);
if(!config.load()) if(!config.load())
@ -93,3 +118,54 @@ bool CDGWTimeServerApp::createThread()
return ret; return ret;
} }
void CDGWTimeServerApp::sigHandler(int sig)
{
CLog::logInfo("Caught signal : %s, shutting down gateway", strsignal(sig));
if(g_app != nullptr && g_app->m_thread != nullptr) {
g_app->m_thread->kill();
}
}
void CDGWTimeServerApp::sigHandlerFatal(int sig)
{
CLog::logFatal("Caught signal : %s", strsignal(sig));
fprintf(stderr, "Caught signal : %s\n", strsignal(sig));
#ifdef DEBUG_DSTARGW
std::stringstream stackTrace;
stackTrace << boost::stacktrace::stacktrace();
CLog::logFatal("Stack Trace : \n%s", stackTrace.str().c_str());
fprintf(stderr, "Stack Trace : \n%s\n", stackTrace.str().c_str());
#endif
exit(3);
}
void CDGWTimeServerApp::terminateHandler()
{
#ifdef DEBUG_DSTARGW
std::stringstream stackTrace;
stackTrace << boost::stacktrace::stacktrace();
#endif
std::exception_ptr eptr;
eptr = std::current_exception();
try {
if (eptr != nullptr) {
std::rethrow_exception(eptr);
}
else {
CLog::logFatal("Unhandled unknown exception occured");
fprintf(stderr, "Unknown ex\n");
}
} catch(const std::exception& e) {
CLog::logFatal("Unhandled exception occured %s", e.what());
fprintf(stderr, "Unhandled ex %s\n", e.what());
}
#ifdef DEBUG_DSTARGW
CLog::logFatal("Stack Trace : \n%s", stackTrace.str().c_str());
#endif
exit(2);
}

@ -25,8 +25,6 @@
class CDGWTimeServerApp class CDGWTimeServerApp
{ {
private:
/* data */
public: public:
CDGWTimeServerApp(const CTimeServerConfig * config); CDGWTimeServerApp(const CTimeServerConfig * config);
~CDGWTimeServerApp(); ~CDGWTimeServerApp();
@ -34,9 +32,15 @@ public:
bool init(); bool init();
void run(); void run();
static void sigHandler(int sig);
static void sigHandlerFatal(int sig);
static void terminateHandler();
private: private:
bool createThread(); bool createThread();
static CDGWTimeServerApp * g_app;
const CTimeServerConfig * m_config; const CTimeServerConfig * m_config;
CTimeServerThread * m_thread; CTimeServerThread * m_thread;
}; };

@ -19,7 +19,7 @@
#pragma once #pragma once
const std::string APPLICATION_NAME("DStarGateway time Server"); const std::string APPLICATION_NAME("DStarGateway Time Server");
enum LANGUAGE { enum LANGUAGE {
LANG_ENGLISH_UK_1, LANG_ENGLISH_UK_1,

Loading…
Cancel
Save

Powered by TurnKey Linux.