The Gaudi Framework  v33r1 (b1225454)
GaudiException.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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( 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( 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 
59  s_proc = false;
60 }
61 
63  m_message = Exception.message();
64  m_tag = Exception.tag();
65  m_code = Exception.code();
66  m_backTrace = Exception.backTrace();
67  m_previous.reset( Exception.previous() ? Exception.previous()->clone() : nullptr );
68  return *this;
69 }
70 
72  os << tag() << " \t " << message() << "\t StatusCode=" << code();
73  if ( !backTrace().empty() ) os << std::endl << "Exception stack trace\n" << backTrace();
74  return ( 0 != previous() ) ? previous()->printOut( os << std::endl ) : os;
75 }
76 
78  os << tag() << " \t " << message() << "\t StatusCode=" << code();
79  if ( !backTrace().empty() ) os << endmsg << "Exception stack trace\n" << backTrace();
80  return ( 0 != previous() ) ? previous()->printOut( os << endmsg ) : os;
81 }
82 
83 std::ostream& operator<<( std::ostream& os, const GaudiException& ge ) { return ge.printOut( os ); }
84 
86  return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
87 }
88 
89 MsgStream& operator<<( MsgStream& os, const GaudiException& ge ) { return ge.printOut( os ); }
90 
92  return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
93 }
GaudiException & operator=(const GaudiException &Exception)
assignment operator
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
Define general base for Gaudi exception.
virtual GaudiException * previous() const
get the previous exception ( "previous" element in the linked list)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
static bool s_proc
"previous" element in the linked list
T endl(T... args)
virtual const std::string & tag() const
name tag for the exception, or exception type
virtual const std::string & message() const
error message to be printed
STL namespace.
GAUDI_API int backTrace(void **addresses, const int depth)
std::string m_tag
error message
STL class.
std::string m_message
T what(T... args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
T reset(T... args)
bool isSuccess() const
Definition: StatusCode.h:365
STL class.
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:399
virtual const StatusCode & code() const
StatusCode for Exception.
The Message class.
Definition: Message.h:27
virtual GaudiException * clone() const
clone operation
std::ostream & operator<<(std::ostream &os, const GaudiException &ge)
overloaded printout to std::ostream
std::string m_backTrace
status code for exception
virtual const std::string & backTrace() const
return the stack trace at instantiation
virtual std::ostream & printOut(std::ostream &os=std::cerr) const
methods for overloaded printout to std::ostream& and MsgStream&
virtual ~GaudiException()
destructor (perform the deletion of "previous" field!)
StatusCode m_code
exception tag
STL class.
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:158
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::unique_ptr< GaudiException > m_previous
stack trace at instantiation
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1)