Gaudi Framework, version v23r2

Home   Generated: Thu Jun 28 2012

GslSvc.cpp

Go to the documentation of this file.
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 // ============================================================================
00038 // ============================================================================
00039 DECLARE_SERVICE_FACTORY(GslSvc)
00040 // ============================================================================
00041 
00042 // ============================================================================
00047 // ============================================================================
00048 GslSvc::GslSvc( const std::string& name ,
00049                 ISvcLocator*       svc  )
00050   : base_class ( name , svc )
00051   , m_errorPolicy       ( "GSL" )
00052   , m_handlersTypeNames ()
00053   , m_handlers          ()
00054   , m_ignore            ()
00055 {
00056   declareProperty( "ErrorPolicy" , m_errorPolicy       ) ;
00057   declareProperty( "Handlers"    , m_handlersTypeNames ) ;
00058   declareProperty( "IgnoreCodes" , m_ignore            ) ;
00059 }
00060 // ============================================================================
00061 
00062 // ============================================================================
00064 // ============================================================================
00065 GslSvc::~GslSvc() {}
00066 // ============================================================================
00067 
00068 // ============================================================================
00074 // ============================================================================
00075 StatusCode GslSvc::initialize()
00076 {
00077   // initialize the base class
00078   StatusCode sc = Service::initialize();
00079   MsgStream log( msgSvc() , name() );
00080   if( sc.isFailure() )
00081     { log << MSG::ERROR
00082           << " Error in initialization of base class 'Service'"<< endmsg;
00083     return sc;
00084     }
00085   // activate the static accessor to the service
00086   GaudiGSL::setGslSvc( this );
00087   // set the error handlers
00088   if      ( "GSL"       == m_errorPolicy ) { /* nothing to do */ }
00089   else if ( "Off"       == m_errorPolicy )
00090     { gsl_set_error_handler_off()                                ; }
00091   else if ( "Abort"     == m_errorPolicy )
00092     { gsl_set_error_handler ( 0 )                                ; }
00093   else if ( "Ignore"    == m_errorPolicy )
00094     { gsl_set_error_handler ( GslErrorHandlers::ignoreTheError ) ; }
00095   else if ( "Exception" == m_errorPolicy )
00096     { gsl_set_error_handler ( GslErrorHandlers::throwException ) ; }
00097   else if ( "Handle"    == m_errorPolicy )
00098     { gsl_set_error_handler ( GslErrorHandlers::handleTheError ) ; }
00099   else
00100     { log << MSG::ERROR
00101           << " Unknown Error policy '" << m_errorPolicy << "'"
00102           << " Valid policies: "
00103           << "[ 'GSL' , 'Off' , 'Abort' , 'Ignore' , 'Exception' , 'Handle' ]"
00104           << endmsg;
00105     return StatusCode::FAILURE ;
00106     }
00109   GslErrorHandler handler = gsl_set_error_handler( 0 );
00110   gsl_set_error_handler( handler );
00111   if( 0 != handler )
00112     { log << MSG::VERBOSE
00113           << " GSL Error Handler is '"
00114           << System::typeinfoName( typeid(*handler) ) << "'"
00115           << endmsg; }
00116   else { log << MSG::INFO << " Error Handler is NULL" << endmsg ; }
00117 
00118   if( !m_handlersTypeNames.empty() )
00119     {
00121       SmartIF<IToolSvc> toolsvc(serviceLocator()->service("ToolSvc"));
00122       if (!toolsvc.isValid()) {
00123         log << MSG::ERROR << " Could not locate Tool Service! " << endmsg;
00124         return StatusCode::FAILURE;
00125       }
00126       for( Names::const_iterator it = m_handlersTypeNames.begin() ;
00127            m_handlersTypeNames.end() != it ; ++it )
00128         {
00129           std::string::const_iterator ipos =
00130             std::find( it->begin() , it->end() , '/');
00131           const std::string::size_type pos = ipos - it->begin() ;
00132           IGslErrorHandler* eh = 0 ;
00133           if( it->end() != ipos )
00134             { sc = toolsvc->retrieveTool
00135                 ( std::string( *it , 0 , pos )       ,
00136                   std::string( *it , pos + 1, it->length() ), eh , this ) ; }
00137           else
00138             { sc = toolsvc->retrieveTool
00139                 ( *it , std::string( *it , pos + 1, it->length() ) ,
00140                   eh , this ) ; }
00141           if( sc.isFailure() )
00142             { log << MSG::ERROR
00143                   << " Could not retrieve tool '" << *it << "'"<< endmsg ;
00144             return sc ; }
00145           if( 0 == eh )
00146             { log << MSG::ERROR
00147                   << " Could not retrieve tool '" << *it << "'"<< endmsg ;
00148             return StatusCode::FAILURE  ; }
00149           m_handlers.push_back( eh );
00150         }
00151     }
00152   //
00153   return StatusCode::SUCCESS;
00154 }
00155 // ============================================================================
00156 
00157 // ============================================================================
00163 // ============================================================================
00164 StatusCode GslSvc::finalize()
00165 {
00166   MsgStream log(msgSvc(), name());
00167   log << MSG::DEBUG << "==> Finalize" << endmsg;
00168 
00169   // deactivate the static accessor to the service
00170   GaudiGSL::setGslSvc( 0 );
00171 
00172   // finalize the base class
00173   return Service::finalize() ;
00174 }
00175 // ============================================================================
00176 
00177 // ============================================================================
00181 // ============================================================================
00182 IGslSvc::GslErrorHandler GslSvc::handler ()    const
00183 {
00184   GslErrorHandler hh = gsl_set_error_handler( 0 );
00185   gsl_set_error_handler( hh );
00186   return hh ;
00187 }
00188 // ============================================================================
00189 
00190 // ============================================================================
00195 // ============================================================================
00196 IGslSvc::GslErrorHandler GslSvc::setHandler
00197 ( IGslSvc::GslErrorHandler handler ) const
00198 {
00199   gsl_set_error_handler( handler );
00200   {
00201     MsgStream log( msgSvc(), name() );
00202     log << MSG::DEBUG << " New GSL handler is set '" ;
00203     if( 0 == handler ) { log << "NULL"                                  ; }
00204     else               { log << System::typeinfoName( typeid(handler) ) ; }
00205     log << "'" << endmsg ;
00206   }
00207   return handler ;
00208 }
00209 // ============================================================================
00210 
00211 // ============================================================================
00216 // ============================================================================
00217 StatusCode GslSvc::status        ( const int error         ) const
00218 {
00219   if( GSL_SUCCESS == error ){ return StatusCode::SUCCESS ; }
00220   StatusCode sc( error );
00221   if( sc.isSuccess()       ){ return StatusCode::FAILURE ; }
00222   return sc ;
00223 }
00224 // ============================================================================
00225 
00226 // ============================================================================
00231 // ============================================================================
00232 StatusCode GslSvc::handle
00233 ( const GslError& error ) const
00234 {
00235   StatusCode sc = StatusCode::SUCCESS ;
00236   // code to be ignored?
00237   if( m_ignore.end() != std::find( m_ignore.begin () ,
00238                                    m_ignore.end   () ,
00239                                    error.code        ) ) { return sc ; }
00240   // invoke all handlers
00241   for( Handlers::const_iterator handler = m_handlers.begin() ;
00242        sc.isSuccess() && m_handlers.end() != handler ; ++handler )
00243     { sc = (*handler)->handle( error  ); }
00244   //
00245   return sc ;
00246 }
00247 // ============================================================================
00248 
00249 // ============================================================================
00250 // The END
00251 // ============================================================================
00252 
00253 
00254 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Thu Jun 28 2012 23:27:18 for Gaudi Framework, version v23r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004