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
3 #include "GaudiKernel/MsgStream.h"
4 #include "GaudiKernel/System.h"
5 #include "GaudiKernel/ISvcLocator.h"
6 #include "GaudiKernel/IToolSvc.h"
7 // STD & STL
8 #include <algorithm>
9 #include <functional>
10 // GaudiGSL
11 #include "GaudiGSL/IGslErrorHandler.h"
12 #include "GaudiGSL/GslError.h"
13 // local
14 #include "GaudiGSL/GaudiGSL.h"
15 #include "GslSvc.h"
16 #include "GaudiGSL/GslErrorHandlers.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  MsgStream log( msgSvc() , name() );
61  if( sc.isFailure() )
62  { log << MSG::ERROR
63  << " Error in initialization of base class 'Service'"<< endmsg;
64  return sc;
65  }
66  // activate the static accessor to the service
67  GaudiGSL::setGslSvc( this );
68  // set the error handlers
69  if ( "GSL" == m_errorPolicy ) { /* nothing to do */ }
70  else if ( "Off" == m_errorPolicy )
71  { gsl_set_error_handler_off() ; }
72  else if ( "Abort" == m_errorPolicy )
73  { gsl_set_error_handler ( nullptr ) ; }
74  else if ( "Ignore" == m_errorPolicy )
75  { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; }
76  else if ( "Exception" == m_errorPolicy )
77  { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; }
78  else if ( "Handle" == m_errorPolicy )
79  { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; }
80  else
81  { log << MSG::ERROR
82  << " Unknown Error policy '" << m_errorPolicy << "'"
83  << " Valid policies: "
84  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]"
85  << endmsg;
86  return StatusCode::FAILURE ;
87  }
90  GslErrorHandler handler = gsl_set_error_handler( nullptr );
91  gsl_set_error_handler( handler );
92  if( handler )
93  { log << MSG::VERBOSE
94  << " GSL Error Handler is '"
95  << System::typeinfoName( typeid(*handler) ) << "'"
96  << endmsg; }
97  else { log << MSG::INFO << " Error Handler is NULL" << endmsg ; }
98 
99  if( !m_handlersTypeNames.empty() )
100  {
102  auto toolsvc = serviceLocator()->service<IToolSvc>("ToolSvc");
103  if (!toolsvc) {
104  log << MSG::ERROR << " Could not locate Tool Service! " << endmsg;
105  return StatusCode::FAILURE;
106  }
107  for( const auto& it : m_handlersTypeNames )
108  {
109  auto pos = it.find('/');
110  IGslErrorHandler* eh = nullptr ;
111  if( pos != std::string::npos ) {
112  sc = toolsvc->retrieveTool
113  ( it.substr( 0 , pos ), it.substr( pos + 1 ), eh , this ) ;
114  } else {
115  sc = toolsvc->retrieveTool( it , it , eh , this ) ;
116  }
117  if( sc.isFailure() )
118  { log << MSG::ERROR
119  << " Could not retrieve tool '" << it << "'"<< endmsg ;
120  return sc ; }
121  if( !eh )
122  { log << MSG::ERROR
123  << " Could not retrieve tool '" << it << "'"<< endmsg ;
124  return StatusCode::FAILURE ; }
125  m_handlers.push_back( eh );
126  }
127  }
128  //
129  return StatusCode::SUCCESS;
130 }
131 // ============================================================================
132 
133 // ============================================================================
139 // ============================================================================
141 {
142  MsgStream log(msgSvc(), name());
143  log << MSG::DEBUG << "==> Finalize" << endmsg;
144 
145  // deactivate the static accessor to the service
146  GaudiGSL::setGslSvc( nullptr );
147 
148  // finalize the base class
149  return Service::finalize() ;
150 }
151 // ============================================================================
152 
153 // ============================================================================
157 // ============================================================================
159 {
160  GslErrorHandler hh = gsl_set_error_handler( nullptr );
161  gsl_set_error_handler( hh );
162  return hh ;
163 }
164 // ============================================================================
165 
166 // ============================================================================
171 // ============================================================================
173 ( IGslSvc::GslErrorHandler handler ) const
174 {
175  gsl_set_error_handler( handler );
176  {
177  MsgStream log( msgSvc(), name() );
178  log << MSG::DEBUG << " New GSL handler is set '" ;
179  if( !handler ) { log << "NULL" ; }
180  else { log << System::typeinfoName( typeid(handler) ) ; }
181  log << "'" << endmsg ;
182  }
183  return handler ;
184 }
185 // ============================================================================
186 
187 // ============================================================================
192 // ============================================================================
193 StatusCode GslSvc::status ( const int error ) const
194 {
195  if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; }
196  StatusCode sc( error );
197  if( sc.isSuccess() ){ return StatusCode::FAILURE ; }
198  return sc ;
199 }
200 // ============================================================================
201 
202 // ============================================================================
207 // ============================================================================
209 ( const GslError& error ) const
210 {
212  // code to be ignored?
213  if( m_ignore.end() != std::find( m_ignore.begin () ,
214  m_ignore.end () ,
215  error.code ) ) { return sc ; }
216  // invoke all handlers
217  for( auto handler = m_handlers.begin() ;
218  sc.isSuccess() && m_handlers.end() != handler ; ++handler )
219  { sc = (*handler)->handle( error ); }
220  //
221  return sc ;
222 }
223 // ============================================================================
224 
225 // ============================================================================
226 // The END
227 // ============================================================================
The abstract interface for arbitrary GSL error handler.
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
Helper class to represent GSL errors.
Definition: GslError.h:15
StatusCode finalize() override
Definition: Service.cpp:188
int code
error code (GSL)
Definition: GslError.h:25
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:209
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:28
STL namespace.
Names m_handlersTypeNames
Definition: GslSvc.h:156
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:140
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
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
GAUDI_API void throwException(const char *reason, const char *file, int line, int code)
The simple Gsl Error handler, it throwns the Gaudi Exception.
Handlers m_handlers
Definition: GslSvc.h:158
The implementation of IGslSvc interface.
Definition: GslSvc.h:83
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:173
std::string m_errorPolicy
error policy
Definition: GslSvc.h:152
GslErrorHandler handler() const override
retrieve the current GSL error handler
Definition: GslSvc.cpp:158
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.
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
GAUDI_API void ignoreTheError(const char *reason, const char *file, int line, int code)
The simplest Gsl Error handler, It simply ingnores the error.
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:35
StatusCode status(const int error) const override
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:193