![]() |
|
|
Generated: 8 Jan 2009 |
00001 // $Id: GslSvc.cpp,v 1.2 2006/01/10 20:00:05 hmd Exp $ 00002 // Include files 00003 // from Gaudi 00004 #include "GaudiKernel/SvcFactory.h" 00005 #include "GaudiKernel/MsgStream.h" 00006 #include "GaudiKernel/System.h" 00007 #include "GaudiKernel/ISvcLocator.h" 00008 #include "GaudiKernel/IToolSvc.h" 00009 // STD & STL 00010 #include <algorithm> 00011 #include <functional> 00012 // GaudiGSL 00013 #include "GaudiGSL/IGslErrorHandler.h" 00014 #include "GaudiGSL/GslError.h" 00015 // local 00016 #include "GaudiGSL/GaudiGSL.h" 00017 #include "GslSvc.h" 00018 #include "GaudiGSL/GslErrorHandlers.h" 00019 // gsl 00020 #include "gsl/gsl_errno.h" 00021 00022 // ============================================================================ 00030 // ============================================================================ 00031 00032 // ============================================================================ 00039 // ============================================================================ 00040 DECLARE_SERVICE_FACTORY(GslSvc) 00041 // ============================================================================ 00042 00043 // ============================================================================ 00048 // ============================================================================ 00049 GslSvc::GslSvc( const std::string& name , 00050 ISvcLocator* svc ) 00051 : Service ( name , svc ) 00052 , m_errorPolicy ( "GSL" ) 00053 , m_handlersTypeNames () 00054 , m_handlers () 00055 , m_ignore () 00056 { 00057 declareProperty( "ErrorPolicy" , m_errorPolicy ) ; 00058 declareProperty( "Handlers" , m_handlersTypeNames ) ; 00059 declareProperty( "IgnoreCodes" , m_ignore ) ; 00060 }; 00061 // ============================================================================ 00062 00063 // ============================================================================ 00065 // ============================================================================ 00066 GslSvc::~GslSvc() {}; 00067 // ============================================================================ 00068 00069 // ============================================================================ 00075 // ============================================================================ 00076 StatusCode GslSvc::initialize() 00077 { 00078 // initialize the base class 00079 StatusCode sc = Service::initialize(); 00080 MsgStream log( msgSvc() , name() ); 00081 if( sc.isFailure() ) 00082 { log << MSG::ERROR 00083 << " Error in initialization of base class 'Service'"<< endreq; 00084 return sc; 00085 } 00086 // activate the static accessor to the service 00087 GaudiGSL::setGslSvc( this ); 00088 // set the error handlers 00089 if ( "GSL" == m_errorPolicy ) { /* nothing to do */ } 00090 else if ( "Off" == m_errorPolicy ) 00091 { gsl_set_error_handler_off() ; } 00092 else if ( "Abort" == m_errorPolicy ) 00093 { gsl_set_error_handler ( 0 ) ; } 00094 else if ( "Ignore" == m_errorPolicy ) 00095 { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; } 00096 else if ( "Exception" == m_errorPolicy ) 00097 { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; } 00098 else if ( "Handle" == m_errorPolicy ) 00099 { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; } 00100 else 00101 { log << MSG::ERROR 00102 << " Unknown Error policy '" << m_errorPolicy << "'" 00103 << " Valid policies: " 00104 << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]" 00105 << endreq; 00106 return StatusCode::FAILURE ; 00107 } 00110 GslErrorHandler handler = gsl_set_error_handler( 0 ); 00111 gsl_set_error_handler( handler ); 00112 if( 0 != handler ) 00113 { log << MSG::VERBOSE 00114 << " GSL Error Handler is '" 00115 << System::typeinfoName( typeid(*handler) ) << "'" 00116 << endreq; } 00117 else { log << MSG::INFO << " Error Handler is NULL" << endreq ; } 00118 00119 if( !m_handlersTypeNames.empty() ) 00120 { 00122 IToolSvc* toolsvc = 0 ; 00123 StatusCode sc = 00124 serviceLocator()->service( "ToolSvc" , toolsvc , true ); 00125 if ( sc.isFailure() ) 00126 { log << MSG::ERROR << " Could not locate Tool Service! " << endreq ; 00127 return sc ; } 00128 if ( 0 == toolsvc ) 00129 { log << MSG::ERROR << " Could not locate Tool Service! " << endreq ; 00130 return StatusCode::FAILURE ; } 00131 for( Names::const_iterator it = m_handlersTypeNames.begin() ; 00132 m_handlersTypeNames.end() != it ; ++it ) 00133 { 00134 std::string::const_iterator ipos = 00135 std::find( it->begin() , it->end() , '/'); 00136 const std::string::size_type pos = ipos - it->begin() ; 00137 IGslErrorHandler* eh = 0 ; 00138 if( it->end() != ipos ) 00139 { sc = toolsvc->retrieveTool 00140 ( std::string( *it , 0 , pos ) , 00141 std::string( *it , pos + 1, it->length() ), eh , this ) ; } 00142 else 00143 { sc = toolsvc->retrieveTool 00144 ( *it , std::string( *it , pos + 1, it->length() ) , 00145 eh , this ) ; } 00146 if( sc.isFailure() ) 00147 { log << MSG::ERROR 00148 << " Could not retrieve tool '" << *it << "'"<< endreq ; 00149 return sc ; } 00150 if( 0 == eh ) 00151 { log << MSG::ERROR 00152 << " Could not retrieve tool '" << *it << "'"<< endreq ; 00153 return StatusCode::FAILURE ; } 00154 m_handlers.push_back( eh ); 00155 } 00156 } 00157 // 00158 return StatusCode::SUCCESS; 00159 }; 00160 // ============================================================================ 00161 00162 // ============================================================================ 00168 // ============================================================================ 00169 StatusCode GslSvc::finalize() 00170 { 00171 MsgStream log(msgSvc(), name()); 00172 log << MSG::DEBUG << "==> Finalize" << endreq; 00173 00174 // deactivate the static accessor to the service 00175 GaudiGSL::setGslSvc( 0 ); 00176 00177 // finalize the base class 00178 return Service::finalize() ; 00179 }; 00180 // ============================================================================ 00181 00182 // ============================================================================ 00191 // ============================================================================ 00192 StatusCode GslSvc::queryInterface 00193 ( const InterfaceID& iid , 00194 void** ppi ) 00195 { 00196 if ( 0 == ppi ) { return StatusCode::FAILURE ; } 00197 if ( IGslSvc:: interfaceID() == iid ) 00198 { *ppi = static_cast<IGslSvc*> ( this ); } 00199 else if( IService:: interfaceID() == iid ) 00200 { *ppi = static_cast<IService*> ( this ); } 00201 else if( IInterface:: interfaceID() == iid ) 00202 { *ppi = static_cast<IInterface*> ( this ); } 00203 else 00204 { return Service::queryInterface( iid , ppi ) ; } // RETURN ! 00205 // 00206 addRef(); 00207 // 00208 return StatusCode::SUCCESS ; 00209 }; 00210 // ============================================================================ 00211 00212 // ============================================================================ 00216 // ============================================================================ 00217 IGslSvc::GslErrorHandler GslSvc::handler () const 00218 { 00219 GslErrorHandler hh = gsl_set_error_handler( 0 ); 00220 gsl_set_error_handler( hh ); 00221 return hh ; 00222 }; 00223 // ============================================================================ 00224 00225 // ============================================================================ 00230 // ============================================================================ 00231 IGslSvc::GslErrorHandler GslSvc::setHandler 00232 ( IGslSvc::GslErrorHandler handler ) const 00233 { 00234 gsl_set_error_handler( handler ); 00235 { 00236 MsgStream log( msgSvc(), name() ); 00237 log << MSG::DEBUG << " New GSL handler is set '" ; 00238 if( 0 == handler ) { log << "NULL" ; } 00239 else { log << System::typeinfoName( typeid(handler) ) ; } 00240 log << "'" << endreq ; 00241 } 00242 return handler ; 00243 }; 00244 // ============================================================================ 00245 00246 // ============================================================================ 00251 // ============================================================================ 00252 StatusCode GslSvc::status ( const int error ) const 00253 { 00254 if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; } 00255 StatusCode sc( error ); 00256 if( sc.isSuccess() ){ return StatusCode::FAILURE ; } 00257 return sc ; 00258 }; 00259 // ============================================================================ 00260 00261 // ============================================================================ 00266 // ============================================================================ 00267 StatusCode GslSvc::handle 00268 ( const GslError& error ) const 00269 { 00270 StatusCode sc = StatusCode::SUCCESS ; 00271 // code to be ignored? 00272 if( m_ignore.end() != std::find( m_ignore.begin () , 00273 m_ignore.end () , 00274 error.code ) ) { return sc ; } 00275 // invoke all handlers 00276 for( Handlers::const_iterator handler = m_handlers.begin() ; 00277 sc.isSuccess() && m_handlers.end() != handler ; ++handler ) 00278 { sc = (*handler)->handle( error ); } 00279 // 00280 return sc ; 00281 }; 00282 // ============================================================================ 00283 00284 // ============================================================================ 00285 // The END 00286 // ============================================================================ 00287 00288 00289