Apache Log4cxx  Version 1.3.0
Loading...
Searching...
No Matches
Stacktrace Support

When debugging a code base and an assertion is hit, it is often useful to have a stacktrace as part of an assertion in order for you to tell where you are in the code to know why it is buggy. Generating a stacktrace can be done with Boost Stacktrace, or using the stacktrace header if you are using a C++23 compatible compiler.

In order to enable stacktraces when using the LOG4CXX_ASSERT family of macros, simply define LOG4CXX_ENABLE_STACKTRACE in your buildsystem. If you are using a compiler that does not support C++17 and the __has_include macro, Boost Stacktrace must be installed and available on your system. If your compiler supports the __has_include macro, then it will search for Boost Stacktrace, followed by searching for <stacktrace>. Both implementations will insert an entry into the MDC named stacktrace that may then be inserted into log statements. When using the PatternLayout, this may be accomplished by using the %X{stacktrace} conversion pattern.

Putting the stacktrace into the MDC

If you want a stacktrace in any part of your code(not just on assertions), the following snippet of code may be used to insert a stacktrace into the current MDC:

::log4cxx::MDC mdc_("stacktrace", LOG4CXX_EOL + boost::stacktrace::to_string(boost::stacktrace::stacktrace()));
A Mapped Diagnostic Context, or MDC in short, is an instrument for distinguishing interleaved log out...
Definition: mdc.h:46
#define LOG4CXX_EOL
Definition: logstring.h:68

This may be inserted at any point in your application, giving you access to the current stacktrace in any log statement, not just in assert statements.