All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 // ============================================================================
31 
32 // ============================================================================
37 // ============================================================================
38 GslSvc::GslSvc( const std::string& name ,
39  ISvcLocator* svc )
40  : base_class ( name , svc )
41  , m_errorPolicy ( "GSL" )
42  , m_handlersTypeNames ()
43  , m_handlers ()
44  , m_ignore ()
45 {
46  declareProperty( "ErrorPolicy" , m_errorPolicy ) ;
47  declareProperty( "Handlers" , m_handlersTypeNames ) ;
48  declareProperty( "IgnoreCodes" , m_ignore ) ;
49 }
50 // ============================================================================
51 
52 // ============================================================================
54 // ============================================================================
56 // ============================================================================
57 
58 // ============================================================================
64 // ============================================================================
66 {
67  // initialize the base class
69  MsgStream log( msgSvc() , name() );
70  if( sc.isFailure() )
71  { log << MSG::ERROR
72  << " Error in initialization of base class 'Service'"<< endmsg;
73  return sc;
74  }
75  // activate the static accessor to the service
76  GaudiGSL::setGslSvc( this );
77  // set the error handlers
78  if ( "GSL" == m_errorPolicy ) { /* nothing to do */ }
79  else if ( "Off" == m_errorPolicy )
80  { gsl_set_error_handler_off() ; }
81  else if ( "Abort" == m_errorPolicy )
82  { gsl_set_error_handler ( 0 ) ; }
83  else if ( "Ignore" == m_errorPolicy )
84  { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; }
85  else if ( "Exception" == m_errorPolicy )
86  { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; }
87  else if ( "Handle" == m_errorPolicy )
88  { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; }
89  else
90  { log << MSG::ERROR
91  << " Unknown Error policy '" << m_errorPolicy << "'"
92  << " Valid policies: "
93  << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]"
94  << endmsg;
95  return StatusCode::FAILURE ;
96  }
99  GslErrorHandler handler = gsl_set_error_handler( 0 );
100  gsl_set_error_handler( handler );
101  if( 0 != handler )
102  { log << MSG::VERBOSE
103  << " GSL Error Handler is '"
104  << System::typeinfoName( typeid(*handler) ) << "'"
105  << endmsg; }
106  else { log << MSG::INFO << " Error Handler is NULL" << endmsg ; }
107 
108  if( !m_handlersTypeNames.empty() )
109  {
111  SmartIF<IToolSvc> toolsvc(serviceLocator()->service("ToolSvc"));
112  if (!toolsvc.isValid()) {
113  log << MSG::ERROR << " Could not locate Tool Service! " << endmsg;
114  return StatusCode::FAILURE;
115  }
116  for( Names::const_iterator it = m_handlersTypeNames.begin() ;
117  m_handlersTypeNames.end() != it ; ++it )
118  {
119  std::string::const_iterator ipos =
120  std::find( it->begin() , it->end() , '/');
121  const std::string::size_type pos = ipos - it->begin() ;
122  IGslErrorHandler* eh = 0 ;
123  if( it->end() != ipos )
124  { sc = toolsvc->retrieveTool
125  ( std::string( *it , 0 , pos ) ,
126  std::string( *it , pos + 1, it->length() ), eh , this ) ; }
127  else
128  { sc = toolsvc->retrieveTool
129  ( *it , std::string( *it , pos + 1, it->length() ) ,
130  eh , this ) ; }
131  if( sc.isFailure() )
132  { log << MSG::ERROR
133  << " Could not retrieve tool '" << *it << "'"<< endmsg ;
134  return sc ; }
135  if( 0 == eh )
136  { log << MSG::ERROR
137  << " Could not retrieve tool '" << *it << "'"<< endmsg ;
138  return StatusCode::FAILURE ; }
139  m_handlers.push_back( eh );
140  }
141  }
142  //
143  return StatusCode::SUCCESS;
144 }
145 // ============================================================================
146 
147 // ============================================================================
153 // ============================================================================
155 {
156  MsgStream log(msgSvc(), name());
157  log << MSG::DEBUG << "==> Finalize" << endmsg;
158 
159  // deactivate the static accessor to the service
160  GaudiGSL::setGslSvc( 0 );
161 
162  // finalize the base class
163  return Service::finalize() ;
164 }
165 // ============================================================================
166 
167 // ============================================================================
171 // ============================================================================
173 {
174  GslErrorHandler hh = gsl_set_error_handler( 0 );
175  gsl_set_error_handler( hh );
176  return hh ;
177 }
178 // ============================================================================
179 
180 // ============================================================================
185 // ============================================================================
187 ( IGslSvc::GslErrorHandler handler ) const
188 {
189  gsl_set_error_handler( handler );
190  {
191  MsgStream log( msgSvc(), name() );
192  log << MSG::DEBUG << " New GSL handler is set '" ;
193  if( 0 == handler ) { log << "NULL" ; }
194  else { log << System::typeinfoName( typeid(handler) ) ; }
195  log << "'" << endmsg ;
196  }
197  return handler ;
198 }
199 // ============================================================================
200 
201 // ============================================================================
206 // ============================================================================
207 StatusCode GslSvc::status ( const int error ) const
208 {
209  if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; }
210  StatusCode sc( error );
211  if( sc.isSuccess() ){ return StatusCode::FAILURE ; }
212  return sc ;
213 }
214 // ============================================================================
215 
216 // ============================================================================
221 // ============================================================================
223 ( const GslError& error ) const
224 {
226  // code to be ignored?
227  if( m_ignore.end() != std::find( m_ignore.begin () ,
228  m_ignore.end () ,
229  error.code ) ) { return sc ; }
230  // invoke all handlers
231  for( Handlers::const_iterator handler = m_handlers.begin() ;
232  sc.isSuccess() && m_handlers.end() != handler ; ++handler )
233  { sc = (*handler)->handle( error ); }
234  //
235  return sc ;
236 }
237 // ============================================================================
238 
239 // ============================================================================
240 // The END
241 // ============================================================================
242 
243 
244 
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.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
virtual StatusCode handle(const GslError &error) const
handle the GSL error
Definition: GslSvc.cpp:223
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
Helper class to represent GSL errors.
Definition: GslError.h:17
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
virtual StatusCode initialize()
standard service initialization
Definition: GslSvc.cpp:65
int code
error code (GSL)
Definition: GslError.h:27
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
Names m_handlersTypeNames
Definition: GslSvc.h:158
virtual ~GslSvc()
destructor, virtual and protected
Definition: GslSvc.cpp:55
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:72
void(* GslErrorHandler)(const char *, const char *, int, int)
type definition of "standard" GSL error handler functions
Definition: IGslSvc.h:30
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
virtual GslErrorHandler handler() const
retrieve the current GSL error handler
Definition: GslSvc.cpp:172
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Handlers m_handlers
Definition: GslSvc.h:160
The implementation of IGslSvc interface.
Definition: GslSvc.h:85
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
std::string m_errorPolicy
error policy
Definition: GslSvc.h:154
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
virtual GslErrorHandler setHandler(GslErrorHandler handler) const
set new GSL error handler
Definition: GslSvc.cpp:187
Templated class to add the standard messaging functionalities.
static const IGslSvc * setGslSvc(const IGslSvc *value)
set new value for static Gaudi GSL Service
Definition: GaudiGSL.cpp:38
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:142
GAUDI_API void ignoreTheError(const char *reason, const char *file, int line, int code)
The simplest Gsl Error handler, It simply ingnores the error.
virtual StatusCode finalize()
standard service finalization
Definition: GslSvc.cpp:154
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
virtual StatusCode status(const int error) const
transform GSL error code to Gaudi status code
Definition: GslSvc.cpp:207