I use async logging and, in cases where I cannot debug with debugger attached, I'm forced to update my code to flush logs on each statement. Something like
LOG("something"); LOG_FLUSH();
do_something();
LOG("else"); LOG_FLUSH();
do_something_else();
LOG("done); LOG_FLUSH();
Basically, after each log statement I need to add code to make it flush logs. This way when it crashes I know what was the last log.
What options do I have to "intercept" crashing, so that my async logging thread flushes the logs before allowing "crashing" to continue?
I mainly develop on windows (visual studio) or msys2 (clang, gcc). Msft libc has some hooks for that, like _set_invalid_parameter_handler, there is also SetUnhandledExceptionFilter WinAPI. However, I think most of these should have issues with googletest/boost::test and other testing frameworks which supposedly have to set such handlers.
So, what do async logging libs usually to do avoid losing logs when crashing, and how they handle testing frameworks?
I use async logging and, in cases where I cannot debug with debugger attached, I'm forced to update my code to flush logs on each statement. Something like
LOG("something"); LOG_FLUSH();
do_something();
LOG("else"); LOG_FLUSH();
do_something_else();
LOG("done); LOG_FLUSH();
Basically, after each log statement I need to add code to make it flush logs. This way when it crashes I know what was the last log.
What options do I have to "intercept" crashing, so that my async logging thread flushes the logs before allowing "crashing" to continue?
I mainly develop on windows (visual studio) or msys2 (clang, gcc). Msft libc has some hooks for that, like _set_invalid_parameter_handler, there is also SetUnhandledExceptionFilter WinAPI. However, I think most of these should have issues with googletest/boost::test and other testing frameworks which supposedly have to set such handlers.
So, what do async logging libs usually to do avoid losing logs when crashing, and how they handle testing frameworks?
Share Improve this question asked Mar 13 at 1:46 Pavel PPavel P 17k11 gold badges90 silver badges141 bronze badges 5 |1 Answer
Reset to default 2So, what do async logging libs usually to do avoid losing logs when crashing
Proceeding with flushing the logs (or with any other activity for that matter) after some thread has crashed is never safe. You never know whether critical memory areas like heap management structures are corrupted and whether standard library functions would work as expected.
A (proprietary) async logging library I've seen didn't even bother intercepting the crash. Instead, it stored queued messages in a kind of circular buffer in memory, and provided a tool that can scan a core dump file, locate that message queue in the dumped process memory, and append those unflushed messages to the log file. In other words, it did the flushing post mortem and didn't interfere with any testing framework.
Such technique was enough for debugging, but obviously it isn't very convenient for the end users.
gtest_catch_exceptions=0
, so I guess you can still use your hooks. This answer also suggests using mmap for logging to be able to flush each line without performance cut. – pptaszni Commented Mar 13 at 7:01LOG("something"); printf("%s", 123);
the logger thread likely won't even have a chance to react to the new log message when the producer thread crashes on the printf right after making the log – Pavel P Commented Mar 13 at 10:56