00001
00002
00003
00004 #include "GaudiKernel/MsgStream.h"
00005 #include "GaudiKernel/ISvcLocator.h"
00006 #include "GaudiKernel/IIncidentListener.h"
00007 #include "GaudiKernel/Incident.h"
00008 #include "GaudiKernel/GaudiException.h"
00009 #include "IncidentSvc.h"
00010
00011
00012
00013 static SvcFactory<IncidentSvc> s_factory;
00014 const ISvcFactory& IncidentSvcFactory = s_factory;
00015
00016
00017
00018
00019 IncidentSvc::IncidentSvc( const std::string& name, ISvcLocator* svc )
00020 : Service(name, svc) {
00021 }
00022
00023 IncidentSvc::~IncidentSvc() {
00024 }
00025
00026
00027
00028
00029
00030 StatusCode IncidentSvc::initialize() {
00031
00032 StatusCode sc = Service::initialize();
00033 if ( sc.isFailure() ) {
00034 return sc;
00035 }
00036
00037 MsgStream log( msgSvc(), name() );
00038
00039
00040 setProperties();
00041
00042 return StatusCode::SUCCESS;
00043 }
00044
00045 StatusCode IncidentSvc::finalize() {
00046
00047 StatusCode sc = Service::finalize();
00048 if ( sc.isFailure() ) {
00049 return sc;
00050 }
00051
00052 return StatusCode::SUCCESS;
00053 }
00054
00055 StatusCode IncidentSvc::queryInterface( const IID& riid, void** ppvInterface ) {
00056 if ( IID_IIncidentSvc == riid ) {
00057 *ppvInterface = (IIncidentSvc*)this;
00058 }
00059 else {
00060
00061 return Service::queryInterface(riid, ppvInterface);
00062 }
00063 addRef();
00064 return StatusCode::SUCCESS;
00065 }
00066
00067
00068
00069
00070
00071 void IncidentSvc::addListener(IIncidentListener* lis, const std::string& type, long prio) {
00072 std::string ltype;
00073 if( type == "" ) ltype = "ALL";
00074 else ltype = type;
00075
00076 ListenerMap::iterator itMap = m_listenerMap.find( ltype );
00077 if( itMap == m_listenerMap.end() ) {
00078
00079 ListenerList* newlist = new ListenerList();
00080 std::pair<ListenerMap::iterator, bool> p;
00081 p = m_listenerMap.insert(ListenerMap::value_type(ltype, newlist));
00082 if( p.second ) itMap = p.first;
00083 }
00084 ListenerList* llist = (*itMap).second;
00085
00086 ListenerList::iterator itlist;
00087 for( itlist = llist->begin(); itlist != llist->end(); itlist++ ) {
00088 if( (*itlist).second < prio ) {
00089
00090 break;
00091 }
00092 }
00093 llist->insert(itlist, Listener(lis, prio));
00094 return;
00095 }
00096 void IncidentSvc::removeListener(IIncidentListener* lis, const std::string& type) {
00097 if( type == "") {
00098
00099 ListenerMap::iterator itmap;
00100 for ( itmap = m_listenerMap.begin(); itmap != m_listenerMap.end();) {
00101
00102
00103 ListenerMap::iterator itmap_old = itmap;
00104 itmap++;
00105 removeListener( lis, (*itmap_old).first );
00106 }
00107 }
00108 else {
00109 ListenerMap::iterator itmap = m_listenerMap.find( type );
00110 if( itmap == m_listenerMap.end() ) {
00111
00112 return;
00113 }
00114 else {
00115 ListenerList* llist = (*itmap).second;
00116 ListenerList::iterator itlist;
00117
00118
00119 for( itlist = llist->begin(); itlist != llist->end(); ) {
00120 if( (*itlist).first == lis || lis == 0) {
00121 itlist = llist->erase(itlist);
00122 }
00123 else {
00124 itlist++;
00125 }
00126 }
00127 if( llist->size() == 0) {
00128 delete llist;
00129 m_listenerMap.erase(itmap);
00130 }
00131 }
00132 }
00133 return;
00134 }
00135 void IncidentSvc::fireIncident( const Incident& incident ) {
00136 MsgStream log ( msgSvc() , name());
00137 ListenerMap::iterator itmap = m_listenerMap.find( incident.type() );
00138 if( itmap != m_listenerMap.end() ) {
00139 ListenerList* llist = (*itmap).second;
00140 ListenerList::iterator itlist;
00141
00142 for( itlist = llist->begin(); itlist != llist->end(); itlist++ ) {
00143
00144 try {
00145 (*itlist).first->handle(incident);
00146 }
00147 catch( const GaudiException& exc ) {
00148 log << MSG::ERROR << "Exception with tag=" << exc.tag() << " is caught " << endreq;
00149 log << MSG::ERROR << exc << endreq;
00150 }
00151 catch( const std::exception& exc ) {
00152 log << MSG::ERROR << "Standard std::exception is caught " << endreq;
00153 log << MSG::ERROR << exc.what() << endreq;
00154 }
00155 catch(...) {
00156 log << MSG::ERROR << "UNKNOWN Exception is caught " << endreq;
00157 }
00158 }
00159 }
00160 return;
00161 }
00162
00163