Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: log rotate #2251

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

62 changes: 50 additions & 12 deletions core/application/impl/app_state_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <functional>

namespace kagome::application {
std::atomic_bool AppStateManagerImpl::signals_enabled{false};
std::weak_ptr<AppStateManagerImpl> 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));
Expand All @@ -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)
Expand All @@ -43,24 +46,59 @@ namespace kagome::application {
sigaction(SIGQUIT, &act, nullptr);
}

std::weak_ptr<AppStateManagerImpl> 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};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move log-rotate to main() with RAII like backward::SignalHandling?


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();
}

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions core/application/impl/app_state_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppStateManagerImpl> 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_;
Expand Down
5 changes: 5 additions & 0 deletions core/log/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<soralog::LoggerFactory>(logging_system)
Expand Down
2 changes: 2 additions & 0 deletions core/log/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace kagome::log {

void tuneLoggingSystem(const std::vector<std::string> &cfg);

void doLogRotate();

static const std::string defaultGroupName("kagome");

[[nodiscard]] Logger createLogger(const std::string &tag);
Expand Down
Loading