打印日志4CXX异常堆栈跟踪

Print Log4CXX exception stacktrace

本文关键字:堆栈 跟踪 异常 4CXX 日志 打印      更新时间:2023-10-16

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

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

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

首先添加一个处理程序函数:

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 上转储堆栈跟踪。