打印Log4CXX异常堆栈跟踪

时间:2016-07-18 09:55:00

标签: c++ logging log4cxx

使用Log4CXX_ERROR我只能打印e.what()。

catch (const std::exception e)
{
    logger->error("exception:" << e.what());
    //logger->error("exception:" << e); //not allowed
}

如何使用log4cxx打印异常堆栈跟踪?

1 个答案:

答案 0 :(得分:1)

首先添加处理函数:

void trace() {
    void *array[20];
    size_t size;

    /* store up to 20 return address of the current program state in array
       and return the exact number of values stored */
    size = (size_t)backtrace(array, 20);

    /* return names of functions from the backtrace list in array and
       write result immediately to stderr */
    backtrace_symbols_fd(array, size, STDERR_FILENO);
}

然后调用此函数在stderr上转储堆栈跟踪。