The Gaudi Framework  v33r0 (d5ea422b)
StatusCode.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 \***********************************************************************************/
11 #define GAUDIKERNEL_STATUSCODE_CPP 1
12 
13 #include "GaudiKernel/StatusCode.h"
14 #include "GaudiKernel/Bootstrap.h"
19 #include "GaudiKernel/MsgStream.h"
20 #include "GaudiKernel/System.h"
21 #include <exception>
22 
23 // statics
24 bool StatusCode::s_checking( false );
25 
29 
30 namespace {
32  struct DefaultCategory : public StatusCode::Category {
33 
34  const char* name() const override { return "Gaudi"; }
35 
36  std::string message( StatusCode::code_t code ) const override {
37  switch ( static_cast<StatusCode::ErrorCode>( code ) ) {
39  return "SUCCESS";
41  return "FAILURE";
43  return "RECOVERABLE";
44  default:
45  return "UNKNOWN(" + std::to_string( code ) + ")";
46  }
47  }
48  };
49 } // namespace
50 
52 
53 void StatusCode::enableChecking() { s_checking = true; }
54 
56 
58 
60 
61  if ( !m_checked && !GaudiException::s_proc && !std::uncaught_exceptions() ) {
62 
63  auto msg = Gaudi::svcLocator()->as<IMessageSvc>();
64  auto scs = Gaudi::svcLocator()->service<IStatusCodeSvc>( "StatusCodeSvc" );
65 
66  const size_t depth = 21;
67  void* addresses[depth];
68 
69  std::string lib, fnc;
70  void* addr = nullptr;
72  if ( System::backTrace( addresses, depth ) ) {
73 
74  for ( size_t idx : {2, 3} ) {
75  // When running address sanitizer builds with -fno-omit-frame-pointer
76  // StatusCode::check() might appear as the function name, so skip.
77  if ( System::getStackLevel( addresses[idx], addr, fnc, lib ) && fnc != "StatusCode::~StatusCode()" &&
78  fnc != "StatusCode::check()" ) {
79  if ( scs ) {
80  scs->regFnc( fnc, lib );
81  } else {
82  MsgStream log( msg, "StatusCode" );
83  log << MSG::WARNING << "Unchecked in " << fnc << " (" << lib << ")" << endmsg;
84  }
85  break;
86  }
87  }
88  }
89  }
90 }
91 
92 void StatusCode::i_doThrow( std::string message, std::string tag ) const {
93  throw GaudiException{std::move( message ), std::move( tag ), *this};
94 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
Define general base for Gaudi exception.
void check()
Do StatusCode check.
Definition: StatusCode.cpp:59
GAUDI_API bool getStackLevel(void *addresses, void *&addr, std::string &fnc, std::string &lib)
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:305
The category assigned to a StatusCode.
Definition: StatusCode.h:74
static bool s_proc
"previous" element in the linked list
constexpr static const auto RECOVERABLE
Definition: StatusCode.h:98
T to_string(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
virtual const char * name() const =0
Name of the category.
SmartIF< IFace > as()
Definition: ISvcLocator.h:113
static GAUDI_API bool checkingEnabled()
Definition: StatusCode.cpp:57
GAUDI_API int backTrace(void **addresses, const int depth)
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
GAUDI_API ISvcLocator * svcLocator()
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:82
#define STATUSCODE_ENUM_IMPL(...)
Assign a category to the StatusCode enum declared with STATUSCODE_ENUM_DECL( ENUM )
Definition: StatusCode.h:342
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:47
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:306
T move(T... args)
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:248
constexpr static const auto FAILURE
Definition: StatusCode.h:97
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:55
void i_doThrow(std::string message, std::string tag) const
Helper function to avoid circular dependency between GaudiException.h and StatusCode....
Definition: StatusCode.cpp:92
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:63