The Gaudi Framework  master (37c0b60a)
GaudiException.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
12 
13 #include <GaudiKernel/System.h>
14 
15 bool GaudiException::s_proc( false );
16 static const bool enableBacktrace = System::isEnvSet( "ENABLE_BACKTRACE" );
17 
18 namespace {
19  inline std::string captureBacktrace( const StatusCode& code ) {
20  std::string backtrace;
21  if ( enableBacktrace && !code.isSuccess() ) System::backTrace( backtrace, 100, 1 );
22  return backtrace;
23  }
24 } // namespace
25 
27  : m_message( std::move( Message ) ), m_tag( std::move( Tag ) ), m_code( std::move( Code ) ) {
28  s_proc = true;
29  m_backTrace = captureBacktrace( m_code );
30 }
31 
33  : m_message( std::move( Message ) )
34  , m_tag( std::move( Tag ) )
35  , m_code( std::move( Code ) )
36  , m_previous( Exception.clone() ) {
37  // Do not capture backtrace in outer chained exceptions, so only innermost exception is printed
38 }
39 
41  : m_message( std::move( Message ) ), m_tag( std::move( Tag ) ), m_code( std::move( Code ) ) {
42  s_proc = true;
43  m_message += ": " + System::typeinfoName( typeid( Exception ) ) + ", " + Exception.what();
44  m_backTrace = captureBacktrace( m_code );
45 }
46 
48  : std::exception( Exception )
49  , m_message{ Exception.message() }
50  , m_tag{ Exception.tag() }
51  , m_code{ Exception.code() }
52  , m_backTrace{ Exception.backTrace() }
53  , m_previous{ Exception.previous() ? Exception.previous()->clone() : nullptr } {
54  s_proc = true;
55 }
56 
58 
60  m_message = Exception.message();
61  m_tag = Exception.tag();
62  m_code = Exception.code();
63  m_backTrace = Exception.backTrace();
64  m_previous.reset( Exception.previous() ? Exception.previous()->clone() : nullptr );
65  return *this;
66 }
67 
69  os << tag() << " \t " << message() << "\t StatusCode=" << code();
70  if ( !backTrace().empty() ) os << std::endl << "Exception stack trace\n" << backTrace();
71  return ( 0 != previous() ) ? previous()->printOut( os << std::endl ) : os;
72 }
73 
75  os << tag() << " \t " << message() << "\t StatusCode=" << code();
76  if ( !backTrace().empty() ) os << endmsg << "Exception stack trace\n" << backTrace();
77  return ( 0 != previous() ) ? previous()->printOut( os << endmsg ) : os;
78 }
79 
80 std::ostream& operator<<( std::ostream& os, const GaudiException& ge ) { return ge.printOut( os ); }
81 
83  return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
84 }
85 
86 MsgStream& operator<<( MsgStream& os, const GaudiException& ge ) { return ge.printOut( os ); }
87 
89  return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
90 }
std::string
STL class.
std::exception
STL class.
GaudiException::s_proc
static bool s_proc
"previous" element in the linked list
Definition: GaudiException.h:130
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
System.h
GaudiException.h
GaudiException
Definition: GaudiException.h:31
GaudiException::m_previous
std::unique_ptr< GaudiException > m_previous
stack trace at instantiation
Definition: GaudiException.h:129
GaudiException::previous
virtual GaudiException * previous() const
get the previous exception ( "previous" element in the linked list)
Definition: GaudiException.h:95
operator<<
std::ostream & operator<<(std::ostream &os, const GaudiException &ge)
Definition: GaudiException.cpp:80
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
System::backTrace
GAUDI_API int backTrace(void **addresses, const int depth)
std::unique_ptr::reset
T reset(T... args)
GaudiException::m_message
std::string m_message
Definition: GaudiException.h:125
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:68
GaudiException::operator=
GaudiException & operator=(const GaudiException &Exception)
assignment operator
Definition: GaudiException.cpp:59
GaudiException::backTrace
virtual const std::string & backTrace() const
return the stack trace at instantiation
Definition: GaudiException.h:98
StatusCode
Definition: StatusCode.h:65
Message
Definition: Message.h:26
std::ostream
STL class.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
GaudiException::tag
virtual const std::string & tag() const
name tag for the exception, or exception type
Definition: GaudiException.h:77
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:410
GaudiException::m_backTrace
std::string m_backTrace
status code for exception
Definition: GaudiException.h:128
GaudiException::m_tag
std::string m_tag
error message
Definition: GaudiException.h:126
MsgStream
Definition: MsgStream.h:33
GaudiException::code
virtual const StatusCode & code() const
StatusCode for Exception.
Definition: GaudiException.h:86
std::endl
T endl(T... args)
std
STL namespace.
GaudiException::printOut
virtual std::ostream & printOut(std::ostream &os=std::cerr) const
methods for overloaded printout to std::ostream& and MsgStream&
Definition: GaudiException.cpp:68
GaudiException::GaudiException
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1)
Definition: GaudiException.cpp:26
Gaudi::Details::Property::ParsingErrorPolicy::Exception
@ Exception
GaudiException::~GaudiException
virtual ~GaudiException()
destructor (perform the deletion of "previous" field!)
Definition: GaudiException.cpp:57
GaudiException::m_code
StatusCode m_code
exception tag
Definition: GaudiException.h:127