2004-08-28 16:21:25 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
$Date$
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
@copy © Marc Wäckerlin
|
|
|
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
|
|
|
|
|
|
|
$Log$
|
2004-10-13 10:41:28 +00:00
|
|
|
Revision 1.4 2004/10/13 10:41:28 marc
|
|
|
|
no newline at the end of stack trace
|
|
|
|
|
2004-08-28 16:21:25 +00:00
|
|
|
Revision 1.3 2004/08/28 16:21:25 marc
|
|
|
|
mrw-c++-0.92 (mrw)
|
|
|
|
- new file: version.cpp
|
|
|
|
- new file header for all sources
|
|
|
|
- work around warning in mrw::auto<T>
|
|
|
|
- possibility to compile without log4cxx
|
|
|
|
- work around bugs in demangle.h and libiberty.h
|
|
|
|
- corrections in documentation
|
|
|
|
- added simple tracing mechanism
|
|
|
|
- more warnings
|
|
|
|
- small corrections in Auto<>::Free and a new test for it
|
|
|
|
- possibility to compile without stack trace
|
|
|
|
|
|
|
|
*/
|
2004-04-21 06:39:20 +00:00
|
|
|
#include <mrw/stacktrace.hpp>
|
|
|
|
#include <mrw/exception.hpp>
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace mrw {
|
|
|
|
|
2004-04-23 16:03:29 +00:00
|
|
|
/** @addtogroup StackTrace */
|
|
|
|
//@{
|
|
|
|
|
|
|
|
/** @defgroup AutoTrace Automated Unexpected Handler with Stack Trace
|
|
|
|
|
|
|
|
@brief Don't care about the unexpected handler, let the library
|
|
|
|
do all the repetitive work for you.
|
|
|
|
|
|
|
|
For all your programs it is recommended to implement an identical
|
|
|
|
unexpected handler, that rethrows, catches the @c
|
|
|
|
mrw::exception, @c std::exception and all unknown exceptions,
|
|
|
|
traces them and finally quits with a throw of a @c
|
|
|
|
atd::bad_exception. The only thing that may be different from
|
|
|
|
project to project is, how tracing is done. The MRW C++ Class
|
|
|
|
Library provides you with additional libraries you can link
|
|
|
|
to. By linking to the library, you get an unexpected handler for
|
|
|
|
free: You don't need to add a single line of code, just link to
|
|
|
|
one more library! The libraries differ in how tracing is done.
|
|
|
|
|
|
|
|
The Implementation is done with a static instance of a class that
|
|
|
|
sets the unexpected handler in the constructor.
|
|
|
|
|
|
|
|
@section trcstderr Trace using std::cerr
|
|
|
|
|
|
|
|
If you link to the library @c libmrwexcstderr using a linker
|
|
|
|
option such as: @c -lmrwexcstderr, then an unexpected handler is
|
|
|
|
registered, that traces to the standard error stream @c
|
|
|
|
std::cerr. You don't need to change a single line in your code!
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
/** @brief unexpected handler, that traces to @c std::cerr
|
|
|
|
|
|
|
|
The unexpected handler is installed automatically when you link
|
|
|
|
to @c -lmrwexcstderr. The implementation of this unexpected
|
|
|
|
handler is as follows:
|
|
|
|
|
|
|
|
@code
|
|
|
|
void unexpected_stderr() {
|
|
|
|
std::cerr<<"UNEXPECTED EXCEPTION: ----------------------------"<<std::endl;
|
|
|
|
try {
|
|
|
|
throw;
|
|
|
|
} catch (const mrw::exception& x) {
|
|
|
|
StackTrace::createSymtable();
|
|
|
|
std::cerr<<"---------- Reason:"<<std::endl
|
|
|
|
<<x.what()<<std::endl
|
|
|
|
<<"---------- Stack:"<<std::endl
|
2004-10-13 10:41:28 +00:00
|
|
|
<<x.stacktrace();
|
2004-04-23 16:03:29 +00:00
|
|
|
} catch (const std::exception& x) {
|
|
|
|
std::cerr<<"---------- Reason:"<<std::endl
|
|
|
|
<<x.what()<<std::endl
|
|
|
|
<<"---------- Stack: **** not available ****"<<std::endl;
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr<<"---------- Reason: **** not available ****"<<std::endl
|
|
|
|
<<"---------- Stack: **** not available ****"<<std::endl;
|
|
|
|
}
|
|
|
|
std::cerr<<"-------------------------------------------------"<<std::endl;
|
|
|
|
throw std::bad_exception();
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
*/
|
|
|
|
void unexpected_stderr() {
|
2004-04-21 06:39:20 +00:00
|
|
|
std::cerr<<"UNEXPECTED EXCEPTION: ----------------------------"<<std::endl;
|
|
|
|
try {
|
|
|
|
throw;
|
|
|
|
} catch (const mrw::exception& x) {
|
|
|
|
StackTrace::createSymtable();
|
|
|
|
std::cerr<<"---------- Reason:"<<std::endl
|
|
|
|
<<x.what()<<std::endl
|
|
|
|
<<"---------- Stack:"<<std::endl
|
2004-10-13 10:41:28 +00:00
|
|
|
<<x.stacktrace();
|
2004-04-21 06:39:20 +00:00
|
|
|
} catch (const std::exception& x) {
|
|
|
|
std::cerr<<"---------- Reason:"<<std::endl
|
|
|
|
<<x.what()<<std::endl
|
|
|
|
<<"---------- Stack: **** not available ****"<<std::endl;
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr<<"---------- Reason: **** not available ****"<<std::endl
|
|
|
|
<<"---------- Stack: **** not available ****"<<std::endl;
|
|
|
|
}
|
|
|
|
std::cerr<<"-------------------------------------------------"<<std::endl;
|
|
|
|
throw std::bad_exception();
|
|
|
|
}
|
|
|
|
|
2004-04-23 16:03:29 +00:00
|
|
|
//@}
|
|
|
|
//@}
|
|
|
|
|
2004-04-21 06:39:20 +00:00
|
|
|
class AutoStackTrace {
|
|
|
|
public:
|
|
|
|
AutoStackTrace() {
|
2004-04-23 16:03:29 +00:00
|
|
|
std::set_unexpected(&mrw::unexpected_stderr);
|
2004-04-21 06:39:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// initialize stack traces (load symbols)
|
|
|
|
static AutoStackTrace _autoStackTrace;
|
|
|
|
|
|
|
|
}
|