25 #if( ( defined( __linux__ ) && defined( __GNUC__ ) ) && !defined( __ANDROID__ ) || defined( __APPLE__ ) )
26 #define BACKTRACE_SUPPORTED
29 #ifdef BACKTRACE_SUPPORTED
41 #include <boost/algorithm/string.hpp>
43 #include "WException.h"
64 std::cerr <<
m_headlineColor( std::string(
"Exception thrown! Callstack's backtrace:" ) ) << std::endl <<
getBacktrace() << std::endl;
81 std::cerr <<
m_headlineColor( std::string(
"Exception thrown! Callstack's backtrace:" ) ) << std::endl <<
getBacktrace() << std::endl;
98 std::string result(
what() );
100 std::list< std::string >::const_iterator citer;
102 result +=
"trace: " + *citer +
"\n";
103 boost::trim( result );
110 std::ostringstream o;
112 #ifdef BACKTRACE_SUPPORTED
116 const size_t maxDepth = 100;
118 void* stackAddrs[maxDepth];
122 stackDepth = backtrace( stackAddrs, maxDepth );
123 stackSymbols = backtrace_symbols( stackAddrs, stackDepth );
126 for(
size_t i = 1; i < stackDepth; ++i )
130 size_t functionLength = 512;
131 char*
function =
new char[functionLength];
138 for(
char* j = stackSymbols[i]; *j; ++j )
158 char* ret = abi::__cxa_demangle( begin,
function, &functionLength, &status );
168 std::strncpy(
function, begin, functionLength );
169 std::strncat(
function,
"()", functionLength );
170 function[functionLength-1] =
'\0';
191 free( stackSymbols );
193 o <<
"Backtrace not supported on your platform. Currently just works on Linux and MacOS with GCC. Sorry!" << std::endl
194 <<
"Message was: " <<
what();
static bool noBacktrace
True if the backtrace should NOT be printed.
WTerminalColor m_headlineColor
Color used for exception headline.
std::list< std::string > m_trace
Stack trace for identifying the source where this exception came from.
static void disableBacktrace()
Function disables backtraces.
WException(const std::string &msg=std::string())
Default constructor.
std::string m_msg
Message given during throw.
std::string getBacktrace() const
Returns a call stacktrace.
WTerminalColor m_functionColor
Color used for function name.
virtual ~WException()
Destructor.
virtual const char * what() const
Returns the message string set on throw.
WTerminalColor m_symbolColor
Color used for symbols.
WTerminalColor m_labelColor
Color used for the "trace:" label.
std::string getTrace() const
Prints the trace of the call chain which caused this exception.
Helper class to provide a convenient way to colorize output on the console.