92 lines
2.7 KiB
C++
92 lines
2.7 KiB
C++
#include <mrw/stacktrace.hpp>
|
|
#include <mrw/exception.hpp>
|
|
#include <exception>
|
|
#include <log4cxx/logger.h>
|
|
|
|
namespace mrw {
|
|
|
|
/** @addtogroup StackTrace */
|
|
//@{
|
|
|
|
/** @addtogroup AutoTrace
|
|
|
|
@section trclog4cxx Trace using the log4cxx Library
|
|
|
|
If you link to the library @c libmrwexclog4cxx using a linker
|
|
option such as: @c -lmrwexclog4cxx, then an unexpected handler
|
|
is registered, that traces a fatal error using the log4cxx
|
|
library. You don't need to change a single line in your code!
|
|
|
|
The log4cxx library is located at:
|
|
- http://logging.apache.org/log4cxx
|
|
|
|
@note The configurator is not installed automatically. If you
|
|
want to trace e.g. on the console, you have to call @c
|
|
log4cxx::BasicConfigurator::configure(); as first statement in
|
|
your @c main().
|
|
*/
|
|
//@{
|
|
|
|
/// The log4cxx logger where the trace is written to.
|
|
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("libmrw"));
|
|
|
|
/** @brief unexpected handler, that traces using log4cxx
|
|
|
|
The unexpected handler is installed automatically when you link
|
|
to @c -lmrwexclog4cxx. The implementation of this unexpected
|
|
handler is as follows:
|
|
|
|
@code
|
|
void unexpected_log4cxx() {
|
|
logger->fatal("Unexpected Exception", __FILE__, __LINE__);
|
|
try {
|
|
throw;
|
|
} catch (const mrw::exception& x) {
|
|
StackTrace::createSymtable();
|
|
logger->fatal(std::string("Reason:\n")+x.what()
|
|
+"\nStack:+\n"+x.stacktrace());
|
|
} catch (const std::exception& x) {
|
|
logger->fatal(std::string("Reason:\n")+x.what()
|
|
+"\nStack: **** not available ****");
|
|
} catch (...) {
|
|
logger->fatal(std::string("Reason: **** not available ****\n")
|
|
+"\nStack: **** not available ****");
|
|
}
|
|
throw std::bad_exception();
|
|
}
|
|
@endcode
|
|
|
|
*/
|
|
void unexpected_log4cxx() {
|
|
logger->fatal("Unexpected Exception", __FILE__, __LINE__);
|
|
try {
|
|
throw;
|
|
} catch (const mrw::exception& x) {
|
|
StackTrace::createSymtable();
|
|
logger->fatal(std::string("Reason:\n")+x.what()
|
|
+"\nStack:+\n"+x.stacktrace());
|
|
} catch (const std::exception& x) {
|
|
logger->fatal(std::string("Reason:\n")+x.what()
|
|
+"\nStack: **** not available ****");
|
|
} catch (...) {
|
|
logger->fatal(std::string("Reason: **** not available ****\n")
|
|
+"\nStack: **** not available ****");
|
|
}
|
|
throw std::bad_exception();
|
|
}
|
|
|
|
//@}
|
|
//@}
|
|
|
|
class AutoStackTrace {
|
|
public:
|
|
AutoStackTrace() {
|
|
std::set_unexpected(&mrw::unexpected_log4cxx);
|
|
}
|
|
};
|
|
|
|
// initialize stack traces (load symbols)
|
|
static AutoStackTrace _autoStackTrace;
|
|
|
|
}
|