Better crash signal handling

This commit is contained in:
Nikita Lisitsa 2023-08-06 19:07:14 +03:00
parent f6377045c9
commit 5eb7cbff92

View file

@ -1,6 +1,7 @@
#include <psemek/log/log.hpp> #include <psemek/log/log.hpp>
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <boost/stacktrace/stacktrace.hpp>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
@ -28,8 +29,6 @@ namespace psemek::log
std::mutex sinks_mutex; std::mutex sinks_mutex;
std::vector<std::unique_ptr<sink>> sinks; std::vector<std::unique_ptr<sink>> sinks;
using signal_handler = void(*)(int);
struct signal_data struct signal_data
{ {
int signal; int signal;
@ -38,14 +37,17 @@ namespace psemek::log
signal_data const signals[] = signal_data const signals[] =
{ {
{SIGSEGV, "Aborting due to SIGSEGV"}, {SIGINT, "Aborting due to SIGINT" },
{SIGILL, "Aborting due to SIGILL" },
{SIGABRT, "Aborting due to SIGABRT"}, {SIGABRT, "Aborting due to SIGABRT"},
{SIGFPE, "Aborting due to SIGFPE" },
{SIGSEGV, "Aborting due to SIGSEGV"},
{SIGTERM, "Aborting due to SIGTERM"}, {SIGTERM, "Aborting due to SIGTERM"},
}; };
struct sinks_flusher struct signal_handler
{ {
sinks_flusher() signal_handler()
{ {
for (auto const & p : signals) for (auto const & p : signals)
std::signal(p.signal, &handler); std::signal(p.signal, &handler);
@ -58,7 +60,8 @@ namespace psemek::log
{ {
if (p.signal == signal) if (p.signal == signal)
{ {
put_message(level::error, p.message); log::error() << p.message;
log::error() << boost::stacktrace::stacktrace();
break; break;
} }
} }
@ -73,7 +76,7 @@ namespace psemek::log
std::signal(signal, SIG_DFL); std::signal(signal, SIG_DFL);
std::raise(signal); std::raise(signal);
} }
} flusher; } signal_handler;
void put_message(level l, std::string const & str, std::string const & thread_name) void put_message(level l, std::string const & str, std::string const & thread_name)
{ {