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/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 // ============================================================================
36 // ============================================================================
37 StatusCode GslSvc::initialize()
38 {
39  // initialize the base class
41  if( sc.isFailure() )
42  { error()
43  << " Error in initialization of base class 'Service'"<< endmsg;
44  return sc;
45  }
46  // activate the static accessor to the service
47  GaudiGSL::setGslSvc( this );
48  // set the error handlers
49  if ( "GSL" == m_errorPolicy ) { /* nothing to do */ }
50  else if ( "Off" == m_errorPolicy )
51  { gsl_set_error_handler_off() ; }
52  else if ( "Abort" == m_errorPolicy )
53  { gsl_set_error_handler ( nullptr ) ; }
54  else if ( "Ignore" == m_errorPolicy )
55  { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; }
56  else if ( "Exception" == m_errorPolicy )
57  { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; }
58  else if ( "Handle" == m_errorPolicy )
59  { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; }
60  else
61  { error()
62  << " Unknown Error policy '" << m_errorPolicy << "'"
63  << " Valid policies: "
64  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]"
65  << endmsg;
66  return StatusCode::FAILURE ;
67  }
70  GslErrorHandler handler = gsl_set_error_handler( nullptr );
71  gsl_set_error_handler( handler );
72  if( handler )
73  { verbose()
74  << " GSL Error Handler is '"
75  << System::typeinfoName( typeid(*handler) ) << "'"
76  << endmsg; }
77  else { info() << " Error Handler is NULL" << endmsg ; }
78 
79  if( !m_handlersTypeNames.empty() )
80  {
82  auto toolsvc = serviceLocator()->service<IToolSvc>("ToolSvc");
83  if (!toolsvc) {
84  error() << " Could not locate Tool Service! " << endmsg;
85  return StatusCode::FAILURE;
86  }
87  for( const auto& it : m_handlersTypeNames )
88  {
89  auto pos = it.find('/');
90  IGslErrorHandler* eh = nullptr ;
91  if( pos != std::string::npos ) {
92  sc = toolsvc->retrieveTool
93  ( it.substr( 0 , pos ), it.substr( pos + 1 ), eh , this ) ;
94  } else {
95  sc = toolsvc->retrieveTool( it , it , eh , this ) ;
96  }
97  if( sc.isFailure() )
98  { error()
99  << " Could not retrieve tool '" << it << "'"<< endmsg ;
100  return sc ; }
101  if( !eh )
102  { error()
103  << " Could not retrieve tool '" << it << "'"<< endmsg ;
104  return StatusCode::FAILURE ; }
105  m_handlers.push_back( eh );
106  }
107  }
108  //
109  return StatusCode::SUCCESS;
110 }
111 // ============================================================================
112 
113 // ============================================================================
119 // ============================================================================
121 {
122  debug() << "==> Finalize" << endmsg;
123 
124  // deactivate the static accessor to the service
125  GaudiGSL::setGslSvc( nullptr );
126 
127  // finalize the base class
128  return Service::finalize() ;
129 }
130 // ============================================================================
131 
132 // ============================================================================
136 // ============================================================================
138 {
139  GslErrorHandler hh = gsl_set_error_handler( nullptr );
140  gsl_set_error_handler( hh );
141  return hh ;
142 }
143 // ============================================================================
144 
145 // ============================================================================
150 // ============================================================================
153 {
154  gsl_set_error_handler( handler );
155  {
156  debug() << " New GSL handler is set '"
157  << ( handler ? System::typeinfoName( typeid(handler) ) : "NULL" )
158  << "'" << endmsg ;
159  }
160  return handler ;
161 }
162 // ============================================================================
163 
164 // ============================================================================
169 // ============================================================================
170 StatusCode GslSvc::status ( const int error ) const
171 {
172  if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; }
173  StatusCode sc( error );
174  if( sc.isSuccess() ){ return StatusCode::FAILURE ; }
175  return sc ;
176 }
177 // ============================================================================
178 
179 // ============================================================================
184 // ============================================================================
186 ( const GslError& error ) const
187 {
189  // code to be ignored?
190  if( m_ignore.end() != std::find( m_ignore.begin () ,
191  m_ignore.end () ,
192  error.code ) ) { return sc ; }
193  // invoke all handlers
194  for( auto handler = m_handlers.begin() ;
195  sc.isSuccess() && m_handlers.end() != handler ; ++handler )
196  { sc = (*handler)->handle( error ); }
197  //
198  return sc ;
199 }
200 // ============================================================================
201 
202 // ============================================================================
203 // The END
204 // ============================================================================
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:19
StatusCode initialize() override
Definition: Service.cpp:64
Helper class to represent GSL errors.
Definition: GslError.h:15
StatusCode finalize() override
Definition: Service.cpp:174
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
int code
error code (GSL)
Definition: GslError.h:25
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:28
Gaudi::Property< std::vector< int > > m_ignore
Definition: GslSvc.h:145
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
Test for a status code of FAILURE.
Definition: StatusCode.h:84
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:120
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:152
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Handlers m_handlers
Definition: GslSvc.h:148
The implementation of IGslSvc interface.
Definition: GslSvc.h:83
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:186
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:137
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:170