diff --git a/libs/log/include/psemek/log/log.hpp b/libs/log/include/psemek/log/log.hpp index 8f568820..07db955a 100644 --- a/libs/log/include/psemek/log/log.hpp +++ b/libs/log/include/psemek/log/log.hpp @@ -43,6 +43,7 @@ namespace psemek::log struct sink { virtual void put_message(message const & msg) = 0; + virtual void flush() = 0; virtual ~sink() {} }; diff --git a/libs/log/source/log.cpp b/libs/log/source/log.cpp index 553dd402..c85ee769 100644 --- a/libs/log/source/log.cpp +++ b/libs/log/source/log.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace psemek::log { @@ -25,6 +26,39 @@ namespace psemek::log std::mutex sinks_mutex; std::vector> sinks; + std::pair const signals[] = + { + {SIGSEGV, "Aborting due to SIGSEGV"}, + {SIGABRT, "Aborting due to SIGABRT"}, + {SIGTERM, "Aborting due to SIGTERM"}, + }; + + struct sinks_flusher + { + sinks_flusher() + { + for (auto const & p : signals) + std::signal(p.first, &handler); + } + + private: + static void handler(int signal) + { + for (auto const & p : signals) + { + if (p.first == signal) + { + put_message(level::error, p.second); + break; + } + } + + for (auto & sink : sinks) + sink->flush(); + std::exit(1); + } + } flusher; + void put_message(level l, std::string const & str, std::string const & thread_name) { message msg @@ -76,6 +110,11 @@ namespace psemek::log stream_->write(str.data(), str.size()); } + void flush() override + { +// stream_->flush(); + } + private: std::unique_ptr stream_; level level_;