Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

MessageSvc.cpp

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiSvc/src/MessageSvc/MessageSvc.cpp,v 1.1.1.2 2001/04/18 18:32:50 tlindner Exp $
00002 
00003 #include "GaudiKernel/Kernel.h"
00004 #include "GaudiKernel/StatusCode.h"
00005 #include "GaudiKernel/SvcFactory.h"
00006 #include "GaudiKernel/Message.h"
00007 #include "MessageSvc.h"
00008 
00009 #include <strstream>
00010 #include <iostream>
00011 
00012 // Instantiation of a static factory class used by clients to create
00013 // instances of this service
00014 static const SvcFactory<MessageSvc> s_factory;
00015 const ISvcFactory& MessageSvcFactory = s_factory;
00016 
00017 // Constructor
00018 MessageSvc::MessageSvc( const std::string& name, ISvcLocator* svcloc ) : Service( name, svcloc ) {
00019   m_defaultStream = &std::cout;
00020   m_outputLevel   = MSG::NIL;
00021 //  declareProperty( "OutputLevel", m_outputLevel   = MSG::NIL);
00022   declareProperty( "Format",      m_defaultFormat = "% F%18W%S%7W%R%T %0W%M");
00023 }
00024 
00025 
00027 StatusCode MessageSvc::initialize() {
00028   StatusCode sc;
00029   sc = Service::initialize();
00030   if( sc.isFailure() ) return sc;
00031   // Set my own properties
00032   setProperties();
00033   return StatusCode::SUCCESS;
00034 }
00036 StatusCode MessageSvc::finalize() {
00037   return StatusCode::SUCCESS;
00038 }
00039 
00040 //#############################################################################
00041 // ---------------------------------------------------------------------------
00042 // Routine: reportMessage
00043 // Purpose: dispatches a message to the relevant streams.
00044 // ---------------------------------------------------------------------------
00045 //
00046 
00047 void MessageSvc::reportMessage( const Message& msg )    {
00048   int key = msg.getType();
00049   StreamMap::const_iterator first = m_streamMap.lower_bound( key );
00050   if ( first != m_streamMap.end() ) {
00051     StreamMap::const_iterator last = m_streamMap.upper_bound( key );
00052     while( first != last ) {
00053       std::ostream& stream = *( (*first).second.second );
00054       stream << msg << std::endl;
00055       first++;
00056     }
00057   }
00058   else if ( key >= outputLevel(msg.getSource()) )   {
00059     msg.setFormat(m_defaultFormat);
00060     (*m_defaultStream) << msg << std::endl << std::flush;
00061   }
00062 }
00063 
00064 //#############################################################################
00065 // ---------------------------------------------------------------------------
00066 // Routine: reportMessage
00067 // Purpose: dispatches a message to the relevant streams.
00068 // ---------------------------------------------------------------------------
00069 //
00070 void MessageSvc::reportMessage( const char* source, int type, const char* message) {
00071   Message msg( source, type, message);
00072   reportMessage( msg );
00073 }
00074 
00075 //#############################################################################
00076 // ---------------------------------------------------------------------------
00077 // Routine: reportMessage
00078 // Purpose: dispatches a message to the relevant streams.
00079 // ---------------------------------------------------------------------------
00080 //
00081 void MessageSvc::reportMessage( const std::string& source, int type, const std::string& message) {
00082   Message msg( source, type, message);
00083   reportMessage( msg );
00084 }
00085 
00086 //#############################################################################
00087 // ---------------------------------------------------------------------------
00088 // Routine: sendMessage
00089 // Purpose: finds a message for a given status code and dispatches it.
00090 // ---------------------------------------------------------------------------
00091 //
00092 
00093 void MessageSvc::reportMessage( const StatusCode& key, const std::string& source )
00094 {  
00095   MessageMap::const_iterator first = m_messageMap.lower_bound( key );
00096   if ( first != m_messageMap.end() ) {
00097     MessageMap::const_iterator last = m_messageMap.upper_bound( key );
00098     while( first != last ) {
00099       Message msg = (*first).second;
00100       msg.setSource( source );
00101       std::ostrstream os1;
00102       os1 << "Status Code " << key.getCode() << std::ends;
00103       Message stat_code1( source, msg.getType(), os1.str() );
00104       delete os1.str();
00105       reportMessage( stat_code1 );
00106       reportMessage( msg );
00107       first++;
00108     }
00109   }
00110   else {
00111     Message mesg = m_defaultMessage;
00112     mesg.setSource( source );
00113     std::ostrstream os2;
00114     os2 << "Status Code " << key.getCode() << std::ends;
00115     Message stat_code2( source,  mesg.getType(), os2.str() );
00116     delete os2.str();
00117     reportMessage( stat_code2 );
00118     reportMessage( mesg );
00119   }
00120 }
00121 
00122 //#############################################################################
00123 // ---------------------------------------------------------------------------
00124 // Routine: insertStream
00125 // Purpose: inserts a stream for a message type.
00126 // ---------------------------------------------------------------------------
00127 //
00128 
00129 void MessageSvc::insertStream( int key, const std::string& name, std::ostream *stream )
00130 {
00131   typedef StreamMap::value_type value_type;
00132   m_streamMap.insert( value_type( key, NamedStream(name,stream) ) );
00133 }
00134 
00135 //#############################################################################
00136 // ---------------------------------------------------------------------------
00137 // Routine: eraseStream
00138 // Purpose: erases all the streams for all the message types.
00139 // ---------------------------------------------------------------------------
00140 //
00141 
00142 void MessageSvc::eraseStream()
00143 {
00144   m_streamMap.erase( m_streamMap.begin(), m_streamMap.end() );
00145 }
00146 
00147 //#############################################################################
00148 // ---------------------------------------------------------------------------
00149 // Routine: eraseStream
00150 // Purpose: erases all the streams for a message type.
00151 // ---------------------------------------------------------------------------
00152 //
00153 
00154 void MessageSvc::eraseStream( int message_type )
00155 {
00156   m_streamMap.erase( message_type );
00157 }
00158 
00159 //#############################################################################
00160 // ---------------------------------------------------------------------------
00161 // Routine: eraseStream
00162 // Purpose: erases one stream for a message type.
00163 // ---------------------------------------------------------------------------
00164 //
00165 
00166 void MessageSvc::eraseStream( int key, std::ostream* stream )   {
00167   if ( 0 != stream )    {
00168     bool changed = true;
00169     while( changed ) {
00170       changed = false;
00171       StreamMap::iterator first = m_streamMap.lower_bound( key );
00172       StreamMap::iterator last = m_streamMap.upper_bound( key );
00173       while( first != last ) {
00174         if ( (*first).second.second == stream ) {
00175           m_streamMap.erase( first );
00176           changed = true;
00177           break;
00178         }
00179       }
00180     }      
00181   }
00182 }
00183 
00184 //#############################################################################
00185 // ---------------------------------------------------------------------------
00186 // Routine: eraseStream
00187 // Purpose: erases one stream for all message types.
00188 // ---------------------------------------------------------------------------
00189 //
00190 
00191 void MessageSvc::eraseStream( std::ostream* stream )    {
00192   if ( 0 != stream )    {
00193     bool changed = true;
00194     while( changed ) {
00195       changed = false;
00196       StreamMap::iterator first = m_streamMap.begin();
00197       while( first != m_streamMap.end() ) {
00198         if ( (*first).second.second == stream ) {
00199           m_streamMap.erase( first );
00200           changed = true;
00201           break;
00202         }
00203       }
00204     }      
00205   }
00206 }
00207 
00208 
00209 //#############################################################################
00210 // ---------------------------------------------------------------------------
00211 // Routine: insertMessage
00212 // Purpose: inserts a message for a status code.
00213 // ---------------------------------------------------------------------------
00214 //
00215 
00216 void MessageSvc::insertMessage( const StatusCode& key, const Message& msg )
00217 {
00218   typedef MessageMap::value_type value_type;
00219   m_messageMap.insert( value_type( key, msg ) );
00220 }
00221 
00222 //#############################################################################
00223 // ---------------------------------------------------------------------------
00224 // Routine: eraseMessage
00225 // Purpose: erases all the messages for all the status codes.
00226 // ---------------------------------------------------------------------------
00227 //
00228 
00229 void MessageSvc::eraseMessage()
00230 {
00231   m_messageMap.erase( m_messageMap.begin(), m_messageMap.end() );
00232 }
00233 
00234 //#############################################################################
00235 // ---------------------------------------------------------------------------
00236 // Routine: eraseMessage
00237 // Purpose: erases all the messages for a status code.
00238 // ---------------------------------------------------------------------------
00239 //
00240 
00241 void MessageSvc::eraseMessage( const StatusCode& key )
00242 {
00243   m_messageMap.erase( key );
00244 }
00245 
00246 //#############################################################################
00247 // ---------------------------------------------------------------------------
00248 // Routine: eraseMessage
00249 // Purpose: erases one message for a status code.
00250 // ---------------------------------------------------------------------------
00251 //
00252 
00253 void MessageSvc::eraseMessage( const StatusCode& key, const Message& msg )
00254 {
00255   bool changed = true;
00256   while( changed ) {
00257     changed = false;
00258     MessageMap::iterator first = m_messageMap.lower_bound( key );
00259     MessageMap::iterator last = m_messageMap.upper_bound( key );
00260     while( first != last ) {
00261       const Message& message = (*first).second;
00262       if ( message == msg ) {
00263         m_messageMap.erase( first );
00264         changed = true;
00265         break;
00266       }
00267     }      
00268   }
00269 }
00270 
00271 // ---------------------------------------------------------------------------
00272 StatusCode MessageSvc::queryInterface(const IID& riid, void** ppvInterface) {
00273 // ---------------------------------------------------------------------------
00274   if ( IID_IMessageSvc == riid )  {
00275     *ppvInterface = (IMessageSvc*)this;
00276   }
00277   else  {
00278     return Service::queryInterface(riid, ppvInterface);
00279   }
00280   addRef();
00281   return StatusCode::SUCCESS;
00282 }
00283 
00284 // ---------------------------------------------------------------------------
00285 int MessageSvc::outputLevel()   const {
00286 // ---------------------------------------------------------------------------
00287   return m_outputLevel;
00288 }
00289 // ---------------------------------------------------------------------------
00290 int MessageSvc::outputLevel( const std::string& source )   const {
00291 // ---------------------------------------------------------------------------
00292   ThresholdMap::const_iterator it;
00293 
00294   it = m_thresholdMap.find( source );
00295   if( it != m_thresholdMap.end() ) {
00296     return (*it).second;
00297   }
00298   else {
00299     return m_outputLevel;
00300   }
00301 }
00302 
00303 // ---------------------------------------------------------------------------
00304 void MessageSvc::setOutputLevel(int new_level)    {
00305 // ---------------------------------------------------------------------------
00306   m_outputLevel = new_level;
00307 }
00308 
00309 // ---------------------------------------------------------------------------
00310 void MessageSvc::setOutputLevel(const std::string& source, int level)    {
00311 // ---------------------------------------------------------------------------
00312   std::pair<ThresholdMap::iterator, bool> p;
00313   p = m_thresholdMap.insert(ThresholdMap::value_type( source, level) );
00314   if( p.second == false ) {
00315     // Already esisting an output level for that source. Erase an enter it again
00316     m_thresholdMap.erase ( p.first );
00317     m_thresholdMap.insert(ThresholdMap::value_type( source, level) );
00318   }
00319 }
00320 

Generated at Wed Nov 21 12:22:30 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000