-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Provide way to gracefully terminate or use spdlog in signal handlers #1607
Comments
There are not many safe operations within the signal handler (see reference: std::signal - cppreference.com). One workaround is to use |
Thanks for the feedback :) I will add it to the todo list to implement in spdlog somehow (probably similar to @tt4g suggestion). |
Thanks for the suggestion @tt4g ! I will look into it. |
Could you please provide an example code snippet of how to implement logs flushing when segfault occurs? |
@qaler This is very simple example: #include <atomic>
#include <csignal>
#include <iostream>
#include "spdlog/spdlog.h"
static std::atomic<bool> signalReceived = false;
void signal_handler(int signal)
{
signalReceived = true;
}
int main()
{
std::signal(SIGINT, signal_handler);
for (;;) {
if (signalReceived) {
// Handle signal
spdlog::error("Signal!!!");
break;
} else {
// Main process.
std::cout << "Main\n";
}
}
} |
Hey,
First of all, thank your for all the work that you have put into this!
I was wondering what is the right way of handling the following situation: we are using the async logger and the process suddenly crashes without having the chance to flush the log messages to the file.
My first thought was to install a signal handler for
SIGSEGV
andSIGABRT
and callspdlog::shutdown()
from the respective handler. However, theshutdown
method does not seem to be async-safe since it usesstd::mutex
, meaning that this could cause deadlocks when the process crashes and that might be even worse than losing a few logs.Are there any other suggestions that could help us get around this issue?
Thanks
The text was updated successfully, but these errors were encountered: