Add backtrace after segfault.

This commit is contained in:
Michał Słomkowski 2015-12-14 22:58:31 +01:00
parent d1d8c91f14
commit 804f01b162

View File

@ -8,7 +8,24 @@
#include <log4cpp/OstreamAppender.hh> #include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh> #include <log4cpp/PatternLayout.hh>
#include <execinfo.h>
/*
* Code from http://stackoverflow.com/a/77336/5419223
*/
static void sigsegv_handler(int sig) {
constexpr int STACK_DEPTH = 10;
void *array[STACK_DEPTH];
size_t size = backtrace(array, STACK_DEPTH);
fprintf(stderr, "ERROR: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
signal(SIGSEGV, sigsegv_handler);
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout); log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
log4cpp::PatternLayout layout; log4cpp::PatternLayout layout;