Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GslSvc.cpp
Go to the documentation of this file.
1 // Include files
2 // from Gaudi
4 #include "GaudiKernel/IToolSvc.h"
6 #include "GaudiKernel/System.h"
7 // STD & STL
8 #include <algorithm>
9 #include <functional>
10 // GaudiGSL
11 #include "GaudiGSL/GslError.h"
13 // local
14 #include "GaudiGSL/GaudiGSL.h"
16 #include "GslSvc.h"
17 // gsl
18 #include "gsl/gsl_errno.h"
19 
20 // ============================================================================
28 // ============================================================================
30 // ============================================================================
36 // ============================================================================
37 StatusCode GslSvc::initialize() {
38  // initialize the base class
40  if ( sc.isFailure() ) {
41  error() << " Error in initialization of base class 'Service'" << endmsg;
42  return sc;
43  }
44  // activate the static accessor to the service
45  GaudiGSL::setGslSvc( this );
46  // set the error handlers
47  if ( "GSL" == m_errorPolicy ) { /* nothing to do */
48  } else if ( "Off" == m_errorPolicy ) {
49  gsl_set_error_handler_off();
50  } else if ( "Abort" == m_errorPolicy ) {
51  gsl_set_error_handler( nullptr );
52  } else if ( "Ignore" == m_errorPolicy ) {
53  gsl_set_error_handler( GslErrorHandlers::ignoreTheError );
54  } else if ( "Exception" == m_errorPolicy ) {
55  gsl_set_error_handler( GslErrorHandlers::throwException );
56  } else if ( "Handle" == m_errorPolicy ) {
57  gsl_set_error_handler( GslErrorHandlers::handleTheError );
58  } else {
59  error() << " Unknown Error policy '" << m_errorPolicy << "'"
60  << " Valid policies: "
61  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]" << endmsg;
62  return StatusCode::FAILURE;
63  }
66  GslErrorHandler handler = gsl_set_error_handler( nullptr );
67  gsl_set_error_handler( handler );
68  if ( handler ) {
69  verbose() << " GSL Error Handler is '" << System::typeinfoName( typeid( *handler ) ) << "'" << endmsg;
70  } else {
71  info() << " Error Handler is NULL" << endmsg;
72  }
73 
74  if ( !m_handlersTypeNames.empty() ) {
76  auto toolsvc = serviceLocator()->service<IToolSvc>( "ToolSvc" );
77  if ( !toolsvc ) {
78  error() << " Could not locate Tool Service! " << endmsg;
79  return StatusCode::FAILURE;
80  }
81  for ( const auto& it : m_handlersTypeNames ) {
82  auto pos = it.find( '/' );
83  IGslErrorHandler* eh = nullptr;
84  if ( pos != std::string::npos ) {
85  sc = toolsvc->retrieveTool( it.substr( 0, pos ), it.substr( pos + 1 ), eh, this );
86  } else {
87  sc = toolsvc->retrieveTool( it, it, eh, this );
88  }
89  if ( sc.isFailure() ) {
90  error() << " Could not retrieve tool '" << it << "'" << endmsg;
91  return sc;
92  }
93  if ( !eh ) {
94  error() << " Could not retrieve tool '" << it << "'" << endmsg;
95  return StatusCode::FAILURE;
96  }
97  m_handlers.push_back( eh );
98  }
99  }
100  //
101  return StatusCode::SUCCESS;
102 }
103 // ============================================================================
104 
105 // ============================================================================
111 // ============================================================================
113  debug() << "==> Finalize" << endmsg;
114 
115  // deactivate the static accessor to the service
116  GaudiGSL::setGslSvc( nullptr );
117 
118  // finalize the base class
119  return Service::finalize();
120 }
121 // ============================================================================
122 
123 // ============================================================================
127 // ============================================================================
129  GslErrorHandler hh = gsl_set_error_handler( nullptr );
130  gsl_set_error_handler( hh );
131  return hh;
132 }
133 // ============================================================================
134 
135 // ============================================================================
140 // ============================================================================
142  gsl_set_error_handler( handler );
143  {
144  debug() << " New GSL handler is set '" << ( handler ? System::typeinfoName( typeid( handler ) ) : "NULL" ) << "'"
145  << endmsg;
146  }
147  return handler;
148 }
149 // ============================================================================
150 
151 // ============================================================================
156 // ============================================================================
157 StatusCode GslSvc::status( const int error ) const {
158  if ( GSL_SUCCESS == error ) { return StatusCode::SUCCESS; }
159  StatusCode sc( error );
160  if ( sc.isSuccess() ) { return StatusCode::FAILURE; }
161  return sc;
162 }
163 // ============================================================================
164 
165 // ============================================================================
170 // ============================================================================
173  // code to be ignored?
174  if ( m_ignore.end() != std::find( m_ignore.begin(), m_ignore.end(), error.code ) ) { return sc; }
175  // invoke all handlers
176  for ( auto handler = m_handlers.begin(); sc.isSuccess() && m_handlers.end() != handler; ++handler ) {
177  sc = ( *handler )->handle( error );
178  }
179  //
180  return sc;
181 }
182 // ============================================================================
183 
184 // ============================================================================
185 // The END
186 // ============================================================================
GAUDI_API void handleTheError(const char *reason, const char *file, int line, int code)
The simplest Gsl Error handler, It delegates the actual error handling to GSL Service.
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
StatusCode initialize() override
Definition: Service.cpp:60
Helper class to represent GSL errors.
Definition: GslError.h:16
StatusCode finalize() override
Definition: Service.cpp:164
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
int code
error code (GSL)
Definition: GslError.h:25
bool isSuccess() const
Definition: StatusCode.h:267
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:37
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
The abstract interface for arbitrary GSL error handler.
Gaudi::Property< std::vector< int > > m_ignore
Definition: GslSvc.h:137
T end(T...args)
GAUDI_API void throwException(const char *reason, const char *file, int line, int code)
The simple Gsl Error handler, it throwns the Gaudi Exception.
bool isFailure() const
Definition: StatusCode.h:130
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:112
#define DECLARE_COMPONENT(type)
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:141
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:26
The implementation of IGslSvc interface.
Definition: GslSvc.h:83
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:171
T find(T...args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T...args)
GslErrorHandler handler() const override
retrieve the current GSL error handler
Definition: GslSvc.cpp:128
constexpr static const auto FAILURE
Definition: StatusCode.h:86
std::vector< IGslErrorHandler * > m_handlers
Definition: GslSvc.h:139
GAUDI_API void ignoreTheError(const char *reason, const char *file, int line, int code)
The simplest Gsl Error handler, It simply ingnores the error.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
StatusCode status(const int error) const override
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:157