diff --git a/cmake/Hunter/config.cmake b/cmake/Hunter/config.cmake index 228b14f192..bdda5f08ca 100644 --- a/cmake/Hunter/config.cmake +++ b/cmake/Hunter/config.cmake @@ -128,7 +128,7 @@ hunter_config( soralog # VERSION 0.2.4 URL https://github.com/qdrvm/soralog/archive/refs/tags/v0.2.4.tar.gz - SHA1 832a32cf134f8caab4a79c03024ac8408f848dae + SHA1 1de495d8a3a73c1e940be3fdddf263a2d673aec1 KEEP_PACKAGE_SOURCES ) diff --git a/core/application/impl/app_state_manager_impl.cpp b/core/application/impl/app_state_manager_impl.cpp index 36dfd4ffd2..ac7f3ceb08 100644 --- a/core/application/impl/app_state_manager_impl.cpp +++ b/core/application/impl/app_state_manager_impl.cpp @@ -10,9 +10,11 @@ #include namespace kagome::application { - std::atomic_bool AppStateManagerImpl::signals_enabled{false}; + std::weak_ptr AppStateManagerImpl::wp_to_myself; + + std::atomic_bool AppStateManagerImpl::shutting_down_signals_enabled{false}; - void AppStateManagerImpl::signalsEnable() { + void AppStateManagerImpl::shuttingDownSignalsEnable() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) struct sigaction act; memset(&act, 0, sizeof(act)); @@ -25,13 +27,14 @@ namespace kagome::application { sigaction(SIGINT, &act, nullptr); sigaction(SIGTERM, &act, nullptr); sigaction(SIGQUIT, &act, nullptr); - signals_enabled.store(true); + shutting_down_signals_enabled.store(true); sigprocmask(SIG_UNBLOCK, &act.sa_mask, nullptr); } - void AppStateManagerImpl::signalsDisable() { + void AppStateManagerImpl::shuttingDownSignalsDisable() { auto expected = true; - if (not signals_enabled.compare_exchange_strong(expected, false)) { + if (not shutting_down_signals_enabled.compare_exchange_strong(expected, + false)) { return; } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) @@ -43,24 +46,59 @@ namespace kagome::application { sigaction(SIGQUIT, &act, nullptr); } - std::weak_ptr AppStateManagerImpl::wp_to_myself; - void AppStateManagerImpl::shuttingDownSignalsHandler(int signal) { - signalsDisable(); + shuttingDownSignalsDisable(); if (auto self = wp_to_myself.lock()) { SL_TRACE(self->logger_, "Shutdown signal {} received", signal); self->shutdown(); } } + std::atomic_bool AppStateManagerImpl::log_rotate_signals_enabled{false}; + + void AppStateManagerImpl::logRotateSignalsEnable() { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + struct sigaction act; + memset(&act, 0, sizeof(act)); + act.sa_handler = logRotateSignalsHandler; + sigemptyset(&act.sa_mask); + sigaddset(&act.sa_mask, SIGHUP); + sigprocmask(SIG_BLOCK, &act.sa_mask, nullptr); + sigaction(SIGHUP, &act, nullptr); + log_rotate_signals_enabled.store(true); + sigprocmask(SIG_UNBLOCK, &act.sa_mask, nullptr); + } + + void AppStateManagerImpl::logRotateSignalsDisable() { + auto expected = true; + if (not log_rotate_signals_enabled.compare_exchange_strong(expected, + false)) { + return; + } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + struct sigaction act; + memset(&act, 0, sizeof(act)); + act.sa_handler = SIG_DFL; + sigaction(SIGHUP, &act, nullptr); + } + + void AppStateManagerImpl::logRotateSignalsHandler(int signal) { + if (auto self = wp_to_myself.lock()) { + SL_TRACE(self->logger_, "Log rotate signal {} received", signal); + log::doLogRotate(); + } + } + AppStateManagerImpl::AppStateManagerImpl() : logger_(log::createLogger("AppStateManager", "application")) { - signalsEnable(); - SL_TRACE(logger_, "Signal handler set up"); + shuttingDownSignalsEnable(); + logRotateSignalsEnable(); + SL_TRACE(logger_, "Signal handlers set up"); } AppStateManagerImpl::~AppStateManagerImpl() { - signalsDisable(); + shuttingDownSignalsDisable(); + logRotateSignalsDisable(); wp_to_myself.reset(); } @@ -223,7 +261,7 @@ namespace kagome::application { } void AppStateManagerImpl::shutdown() { - signalsDisable(); + shuttingDownSignalsDisable(); if (state_.load() == State::ReadyToStop) { SL_TRACE(logger_, "Shutting down requested, but app is ready to stop"); return; diff --git a/core/application/impl/app_state_manager_impl.hpp b/core/application/impl/app_state_manager_impl.hpp index 433e0a6c5e..a838a261b7 100644 --- a/core/application/impl/app_state_manager_impl.hpp +++ b/core/application/impl/app_state_manager_impl.hpp @@ -49,12 +49,18 @@ namespace kagome::application { void doShutdown() override; private: - static std::atomic_bool signals_enabled; - static void signalsEnable(); - static void signalsDisable(); static std::weak_ptr wp_to_myself; + + static std::atomic_bool shutting_down_signals_enabled; + static void shuttingDownSignalsEnable(); + static void shuttingDownSignalsDisable(); static void shuttingDownSignalsHandler(int); + static std::atomic_bool log_rotate_signals_enabled; + static void logRotateSignalsEnable(); + static void logRotateSignalsDisable(); + static void logRotateSignalsHandler(int); + void shutdownRequestWaiting(); log::Logger logger_; diff --git a/core/log/logger.cpp b/core/log/logger.cpp index 887cd5df98..ad29e9a470 100644 --- a/core/log/logger.cpp +++ b/core/log/logger.cpp @@ -120,6 +120,11 @@ namespace kagome::log { } } + void doLogRotate() { + auto logging_system = ensure_logger_system_is_initialized(); + logging_system->callRotateForAllSinks(); + } + Logger createLogger(const std::string &tag) { auto logging_system = ensure_logger_system_is_initialized(); return std::static_pointer_cast(logging_system) diff --git a/core/log/logger.hpp b/core/log/logger.hpp index 526a5c79ba..5804bc724f 100644 --- a/core/log/logger.hpp +++ b/core/log/logger.hpp @@ -31,6 +31,8 @@ namespace kagome::log { void tuneLoggingSystem(const std::vector &cfg); + void doLogRotate(); + static const std::string defaultGroupName("kagome"); [[nodiscard]] Logger createLogger(const std::string &tag);