GslSvc.cpp
Go to the documentation of this file.
1 // Include files
2 // from Gaudi
4 #include "GaudiKernel/System.h"
6 #include "GaudiKernel/IToolSvc.h"
7 // STD & STL
8 #include <algorithm>
9 #include <functional>
10 // GaudiGSL
12 #include "GaudiGSL/GslError.h"
13 // local
14 #include "GaudiGSL/GaudiGSL.h"
15 #include "GslSvc.h"
17 // gsl
18 #include "gsl/gsl_errno.h"
19 
20 // ============================================================================
28 // ============================================================================
30 // ============================================================================
31 
32 // ============================================================================
37 // ============================================================================
38 GslSvc::GslSvc( const std::string& name ,
39  ISvcLocator* svc )
40  : base_class ( name , svc )
41 {
42  declareProperty( "ErrorPolicy" , m_errorPolicy ) ;
43  declareProperty( "Handlers" , m_handlersTypeNames ) ;
44  declareProperty( "IgnoreCodes" , m_ignore ) ;
45 }
46 // ============================================================================
47 // ============================================================================
48 
49 // ============================================================================
55 // ============================================================================
57 {
58  // initialize the base class
60  if( sc.isFailure() )
61  { error()
62  << " Error in initialization of base class 'Service'"<< endmsg;
63  return sc;
64  }
65  // activate the static accessor to the service
66  GaudiGSL::setGslSvc( this );
67  // set the error handlers
68  if ( "GSL" == m_errorPolicy ) { /* nothing to do */ }
69  else if ( "Off" == m_errorPolicy )
70  { gsl_set_error_handler_off() ; }
71  else if ( "Abort" == m_errorPolicy )
72  { gsl_set_error_handler ( nullptr ) ; }
73  else if ( "Ignore" == m_errorPolicy )
74  { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; }
75  else if ( "Exception" == m_errorPolicy )
76  { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; }
77  else if ( "Handle" == m_errorPolicy )
78  { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; }
79  else
80  { error()
81  << " Unknown Error policy '" << m_errorPolicy << "'"
82  << " Valid policies: "
83  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]"
84  << endmsg;
85  return StatusCode::FAILURE ;
86  }
89  GslErrorHandler handler = gsl_set_error_handler( nullptr );
90  gsl_set_error_handler( handler );
91  if( handler )
92  { verbose()
93  << " GSL Error Handler is '"
94  << System::typeinfoName( typeid(*handler) ) << "'"
95  << endmsg; }
96  else { info() << " Error Handler is NULL" << endmsg ; }
97 
98  if( !m_handlersTypeNames.empty() )
99  {
101  auto toolsvc = serviceLocator()->service<IToolSvc>("ToolSvc");
102  if (!toolsvc) {
103  error() << " Could not locate Tool Service! " << endmsg;
104  return StatusCode::FAILURE;
105  }
106  for( const auto& it : m_handlersTypeNames )
107  {
108  auto pos = it.find('/');
109  IGslErrorHandler* eh = nullptr ;
110  if( pos != std::string::npos ) {
111  sc = toolsvc->retrieveTool
112  ( it.substr( 0 , pos ), it.substr( pos + 1 ), eh , this ) ;
113  } else {
114  sc = toolsvc->retrieveTool( it , it , eh , this ) ;
115  }
116  if( sc.isFailure() )
117  { error()
118  << " Could not retrieve tool '" << it << "'"<< endmsg ;
119  return sc ; }
120  if( !eh )
121  { error()
122  << " Could not retrieve tool '" << it << "'"<< endmsg ;
123  return StatusCode::FAILURE ; }
124  m_handlers.push_back( eh );
125  }
126  }
127  //
128  return StatusCode::SUCCESS;
129 }
130 // ============================================================================
131 
132 // ============================================================================
138 // ============================================================================
140 {
141  debug() << "==> Finalize" << endmsg;
142 
143  // deactivate the static accessor to the service
144  GaudiGSL::setGslSvc( nullptr );
145 
146  // finalize the base class
147  return Service::finalize() ;
148 }
149 // ============================================================================
150 
151 // ============================================================================
155 // ============================================================================
157 {
158  GslErrorHandler hh = gsl_set_error_handler( nullptr );
159  gsl_set_error_handler( hh );
160  return hh ;
161 }
162 // ============================================================================
163 
164 // ============================================================================
169 // ============================================================================
171 ( IGslSvc::GslErrorHandler handler ) const
172 {
173  gsl_set_error_handler( handler );
174  {
175  debug() << " New GSL handler is set '"
176  << ( handler ? System::typeinfoName( typeid(handler) ) : "NULL" )
177  << "'" << endmsg ;
178  }
179  return handler ;
180 }
181 // ============================================================================
182 
183 // ============================================================================
188 // ============================================================================
189 StatusCode GslSvc::status ( const int error ) const
190 {
191  if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; }
192  StatusCode sc( error );
193  if( sc.isSuccess() ){ return StatusCode::FAILURE ; }
194  return sc ;
195 }
196 // ============================================================================
197 
198 // ============================================================================
203 // ============================================================================
205 ( const GslError& error ) const
206 {
208  // code to be ignored?
209  if( m_ignore.end() != std::find( m_ignore.begin () ,
210  m_ignore.end () ,
211  error.code ) ) { return sc ; }
212  // invoke all handlers
213  for( auto handler = m_handlers.begin() ;
214  sc.isSuccess() && m_handlers.end() != handler ; ++handler )
215  { sc = (*handler)->handle( error ); }
216  //
217  return sc ;
218 }
219 // ============================================================================
220 
221 // ============================================================================
222 // The END
223 // ============================================================================
The abstract interface for arbitrary GSL error handler.
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:18
StatusCode initialize() override
Definition: Service.cpp:68
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
T empty(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
Helper class to represent GSL errors.
Definition: GslError.h:15
StatusCode finalize() override
Definition: Service.cpp:193
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
int code
error code (GSL)
Definition: GslError.h:25
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:205
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:28
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
STL namespace.
Names m_handlersTypeNames
Definition: GslSvc.h:157
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
Test for a status code of FAILURE.
Definition: StatusCode.h:86
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:139
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode initialize() override
standard service initialization
Definition: GslSvc.cpp:56
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Handlers m_handlers
Definition: GslSvc.h:159
The implementation of IGslSvc interface.
Definition: GslSvc.h:83
T find(T...args)
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:171
std::string m_errorPolicy
error policy
Definition: GslSvc.h:153
GslErrorHandler handler() const override
retrieve the current GSL error handler
Definition: GslSvc.cpp:156
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:35
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:244
StatusCode status(const int error) const override
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:189