admin管理员组文章数量:1391956
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.
本文标签: cAsynchronous logging and printfdebuggingStack Overflow
版权声明:本文标题:c++ - Asynchronous logging and printf-debugging - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723316a2621825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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