| The Gaudi Framework v32r1 (f65d50dc)

GaudiException.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/GaudiException.h"
2
3 #include "GaudiKernel/System.h"
4
5 bool GaudiException::s_proc( false );
6 static const bool enableBacktrace = System::isEnvSet( "ENABLE_BACKTRACE" );
7
8 namespace {
9 inline std::string captureBacktrace( const StatusCode& code ) {
10 std::string backtrace;
11 if ( enableBacktrace && !code.isSuccess() ) System::backTrace( backtrace, 100, 1 );
12 return backtrace;
13 }
14 } // namespace
15
16 GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code )
17 : m_message( std::move( Message ) ), m_tag( std::move( Tag ) ), m_code( std::move( Code ) ) {
18 s_proc = true;
19 m_backTrace = captureBacktrace( Code );
20 }
21
22 GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code, const GaudiException& Exception )
23 : m_message( std::move( Message ) )
24 , m_tag( std::move( Tag ) )
25 , m_code( std::move( Code ) )
26 , m_previous( Exception.clone() ) {
27 // Do not capture backtrace in outer chained exceptions, so only innermost exception is printed
28 }
29
30 GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code, const std::exception& Exception )
31 : m_message( std::move( Message ) ), m_tag( std::move( Tag ) ), m_code( std::move( Code ) ) {
32 s_proc = true;
33 m_message += ": " + System::typeinfoName( typeid( Exception ) ) + ", " + Exception.what();
34 m_backTrace = captureBacktrace( Code );
35 }
36
37 GaudiException::GaudiException( const GaudiException& Exception )
38 : std::exception( Exception )
39 , m_message{Exception.message()}
40 , m_tag{Exception.tag()}
41 , m_code{Exception.code()}
42 , m_backTrace{Exception.backTrace()}
43 , m_previous{Exception.previous() ? Exception.previous()->clone() : nullptr} {
44 s_proc = true;
45 }
46
47 GaudiException::~GaudiException() throw() {
48 m_code.setChecked();
49 s_proc = false;
50 }
51
52 GaudiException& GaudiException::operator=( const GaudiException& Exception ) {
53 m_message = Exception.message();
54 m_tag = Exception.tag();
55 m_code = Exception.code();
56 m_backTrace = Exception.backTrace();
57 m_previous.reset( Exception.previous() ? Exception.previous()->clone() : nullptr );
58 return *this;
59 }
60
61 std::ostream& GaudiException::printOut( std::ostream& os ) const {
62 os << tag() << " \t " << message() << "\t StatusCode=" << code();
63 if ( !backTrace().empty() ) os << std::endl << "Exception stack trace\n" << backTrace();
64 return ( 0 != previous() ) ? previous()->printOut( os << std::endl ) : os;
65 }
66
67 MsgStream& GaudiException::printOut( MsgStream& os ) const {
68 os << tag() << " \t " << message() << "\t StatusCode=" << code();
69 if ( !backTrace().empty() ) os << endmsg << "Exception stack trace\n" << backTrace();
70 return ( 0 != previous() ) ? previous()->printOut( os << endmsg ) : os;
71 }
72
73 std::ostream& operator<<( std::ostream& os, const GaudiException& ge ) { return ge.printOut( os ); }
74
75 std::ostream& operator<<( std::ostream& os, const GaudiException* pge ) {
76 return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
77 }
78
79 MsgStream& operator<<( MsgStream& os, const GaudiException& ge ) { return ge.printOut( os ); }
80
81 MsgStream& operator<<( MsgStream& os, const GaudiException* pge ) {
82 return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
83 }
System.h
GaudiException::operator=
GaudiException & operator=(const GaudiException &Exception)
assignment operator
Definition: GaudiException.cpp:52
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
GaudiException::previous
virtual GaudiException * previous() const
get the previous exception ( "previous" element in the linked list)
Definition: GaudiException.h:85
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
GaudiException::s_proc
static bool s_proc
"previous" element in the linked list
Definition: GaudiException.h:108
std::endl
T endl(T... args)
GaudiException::tag
virtual const std::string & tag() const
name tag for the exception, or exception type
Definition: GaudiException.h:67
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:58
std
STL namespace.
System::backTrace
GAUDI_API int backTrace(void **addresses, const int depth)
GaudiException::m_tag
std::string m_tag
error message
Definition: GaudiException.h:104
std::string
STL class.
GaudiException::m_message
std::string m_message
Definition: GaudiException.h:103
std::exception::what
T what(T... args)
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiException.h
std::unique_ptr::reset
T reset(T... args)
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
std::exception
STL class.
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:389
GaudiException::code
virtual const StatusCode & code() const
StatusCode for Exception.
Definition: GaudiException.h:76
Message
The Message class.
Definition: Message.h:17
GaudiException::clone
virtual GaudiException * clone() const
clone operation
Definition: GaudiException.h:97
operator<<
std::ostream & operator<<(std::ostream &os, const GaudiException &ge)
overloaded printout to std::ostream
Definition: GaudiException.cpp:73
GaudiException::m_backTrace
std::string m_backTrace
status code for exception
Definition: GaudiException.h:106
GaudiException::backTrace
virtual const std::string & backTrace() const
return the stack trace at instantiation
Definition: GaudiException.h:88
GaudiException::printOut
virtual std::ostream & printOut(std::ostream &os=std::cerr) const
methods for overloaded printout to std::ostream& and MsgStream&
Definition: GaudiException.cpp:61
GaudiException::~GaudiException
virtual ~GaudiException()
destructor (perform the deletion of "previous" field!)
Definition: GaudiException.cpp:47
GaudiException::m_code
StatusCode m_code
exception tag
Definition: GaudiException.h:105
std::ostream
STL class.
StatusCode::setChecked
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:143
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
GaudiException::m_previous
std::unique_ptr< GaudiException > m_previous
stack trace at instantiation
Definition: GaudiException.h:107
GaudiException::GaudiException
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1)
Definition: GaudiException.cpp:16