00001 #include "SQLMonitoringService.h"
00002 #include "RelationalAccess/MonitoringException.h"
00003 #include "CoralBase/MessageStream.h"
00004 #include "CoralKernel/Context.h"
00005 #include "CoralKernel/IHandle.h"
00006 #include "CondCore/DBCommon/interface/CoralServiceMacros.h"
00007
00008 namespace cond
00009 {
00010 SessionMonitor::SessionMonitor()
00011 : active( false ), level( coral::monitor::Default ), stream()
00012 {
00013 }
00014
00015 SessionMonitor::SessionMonitor( bool act, coral::monitor::Level lvl )
00016 : active( act ), level( lvl ), stream()
00017 {
00018 }
00019
00020 SQLMonitoringService::SQLMonitoringService( const std::string& key )
00021 : coral::Service( key ),
00022 m_events()
00023 {
00024 }
00025
00026 SQLMonitoringService::~SQLMonitoringService()
00027 {
00028 }
00029
00030 void SQLMonitoringService::setLevel( const std::string& contextKey, coral::monitor::Level level )
00031 {
00032 Repository::const_iterator rit;
00033
00034 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00035 {
00036 m_events[contextKey] = SessionMonitor();
00037 m_monitoredDS.insert( contextKey );
00038 }
00039
00040 m_events[contextKey].level = level;
00041
00042 if( level == coral::monitor::Off )
00043 {
00044 m_events[contextKey].active = false;
00045 }
00046 else
00047 {
00048 m_events[contextKey].active = true;
00049 }
00050 }
00051
00052 coral::monitor::Level SQLMonitoringService::level( const std::string& contextKey ) const
00053 {
00054 Repository::const_iterator rit;
00055
00056 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00057 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::level", this->name() );
00058
00059 return (*rit).second.level;
00060 }
00061
00062 bool SQLMonitoringService::active( const std::string& contextKey ) const
00063 {
00064 Repository::const_iterator rit;
00065
00066 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00067 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::active", this->name() );
00068
00069 return (*rit).second.active;
00070 }
00071
00072 void SQLMonitoringService::enable( const std::string& contextKey )
00073 {
00074 Repository::iterator rit;
00075
00076 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00077 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::enable", this->name() );
00078
00079 (*rit).second.active = true;
00080 }
00081
00082 void SQLMonitoringService::disable( const std::string& contextKey )
00083 {
00084 Repository::iterator rit;
00085
00086 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00087 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::disable", this->name() );
00088
00089 (*rit).second.active = false;
00090 }
00091
00092
00093 void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description )
00094 {
00095 Repository::iterator rit;
00096
00097 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00098 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00099
00100 bool active = (*rit).second.active;
00101
00102
00103 if( active )
00104 {
00105 (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description ) );
00106 }
00107 }
00108
00109 void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, int data )
00110 {
00111 Repository::iterator rit;
00112
00113 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00114 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00115
00116 bool active = (*rit).second.active;
00117
00118
00119 if( active )
00120 {
00121 (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00122 }
00123 }
00124
00125 void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, long long data )
00126 {
00127 Repository::iterator rit;
00128
00129 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00130 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00131
00132 bool active = (*rit).second.active;
00133
00134
00135 if( active )
00136 {
00137 (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00138 }
00139 }
00140
00141 void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, double data )
00142 {
00143 Repository::iterator rit;
00144
00145 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00146 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00147
00148 bool active = (*rit).second.active;
00149
00150
00151 if( active )
00152 {
00153 (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00154 }
00155 }
00156
00157 void SQLMonitoringService::record( const std::string& contextKey, coral::monitor::Source source, coral::monitor::Type type, const std::string& description, const std::string& data )
00158 {
00159 Repository::iterator rit;
00160
00161 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00162 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00163
00164 bool active = (*rit).second.active;
00165
00166
00167 if( active )
00168 {
00169 (*rit).second.stream.push_back( coral::monitor::createEvent( source, type, description, data ) );
00170 }
00171 }
00172
00173 const coral::IMonitoringReporter& SQLMonitoringService::reporter() const
00174 {
00175 return( static_cast<const coral::IMonitoringReporter&>(*this) );
00176 }
00177
00178
00179 std::set< std::string > SQLMonitoringService::monitoredDataSources() const
00180 {
00181 return m_monitoredDS;
00182 }
00183
00184 void SQLMonitoringService::report( unsigned int ) const
00185 {
00186 Repository::const_iterator rit;
00187 coral::MessageStream log( "MonitoringService" );
00188
00189
00190 for( rit = m_events.begin(); rit != m_events.end(); ++rit )
00191 reportForSession( rit, log );
00192 }
00193
00194 void SQLMonitoringService::report( const std::string& contextKey, unsigned int ) const
00195 {
00196 Repository::const_iterator rit;
00197
00198 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00199 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00200
00201
00202 coral::MessageStream log( "MonitoringService" );
00203
00204 reportForSession( rit, log );
00205 }
00206
00207 void SQLMonitoringService::reportToOutputStream( const std::string& contextKey, std::ostream& os, unsigned int ) const
00208 {
00209 Repository::const_iterator rit;
00210
00211 if( ( rit = m_events.find( contextKey ) ) == m_events.end() )
00212 throw coral::MonitoringException( "Monitoring for session " + contextKey + " not initialized...", "MonitoringService::record", this->name() );
00213
00214
00215 coral::MessageStream log( "MonitoringService" );
00216
00217 reportForSession( rit, os );
00218 }
00219
00220 void SQLMonitoringService::reportOnEvent( EventStream::const_iterator& it, std::ostream& os ) const
00221 {
00222 if(it->m_source == coral::monitor::Statement)
00223 {
00224 os << (*it).m_description << ";"<< std::endl;
00225 }
00226 }
00227
00228
00229 void SQLMonitoringService::reportOnEvent( EventStream::const_iterator& it,coral::MessageStream& os ) const
00230 {
00231 if(it->m_source == coral::monitor::Statement)
00232 {
00233 os << (*it).m_description <<coral::MessageStream::flush;
00234 }
00235 }
00236
00237 void SQLMonitoringService::reportForSession( Repository::const_iterator& it, std::ostream& os ) const
00238 {
00239 const EventStream& evsref = (*it).second.stream;
00240
00241 for( EventStream::const_iterator evit = evsref.begin(); evit != evsref.end(); ++evit )
00242 {
00243 reportOnEvent( evit, os );
00244 }
00245 }
00246
00247 void SQLMonitoringService::reportForSession( Repository::const_iterator& it, coral::MessageStream& os ) const
00248 {
00249 const EventStream& evsref = (*it).second.stream;
00250
00251 for( EventStream::const_iterator evit = evsref.begin(); evit != evsref.end(); ++evit )
00252 {
00253 reportOnEvent( evit, os );
00254 }
00255
00256 }
00257
00258 }
00259
00260 DEFINE_CORALSERVICE(cond::SQLMonitoringService,"COND/Services/SQLMonitoringService");