CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondCore/DBCommon/plugins/SQLMonitoringService.cc

Go to the documentation of this file.
00001 #include "SQLMonitoringService.h"
00002 #include "RelationalAccess/MonitoringException.h"
00003 #include "CoralBase/MessageStream.h"
00004 #include "CoralBase/TimeStamp.h"
00005 #include "CoralKernel/Context.h"
00006 #include "CoralKernel/IHandle.h"
00007 #include "CondCore/DBCommon/interface/CoralServiceMacros.h"
00008 
00009 namespace cond
00010 {
00011   SessionMonitor::SessionMonitor()
00012     : active( false ), level( coral::monitor::Default ), stream()
00013   {
00014   }
00015   
00016   SessionMonitor::SessionMonitor( bool act, coral::monitor::Level lvl )
00017     : active( act ), level( lvl ), stream()
00018   {
00019   }
00020 
00021   SQLMonitoringService::SQLMonitoringService( const std::string& key )
00022     : coral::Service( key ),
00023       m_events()
00024   {
00025   }
00026   
00027   SQLMonitoringService::~SQLMonitoringService()
00028   {
00029   }
00030   
00031   void SQLMonitoringService::setLevel( const std::string& contextKey, coral::monitor::Level level )
00032   {
00033     Repository::const_iterator rit;
00034     
00035     if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00036       {        
00037         m_events[contextKey] = SessionMonitor();
00038         m_monitoredDS.insert( contextKey );
00039       }
00040     
00041     m_events[contextKey].level = level;
00042     
00043     if( level == coral::monitor::Off )
00044       {
00045         m_events[contextKey].active = false;
00046       }
00047     else
00048       {
00049         m_events[contextKey].active = true;        
00050       }
00051     }
00052   
00053   coral::monitor::Level SQLMonitoringService::level( const std::string& contextKey ) const
00054   {
00055     Repository::const_iterator rit;
00056     
00057     if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00058       throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::level", this->name() );
00059     
00060     return (*rit).second.level;
00061   }
00062   
00063   bool SQLMonitoringService::active( const std::string& contextKey ) const
00064   {
00065     Repository::const_iterator rit;
00066     
00067     if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00068       throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::active", this->name() );
00069     
00070     return (*rit).second.active;
00071   }
00072   
00073   void SQLMonitoringService::enable( const std::string& contextKey )
00074     {
00075       Repository::iterator rit;
00076       
00077       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00078         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::enable", this->name() );
00079 
00080       (*rit).second.active = true;
00081     }
00082 
00083   void SQLMonitoringService::disable( const std::string& contextKey )
00084     {
00085       Repository::iterator rit;
00086       
00087       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00088         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::disable", this->name() );
00089       
00090       (*rit).second.active = false;
00091     }
00092   
00093   //relaxing filter on coral monitoring level due to a bug in the connection pool
00094   void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description )
00095     {
00096       Repository::iterator rit;
00097       
00098       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00099         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record( const std::string& , coral::monitor::Source, coral::monitor::Type, const std::string& )", this->name() );
00100       
00101       bool                  active = (*rit).second.active;
00102       //coral::monitor::Level level  = (*rit).second.level;
00103        
00104       if( active/* && (type & level)*/ )
00105       {
00106         (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description ) );
00107       }
00108     }
00109 
00110   void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, int data )
00111   {
00112     Repository::iterator rit;
00113 
00114     if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00115       throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record( const std::string& , coral::monitor::Source, coral::monitor::Type, const std::string&, int )", this->name() );
00116     
00117     bool                  active = (*rit).second.active;
00118     //coral::monitor::Level level  = (*rit).second.level;
00119     
00120     if( active/* && (type & level)*/ )
00121       {
00122         (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00123       }
00124   }
00125 
00126   void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, long long data )
00127   {
00128     Repository::iterator rit;
00129 
00130     if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00131       throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record( const std::string& , coral::monitor::Source, coral::monitor::Type, const std::string&, long long  )", this->name() );
00132     
00133     bool                  active = (*rit).second.active;
00134     //coral::monitor::Level level  = (*rit).second.level;
00135     
00136     if( active/* && (type & level)*/ )
00137       {
00138         (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00139       }
00140   }
00141 
00142   void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, double data )
00143     {
00144       Repository::iterator rit;
00145 
00146       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00147         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record( const std::string& , coral::monitor::Source, coral::monitor::Type, const std::string&, double )", this->name() );
00148 
00149       bool                  active = (*rit).second.active;
00150       //coral::monitor::Level level  = (*rit).second.level;
00151       
00152       if( active/* && (type & level)*/ )
00153       {
00154         (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00155       }
00156     }
00157 
00158   void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, const std::string& data )
00159     {
00160       Repository::iterator rit;
00161 
00162       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00163         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record( const std::string& , coral::monitor::Source, coral::monitor::Type, const std::string&, const std::string& )", this->name() );
00164 
00165       bool                  active = (*rit).second.active;
00166       //coral::monitor::Level level  = (*rit).second.level;
00167       
00168       if( active /*&& (type & level)*/ )
00169       {
00170         (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00171       }
00172     }
00173 
00174   const coral::IMonitoringReporter& SQLMonitoringService::reporter() const
00175     {
00176       return( static_cast<const coral::IMonitoringReporter&>(*this) );
00177     }
00178     
00179   // The coral::IMonitoringReporter interface implementation
00180   std::set< std::string > SQLMonitoringService::monitoredDataSources() const
00181     {
00182       return m_monitoredDS;
00183     }
00184 
00185   void SQLMonitoringService::report( unsigned int /*level*/ ) const
00186     {
00187       Repository::const_iterator rit;
00188       coral::MessageStream  log( "MonitoringService" );
00189       
00190       // Dummy reporting so far
00191       for( rit = m_events.begin(); rit != m_events.end(); ++rit )
00192         reportForSession( rit, log );
00193     }
00194 
00195   void SQLMonitoringService::report( const std::string& contextKey, unsigned int /* level */ ) const
00196     {
00197       Repository::const_iterator rit;
00198       
00199       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00200         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00201 
00202       // Dummy reporting so far
00203       coral::MessageStream log( "MonitoringService" );
00204 
00205       reportForSession( rit, log );
00206     }
00207 
00208   void SQLMonitoringService::reportToOutputStream( const std::string& contextKey, std::ostream& os, unsigned int /* level */ ) const
00209     {
00210       Repository::const_iterator rit;
00211       
00212       if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00213         throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00214 
00215       // Dummy reporting so far
00216       coral::MessageStream log( "MonitoringService" );
00217 
00218       reportForSession( rit, os );
00219     }
00220 
00221   void SQLMonitoringService::reportOnEvent( EventStream::const_iterator& it, std::ostream& os ) const
00222   {
00223     std::string source("");
00224     switch( it->m_source ) {
00225     case coral::monitor::Application:
00226       source = "Application";
00227       break;
00228     case coral::monitor::Session:
00229       source = "Session";
00230       break;
00231     case coral::monitor::Transaction:
00232       source = "Transaction";
00233       break;
00234     case coral::monitor::Statement:
00235       source = "Statement";
00236       break;
00237     default:
00238       source = "";
00239     };
00240     
00241     std::string type("");
00242     switch( it->m_type ) {
00243     case coral::monitor::Info:
00244       type = "Info";
00245       break;
00246     case coral::monitor::Time:
00247       type = "Time";
00248       break;
00249     case coral::monitor::Warning:
00250       type = "Warning";
00251       break;
00252     case coral::monitor::Error:
00253       type = "Error";
00254       break;
00255     case coral::monitor::Config:
00256       type = "Config";
00257       break;
00258     default:
00259       type = "";
00260     };
00261     
00262     if(it->m_source == coral::monitor::Statement || it->m_source == coral::monitor::Transaction)
00263       {
00264         os << boost::posix_time::to_iso_extended_string((*it).m_time.time()) << ": " 
00265            << source << "; "
00266            << type << "; " 
00267            <<(*it).m_description << ";"<< std::endl;
00268       }
00269   }
00270   
00271 
00272   void SQLMonitoringService::reportOnEvent( EventStream::const_iterator& it,coral::MessageStream& os ) const
00273     {
00274       std::string source("");
00275       switch( it->m_source ) {
00276       case coral::monitor::Application:
00277         source = "Application";
00278         break;
00279       case coral::monitor::Session:
00280         source = "Session";
00281         break;
00282       case coral::monitor::Transaction:
00283         source = "Transaction";
00284         break;
00285       case coral::monitor::Statement:
00286         source = "Statement";
00287         break;
00288       default:
00289         source = "";
00290       };
00291       
00292       std::string type("");
00293       switch( it->m_type ) {
00294       case coral::monitor::Info:
00295         type = "Info";
00296         break;
00297       case coral::monitor::Time:
00298         type = "Time";
00299         break;
00300       case coral::monitor::Warning:
00301         type = "Warning";
00302         break;
00303       case coral::monitor::Error:
00304         type = "Error";
00305         break;
00306       case coral::monitor::Config:
00307         type = "Config";
00308         break;
00309       default:
00310         type = "";
00311       };
00312       
00313       if(it->m_source == coral::monitor::Statement || it->m_source == coral::monitor::Transaction)
00314         {
00315           os << boost::posix_time::to_iso_extended_string((*it).m_time.time()) << ": " 
00316              << source << "; "
00317              << type << "; " 
00318              << (*it).m_description <<coral::MessageStream::flush;
00319         }
00320     }
00321 
00322   void SQLMonitoringService::reportForSession( Repository::const_iterator& it, std::ostream& os ) const
00323     {
00324       os << "Session: " << (*it).first << std::endl;
00325       std::string lvl;
00326       switch( (*it).second.level ) {
00327       case (coral::monitor::Off)     : lvl = "Off"; break;
00328       case (coral::monitor::Minimal) : lvl = "Minimal"; break;
00329       case (coral::monitor::Default) : lvl = "Default"; break;
00330       case (coral::monitor::Debug)   : lvl = "Debug"; break;
00331       case (coral::monitor::Trace)   : lvl = "Trace"; break;
00332       default: lvl = "";
00333       };
00334       os << "Monitoring Level: " << lvl << std::endl;
00335       
00336       const EventStream& evsref = (*it).second.stream;
00337       os << " Recorded " << evsref.size() << " events" << std::endl;
00338       
00339       for( EventStream::const_iterator evit = evsref.begin(); evit != evsref.end(); ++evit )
00340       {
00341         reportOnEvent( evit, os );
00342       }
00343     }
00344 
00345   void SQLMonitoringService::reportForSession( Repository::const_iterator& it, coral::MessageStream& os ) const
00346     {
00347       os << "Session: " << (*it).first;
00348       std::string lvl;
00349       switch( (*it).second.level ) {
00350       case (coral::monitor::Off)     : lvl = "Off"; break;
00351       case (coral::monitor::Minimal) : lvl = "Minimal"; break;
00352       case (coral::monitor::Default) : lvl = "Default"; break;
00353       case (coral::monitor::Debug)   : lvl = "Debug"; break;
00354       case (coral::monitor::Trace)   : lvl = "Trace"; break;
00355       default: lvl = "";
00356       };
00357       os << " monitored at level: " << lvl;
00358       
00359       const EventStream& evsref = (*it).second.stream;
00360       os << lvl << " has recorded " << evsref.size() << " events" << coral::MessageStream::endmsg;
00361       
00362       for( EventStream::const_iterator evit = evsref.begin(); evit != evsref.end(); ++evit )
00363       {
00364         reportOnEvent( evit, os );
00365       }
00366 
00367     }
00368     
00369 } // namespace cond
00370 
00371 DEFINE_CORALSERVICE(cond::SQLMonitoringService,"COND/Services/SQLMonitoringService");