The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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
15bool GaudiException::s_proc( false );
16static const bool enableBacktrace = System::isEnvSet( "ENABLE_BACKTRACE" );
17
18namespace {
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
26GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code )
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
32GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code, const GaudiException& Exception )
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
40GaudiException::GaudiException( std::string Message, std::string Tag, StatusCode Code, const std::exception& Exception )
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
68std::ostream& GaudiException::printOut( std::ostream& os ) const {
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
80std::ostream& operator<<( std::ostream& os, const GaudiException& ge ) { return ge.printOut( os ); }
81
82std::ostream& operator<<( std::ostream& os, const GaudiException* pge ) {
83 return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
84}
85
86MsgStream& operator<<( MsgStream& os, const GaudiException& ge ) { return ge.printOut( os ); }
87
89 return ( 0 == pge ) ? ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
90}
std::ostream & operator<<(std::ostream &os, const GaudiException &ge)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
virtual const std::string & message() const
error message to be printed
static bool s_proc
"previous" element in the linked list
virtual const StatusCode & code() const
StatusCode for Exception.
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1)
virtual std::ostream & printOut(std::ostream &os=std::cerr) const
methods for overloaded printout to std::ostream& and MsgStream&
std::string m_message
virtual ~GaudiException()
destructor (perform the deletion of "previous" field!)
StatusCode m_code
exception tag
friend class StatusCode
virtual GaudiException * clone() const
clone operation
std::unique_ptr< GaudiException > m_previous
stack trace at instantiation
std::string m_tag
error message
std::string m_backTrace
status code for exception
GaudiException & operator=(const GaudiException &Exception)
assignment operator
virtual const std::string & backTrace() const
return the stack trace at instantiation
virtual GaudiException * previous() const
get the previous exception ( "previous" element in the linked list)
virtual const std::string & tag() const
name tag for the exception, or exception type
The Message class.
Definition Message.h:25
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isSuccess() const
Definition StatusCode.h:314
GAUDI_API int backTrace(void **addresses, const int depth)
Definition System.cpp:372
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition System.cpp:349
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260
STL namespace.