| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 // ============================================================================
29 DECLARE_COMPONENT(GslSvc)
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 // ============================================================================
56 StatusCode GslSvc::initialize()
57 {
58 // initialize the base class
59 StatusCode sc = Service::initialize();
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 // ============================================================================
139 StatusCode GslSvc::finalize()
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 // ============================================================================
156 IGslSvc::GslErrorHandler GslSvc::handler () const
157 {
158 GslErrorHandler hh = gsl_set_error_handler( nullptr );
159 gsl_set_error_handler( hh );
160 return hh ;
161 }
162 // ============================================================================
163
164 // ============================================================================
169 // ============================================================================
170 IGslSvc::GslErrorHandler GslSvc::setHandler
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 // ============================================================================
204 StatusCode GslSvc::handle
205 ( const GslError& error ) const
206 {
207 StatusCode sc = StatusCode::SUCCESS ;
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 // ============================================================================
IGslErrorHandler
The abstract interface for arbitrary GSL error handler.
Definition: IGslErrorHandler.h:15
GslErrorHandlers::handleTheError
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.
Definition: GslErrorHandlers.cpp:31
System.h
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:68
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
std::vector::empty
T empty(T...args)
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
GslError
Helper class to represent GSL errors.
Definition: GslError.h:15
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:193
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:124
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
GslError::code
int code
error code (GSL)
Definition: GslError.h:25
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
GslSvc::handle
StatusCode handle(const GslError &error) const override
handle the GSL error
Definition: GslSvc.cpp:205
IGslSvc::GslErrorHandler
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:28
IGslErrorHandler.h
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:130
std
STL namespace.
GslSvc::m_handlersTypeNames
Names m_handlersTypeNames
Definition: GslSvc.h:157
merge_files.sc
int sc
Definition: merge_files.py:167
GslErrorHandlers::throwException
GAUDI_API void throwException(const char *reason, const char *file, int line, int code)
The simple Gsl Error handler, it throwns the Gaudi Exception.
Definition: GslErrorHandlers.cpp:43
IToolSvc.h
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
GslSvc::finalize
StatusCode finalize() override
standard service finalization
Definition: GslSvc.cpp:139
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
std::vector::push_back
T push_back(T...args)
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:118
GslError.h
StatusCode::FAILURE
Definition: StatusCode.h:29
GslSvc::initialize
StatusCode initialize() override
standard service initialization
Definition: GslSvc.cpp:56
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GslSvc::m_handlers
Handlers m_handlers
Definition: GslSvc.h:159
GslSvc
The implementation of IGslSvc interface.
Definition: GslSvc.h:83
std::find
T find(T...args)
GslSvc::setHandler
GslErrorHandler setHandler(GslErrorHandler handler) const override
set new GSL error handler
Definition: GslSvc.cpp:171
GslSvc::m_errorPolicy
std::string m_errorPolicy
error policy
Definition: GslSvc.h:153
GslSvc::handler
GslErrorHandler handler() const override
retrieve the current GSL error handler
Definition: GslSvc.cpp:156
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:127
GslSvc.h
CommonMessaging
Definition: CommonMessaging.h:58
GaudiGSL::setGslSvc
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:35
GslErrorHandlers::ignoreTheError
GAUDI_API void ignoreTheError(const char *reason, const char *file, int line, int code)
The simplest Gsl Error handler, It simply ingnores the error.
Definition: GslErrorHandlers.cpp:24
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
GslErrorHandlers.h
GaudiGSL.h
GslSvc::status
StatusCode status(const int error) const override
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:189