The Gaudi Framework  v30r3 (a5ef0a68)
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 {
39  // initialize the base class
41  if ( sc.isFailure() ) {
42  error() << " Error in initialization of base class 'Service'" << endmsg;
43  return sc;
44  }
45  // activate the static accessor to the service
46  GaudiGSL::setGslSvc( this );
47  // set the error handlers
48  if ( "GSL" == m_errorPolicy ) { /* nothing to do */
49  } else if ( "Off" == m_errorPolicy ) {
50  gsl_set_error_handler_off();
51  } else if ( "Abort" == m_errorPolicy ) {
52  gsl_set_error_handler( nullptr );
53  } else if ( "Ignore" == m_errorPolicy ) {
54  gsl_set_error_handler( GslErrorHandlers::ignoreTheError );
55  } else if ( "Exception" == m_errorPolicy ) {
56  gsl_set_error_handler( GslErrorHandlers::throwException );
57  } else if ( "Handle" == m_errorPolicy ) {
58  gsl_set_error_handler( GslErrorHandlers::handleTheError );
59  } else {
60  error() << " Unknown Error policy '" << m_errorPolicy << "'"
61  << " Valid policies: "
62  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]" << endmsg;
63  return StatusCode::FAILURE;
64  }
67  GslErrorHandler handler = gsl_set_error_handler( nullptr );
68  gsl_set_error_handler( handler );
69  if ( handler ) {
70  verbose() << " GSL Error Handler is '" << System::typeinfoName( typeid( *handler ) ) << "'" << endmsg;
71  } else {
72  info() << " Error Handler is NULL" << endmsg;
73  }
74 
75  if ( !m_handlersTypeNames.empty() ) {
77  auto toolsvc = serviceLocator()->service<IToolSvc>( "ToolSvc" );
78  if ( !toolsvc ) {
79  error() << " Could not locate Tool Service! " << endmsg;
80  return StatusCode::FAILURE;
81  }
82  for ( const auto& it : m_handlersTypeNames ) {
83  auto pos = it.find( '/' );
84  IGslErrorHandler* eh = nullptr;
85  if ( pos != std::string::npos ) {
86  sc = toolsvc->retrieveTool( it.substr( 0, pos ), it.substr( pos + 1 ), eh, this );
87  } else {
88  sc = toolsvc->retrieveTool( it, it, eh, this );
89  }
90  if ( sc.isFailure() ) {
91  error() << " Could not retrieve tool '" << it << "'" << endmsg;
92  return sc;
93  }
94  if ( !eh ) {
95  error() << " Could not retrieve tool '" << it << "'" << endmsg;
96  return StatusCode::FAILURE;
97  }
98  m_handlers.push_back( eh );
99  }
100  }
101  //
102  return StatusCode::SUCCESS;
103 }
104 // ============================================================================
105 
106 // ============================================================================
112 // ============================================================================
114 {
115  debug() << "==> Finalize" << endmsg;
116 
117  // deactivate the static accessor to the service
118  GaudiGSL::setGslSvc( nullptr );
119 
120  // finalize the base class
121  return Service::finalize();
122 }
123 // ============================================================================
124 
125 // ============================================================================
129 // ============================================================================
131 {
132  GslErrorHandler hh = gsl_set_error_handler( nullptr );
133  gsl_set_error_handler( hh );
134  return hh;
135 }
136 // ============================================================================
137 
138 // ============================================================================
143 // ============================================================================
145 {
146  gsl_set_error_handler( handler );
147  {
148  debug() << " New GSL handler is set '" << ( handler ? System::typeinfoName( typeid( handler ) ) : "NULL" ) << "'"
149  << endmsg;
150  }
151  return handler;
152 }
153 // ============================================================================
154 
155 // ============================================================================
160 // ============================================================================
161 StatusCode GslSvc::status( const int error ) const
162 {
163  if ( GSL_SUCCESS == error ) {
164  return StatusCode::SUCCESS;
165  }
166  StatusCode sc( error );
167  if ( sc.isSuccess() ) {
168  return StatusCode::FAILURE;
169  }
170  return sc;
171 }
172 // ============================================================================
173 
174 // ============================================================================
179 // ============================================================================
181 {
183  // code to be ignored?
184  if ( m_ignore.end() != std::find( m_ignore.begin(), m_ignore.end(), error.code ) ) {
185  return sc;
186  }
187  // invoke all handlers
188  for ( auto handler = m_handlers.begin(); sc.isSuccess() && m_handlers.end() != handler; ++handler ) {
189  sc = ( *handler )->handle( error );
190  }
191  //
192  return sc;
193 }
194 // ============================================================================
195 
196 // ============================================================================
197 // The END
198 // ============================================================================
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
constexpr static const auto FAILURE
Definition: StatusCode.h:88
StatusCode initialize() override
Definition: Service.cpp:63
Helper class to represent GSL errors.
Definition: GslError.h:16
StatusCode finalize() override
Definition: Service.cpp:173
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
int code
error code (GSL)
Definition: GslError.h:26
bool isSuccess() const
Definition: StatusCode.h:287
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:38
The abstract interface for arbitrary GSL error handler.
Gaudi::Property< std::vector< int > > m_ignore
Definition: GslSvc.h:138
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:139
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:113
#define DECLARE_COMPONENT(type)
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:144
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
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
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:180
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:130
std::vector< IGslErrorHandler * > m_handlers
Definition: GslSvc.h:140
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:209
StatusCode status(const int error) const override
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:161