CMS 3D CMS Logo

ELadministrator.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 // ELadministrator.cc
00004 //
00005 // Methods of ELadministrator.
00006 //
00007 // History:
00008 //
00009 // 7/2/98  mf   Created
00010 // 7/6/98  mf   Added ELadministratorX knowledge
00011 // 6/16/99 jvr  Attaches a destination when an error is logged and
00012 //              no destinations are attached          $$ JV:1
00013 // 6/18/99 jvr/mf       Constructor for ELadministratorX uses explcitly
00014 //              constructed ELseverityLevels in its init list, rather
00015 //              than objectslike ELabort, which may not yet have
00016 //              been fully constructed.  Repairs a problem with ELcout.
00017 // 6/23/99  mf  Made emptyContextSUpplier static to this comp unit.
00018 // 12/20/99 mf  Added virtual destructor to ELemptyContextSupplier.
00019 // 2/29/00  mf  swapContextSupplier.
00020 // 4/05/00  mf  swapProcess.
00021 // 5/03/00  mf  When aborting, changed exit() to abort() so that dump
00022 //              will reflect true condition without unwinding things.
00023 // 6/6/00  web  Consolidated ELadministrator/X.
00024 // 6/7/00  web  Reflect consolidation of ELdestination/X.
00025 // 6/12/00 web  Attach cerr, rather than cout, in case of no previously-
00026 //              attached destination; using -> USING.
00027 // 3/6/00  mf   Attach taking name to id the destination, getELdestControl()
00028 // 3/14/01 mf   exitThreshold
00029 // 1/10/06 mf   finish()
00030 //
00031 // ---- CMS version
00032 //
00033 // 12/12/05 mf  change exit() to throw edm::exception
00034 //
00035 //-----------------------------------------------------------------------
00036 
00037 // Directory of methods:
00038 //----------------------
00039 
00040 // ELadministrator::setProcess( const ELstring & process )
00041 // ELadministrator::swapProcess( const ELstring & process )
00042 // ELadministrator::setContextSupplier( const ELcontextSupplier & supplier )
00043 // ELadministrator::getContextSupplier() const
00044 // ELadministrator::swapContextSupplier( ELcontextSupplier & supplier )
00045 // ELadministrator::setAbortThreshold( const ELseverityLevel & sev )
00046 // ELadministrator::setExitThreshold( const ELseverityLevel & sev )
00047 // ELadministrator::attach( const ELdestination & sink )
00048 // ELadministrator::attach( const ELdestination & sink, const ELstring & name )
00049 // ELadministrator::getELdestControl ( const ELstring & name,
00050 //                                      ELdestControl & theDestControl )
00051 // ELadministrator::checkSeverity()
00052 // ELadministrator::severityCount( const ELseverityLevel & sev ) const
00053 // ELadministrator::severityCount( const ELseverityLevel & from,
00054 //                                  const ELseverityLevel & to    ) const
00055 // ELadministrator::resetSeverityCount( const ELseverityLevel & sev )
00056 // ELadministrator::resetSeverityCount( const ELseverityLevel & from,
00057 //                                      const ELseverityLevel & to    )
00058 // ELadministrator::resetSeverityCount()
00059 // ELadministrator::setThresholds( const ELseverityLevel & sev )
00060 // ELadministrator::setLimits( const ELstring & id, int limit )
00061 // ELadministrator::setLimits( const ELseverityLevel & sev, int limit )
00062 // ELadministrator::setIntervals( const ELstring & id, int interval )
00063 // ELadministrator::setIntervals( const ELseverityLevel & sev, int interval )
00064 // ELadministrator::setTimespans( const ELstring & id, int seconds )
00065 // ELadministrator::setTimespans( const ELseverityLevel & sev, int seconds )
00066 // ELadministrator::wipe()
00067 // ELadministrator::finish()
00068 //
00069 // ELadministrator::process() const
00070 // ELadministrator::context() const
00071 // ELadministrator::abortThreshold() const
00072 // ELadministrator::exitThreshold() const
00073 // ELadministrator::sinks()
00074 // ELadministrator::highSeverity() const
00075 // ELadministrator::severityCounts( const int lev ) const
00076 // ELadministrator::finishMsg()
00077 // ELadministrator::clearMsg()
00078 //
00079 // class ELemptyContextSupplier : public ELcontextSupplier
00080 //
00081 // ----------------------------------------------------------------------
00082 
00083 
00084 #include "FWCore/MessageService/interface/ELadministrator.h"
00085 #include "FWCore/MessageService/interface/ELdestination.h"
00086 #include "FWCore/MessageService/interface/ELcontextSupplier.h"
00087 #include "FWCore/MessageService/interface/ELoutput.h"
00088 
00089 #include "FWCore/Utilities/interface/EDMException.h"
00090 
00091 #include <iostream>
00092 #include <sstream>
00093 #include <list>
00094 using std::cerr;
00095 
00096 
00097 namespace edm {
00098 namespace service {
00099 
00100 
00101 // Possible Traces:
00102 // #define ELadministratorCONSTRUCTOR_TRACE
00103 // #define ELadTRACE_FINISH
00104 
00105 
00106 // ----------------------------------------------------------------------
00107 // ELadministrator functionality:
00108 // ----------------------------------------------------------------------
00109 
00110 void ELadministrator::setProcess( const ELstring & process )  {
00111 
00112   process_ = process;
00113   #if 0
00114     std::cerr << "Administrator process set to \"" << process << "\"\n";
00115   #endif
00116 
00117 }  // setProcess()
00118 
00119 
00120 ELstring ELadministrator::swapProcess( const ELstring & process )  {
00121 
00122   ELstring temp = process_;
00123   process_ = process;
00124   return temp;
00125 
00126 } // swapProcess()
00127 
00128 
00129 void ELadministrator::setContextSupplier( const ELcontextSupplier & supplier )  {
00130 
00131   delete context_;
00132   context_ = supplier.clone();
00133 
00134 }  // setContextSupplier()
00135 
00136 
00137 const ELcontextSupplier & ELadministrator::getContextSupplier() const  {
00138 
00139   return *(context_);
00140 
00141 }  // getContextSupplier()
00142 
00143 ELcontextSupplier & ELadministrator::swapContextSupplier
00144                                         (ELcontextSupplier & cs)  {
00145   ELcontextSupplier & save = *(context_);
00146   context_ = &cs;
00147   return save;
00148 } // swapContextSupplier
00149 
00150 
00151 void ELadministrator::setAbortThreshold( const ELseverityLevel & sev )  {
00152 
00153   abortThreshold_ = sev;
00154 
00155 }  // setAbortThreshold()
00156 
00157 void ELadministrator::setExitThreshold( const ELseverityLevel & sev )  {
00158 
00159   exitThreshold_ = sev;
00160 
00161 }  // setExitThreshold()
00162 
00163 ELdestControl ELadministrator::attach( const ELdestination & sink )  {
00164 
00165   ELdestination * dest = sink.clone();
00166   sinks().push_back( dest );
00167   return ELdestControl( dest );
00168 
00169 }  // attach()
00170 
00171 ELdestControl ELadministrator::attach(  const ELdestination & sink,
00172                                         const ELstring & name )     {
00173   ELdestination * dest = sink.clone();
00174   attachedDestinations[name] = dest;
00175   sinks().push_back( dest );
00176   return ELdestControl( dest );
00177 } // attach()
00178 
00179 
00180 bool ELadministrator::getELdestControl ( const ELstring & name,
00181                                          ELdestControl & theDestControl ) {
00182 
00183   if ( attachedDestinations.find(name) != attachedDestinations.end() ) {
00184     theDestControl = ELdestControl ( attachedDestinations[name] );
00185     return true;
00186   } else {
00187     return false;
00188   }
00189 
00190 } // getDestControl
00191 
00192 ELseverityLevel  ELadministrator::checkSeverity()  {
00193 
00194   const ELseverityLevel  retval( highSeverity_ );
00195   highSeverity_ = ELzeroSeverity;
00196   return retval;
00197 
00198 }  // checkSeverity()
00199 
00200 
00201 int ELadministrator::severityCount( const ELseverityLevel & sev ) const  {
00202 
00203   return severityCounts_[sev.getLevel()];
00204 
00205 }  // severityCount()
00206 
00207 
00208 int ELadministrator::severityCount(
00209   const ELseverityLevel & from,
00210   const ELseverityLevel & to
00211 )  const  {
00212 
00213   int k = from.getLevel();
00214   int sum = severityCounts_[k];
00215 
00216   while ( ++k <= to.getLevel() )
00217     sum += severityCounts_[k];
00218 
00219   return  sum;
00220 
00221 }  // severityCount()
00222 
00223 
00224 void ELadministrator::resetSeverityCount( const ELseverityLevel & sev )  {
00225 
00226   severityCounts_[sev.getLevel()] = 0;
00227 
00228 }  // resetSeverityCount()
00229 
00230 
00231 void ELadministrator::resetSeverityCount( const ELseverityLevel & from,
00232                                           const ELseverityLevel & to   )  {
00233 
00234   for ( int k = from.getLevel();  k <= to.getLevel();  ++k )
00235     severityCounts_[k] = 0;
00236 
00237 }  // resetSeverityCount()
00238 
00239 
00240 void ELadministrator::resetSeverityCount()  {
00241 
00242   resetSeverityCount( ELzeroSeverity, ELhighestSeverity );
00243 
00244 }  // resetSeverityCount()
00245 
00246 
00247 // ----------------------------------------------------------------------
00248 // Accessors:
00249 // ----------------------------------------------------------------------
00250 
00251 const ELstring & ELadministrator::process() const  { return process_; }
00252 
00253 
00254 ELcontextSupplier & ELadministrator::context() const  { return *context_; }
00255 
00256 
00257 const ELseverityLevel & ELadministrator::abortThreshold() const  {
00258   return abortThreshold_;
00259 }
00260 
00261 const ELseverityLevel & ELadministrator::exitThreshold() const  {
00262   return exitThreshold_;
00263 }
00264 
00265 std::list<ELdestination *> & ELadministrator::sinks()  { return sinks_; }
00266 
00267 
00268 const ELseverityLevel & ELadministrator::highSeverity() const  {
00269   return highSeverity_;
00270 }
00271 
00272 
00273 int ELadministrator::severityCounts( const int lev ) const  {
00274   return severityCounts_[lev];
00275 }
00276 
00277 
00278 // ----------------------------------------------------------------------
00279 // Message handling:
00280 // ----------------------------------------------------------------------
00281 
00282 static inline void msgexit(int s) {
00283   std::ostringstream os;
00284   os << "msgexit - MessageLogger requested to exit with status " << s;
00285   edm::Exception e(edm::errors::LogicError, os.str());
00286   throw e;
00287 }
00288 
00289 static inline void msgabort() {
00290   std::ostringstream os;
00291   os << "msgabort - MessageLogger requested to abort";
00292   edm::Exception e(edm::errors::LogicError, os.str());
00293   throw e;
00294 }
00295 
00296 static inline void possiblyAbortOrExit (int s, int a, int e) {
00297   if (s < a && s < e) return;
00298   if (a < e) {
00299     if ( s < e ) msgabort();
00300     msgexit(s);
00301   } else {
00302     if ( s < a ) msgexit(s);
00303     msgabort();
00304   }
00305 }
00306 
00307 void ELadministrator::finishMsg()  {
00308 
00309   if ( ! msgIsActive )
00310     return;
00311 
00312   int lev = msg.xid().severity.getLevel();
00313   ++ severityCounts_[lev];
00314   if ( lev > highSeverity_.getLevel() )
00315     highSeverity_ = msg.xid().severity;
00316 
00317   #ifdef ELadTRACE_FINISH
00318     std::cerr << "=:=:=: finshMsg() - lev = " << lev << "\n";
00319   #endif
00320 
00321   context_->editErrorObj( msg );
00322 
00323   #ifdef ELadTRACE_FINISH
00324     std::cerr << "=:=:=: finshMsg() returns from editErrorObj( msg ) \n";
00325   #endif
00326 
00327   std::list<ELdestination *>::iterator d;
00328   bool mrt;
00329   #ifdef ELadTRACE_FINISH
00330     int destCounter = 0;
00331   #endif
00332   if ( sinks().begin() == sinks().end() )  {                   // $$ JV:1
00333     std::cerr << "\nERROR LOGGED WITHOUT DESTINATION!\n";
00334     std::cerr << "Attaching destination \"cerr\" to ELadministrator by default\n\n";
00335     ELdestination * dest = new ELoutput(cerr);
00336     this->sinks().push_back(dest);
00337   }
00338   for ( d = sinks().begin();  d != sinks().end();  ++d )  {
00339     #ifdef ELadTRACE_FINISH
00340       std::cerr << "  =:=:=: log(msg) for a destination number "
00341            << ++destCounter << " called ... \n";
00342     #endif
00343     mrt = (*d)->log( msg );
00344     #ifdef ELadTRACE_FINISH
00345       std::cerr << "  =:=:=: log(msg) for a destination returned " << mrt << "\n";
00346     #endif
00347     if ( mrt )
00348       msg.setReactedTo(true);
00349   }
00350 
00351   msgIsActive = false;
00352 
00353   possiblyAbortOrExit ( lev,
00354                         abortThreshold().getLevel(),
00355                         exitThreshold().getLevel()   );         // $$ mf 3/17/04
00356 
00357 }  // finishMsg()
00358 
00359 
00360 void ELadministrator::clearMsg()  {
00361 
00362   msgIsActive = false;
00363   msg.clear();
00364 
00365 }  // clearMsg()
00366 
00367 
00368 // ----------------------------------------------------------------------
00369 // The following do the indicated action to all attached destinations:
00370 // ----------------------------------------------------------------------
00371 
00372 void ELadministrator::setThresholds( const ELseverityLevel & sev )  {
00373 
00374   std::list<ELdestination *>::iterator d;
00375   for ( d = sinks().begin();  d != sinks().end();  ++d )
00376     (*d)->threshold = sev;
00377 
00378 }  // setThresholds()
00379 
00380 
00381 void ELadministrator::setLimits( const ELstring & id, int limit )  {
00382 
00383   std::list<ELdestination *>::iterator d;
00384   for ( d = sinks().begin();  d != sinks().end();  ++d )
00385     (*d)->limits.setLimit( id, limit );
00386 
00387 }  // setLimits()
00388 
00389 
00390 void ELadministrator::setIntervals
00391                         ( const ELseverityLevel & sev, int interval )  {
00392 
00393   std::list<ELdestination *>::iterator d;
00394   for ( d = sinks().begin();  d != sinks().end();  ++d )
00395     (*d)->limits.setInterval( sev, interval );
00396 
00397 }  // setIntervals()
00398 
00399 void ELadministrator::setIntervals( const ELstring & id, int interval )  {
00400 
00401   std::list<ELdestination *>::iterator d;
00402   for ( d = sinks().begin();  d != sinks().end();  ++d )
00403     (*d)->limits.setInterval( id, interval );
00404 
00405 }  // setIntervals()
00406 
00407 
00408 void ELadministrator::setLimits( const ELseverityLevel & sev, int limit )  {
00409 
00410   std::list<ELdestination *>::iterator d;
00411   for ( d = sinks().begin();  d != sinks().end();  ++d )
00412     (*d)->limits.setLimit( sev, limit );
00413 
00414 }  // setLimits()
00415 
00416 
00417 void ELadministrator::setTimespans( const ELstring & id, int seconds )  {
00418 
00419   std::list<ELdestination *>::iterator d;
00420   for ( d = sinks().begin();  d != sinks().end();  ++d )
00421     (*d)->limits.setTimespan( id, seconds );
00422 
00423 }  // setTimespans()
00424 
00425 
00426 void ELadministrator::setTimespans( const ELseverityLevel & sev, int seconds )  {
00427 
00428   std::list<ELdestination *>::iterator d;
00429   for ( d = sinks().begin();  d != sinks().end();  ++d )
00430     (*d)->limits.setTimespan( sev, seconds );
00431 
00432 }  // setTimespans()
00433 
00434 
00435 void ELadministrator::wipe()  {
00436 
00437   std::list<ELdestination *>::iterator d;
00438   for ( d = sinks().begin();  d != sinks().end();  ++d )
00439     (*d)->limits.wipe();
00440 
00441 }  // wipe()
00442 
00443 void ELadministrator::finish()  {
00444 
00445   std::list<ELdestination *>::iterator d;
00446   for ( d = sinks().begin();  d != sinks().end();  ++d )
00447     (*d)->finish();
00448 
00449 }  // wipe()
00450 
00451 
00452 // ----------------------------------------------------------------------
00453 // ELemptyContextSupplier (dummy default ELcontextSupplier):
00454 // ----------------------------------------------------------------------
00455 
00456 class ELemptyContextSupplier : public ELcontextSupplier  {
00457 
00458 public:
00459 
00460   virtual
00461   ELemptyContextSupplier *
00462   clone() const  {
00463     return new ELemptyContextSupplier( *this );
00464   }
00465   virtual ELstring context()        const  {  return ""; }
00466   virtual ELstring summaryContext() const  {  return ""; }
00467   virtual ELstring fullContext()    const  {  return ""; }
00468 
00469   virtual ~ELemptyContextSupplier()  {}
00470 
00471 };  // ELemptyContextSupplier
00472 
00473 
00474 static ELemptyContextSupplier  emptyContext;
00475 
00476 
00477 // ----------------------------------------------------------------------
00478 // The Destructable Singleton pattern
00479 // (see "To Kill a Singleton," Vlissides, C++ Report):
00480 // ----------------------------------------------------------------------
00481 
00482 
00483 ELadministrator * ELadministrator::instance_ = 0;
00484 
00485 
00486 ELadministrator * ELadministrator::instance()  {
00487 
00488   static ELadminDestroyer destroyer_;
00489   // This deviates from Vlissides' pattern where destroyer_ was a static
00490   // instance in the ELadministrator class.  This construct should be
00491   // equivalent, but the original did not call the destructor under KCC.
00492 
00493   if ( !instance_ )  {
00494     instance_ = new ELadministrator;
00495     destroyer_.setELadmin( instance_ );
00496   }
00497   return instance_;
00498 
00499 }  // instance()
00500 
00501 
00502 ELadministrator::ELadministrator()
00503 : process_       ( ""                                                        )
00504 , context_       ( emptyContext.clone()                                      )
00505 , abortThreshold_( ELseverityLevel (ELseverityLevel::ELsev_abort)            )
00506 , exitThreshold_ ( ELseverityLevel (ELseverityLevel::ELsev_highestSeverity   )            )
00507 , sinks_         (                                                           )
00508 , highSeverity_  ( ELseverityLevel (ELseverityLevel::ELsev_zeroSeverity)     )
00509 , msg            ( ELseverityLevel (ELseverityLevel::ELsev_unspecified), ""  )
00510 , msgIsActive    ( false                                                     )
00511 {
00512 
00513   #ifdef ELadministratorCONSTRUCTOR_TRACE
00514     std::cerr << "ELadminstrator constructor\n";
00515   #endif
00516 
00517   for ( int lev = 0;  lev < ELseverityLevel::nLevels;  ++lev )
00518     severityCounts_[lev] = 0;
00519 
00520 }  // ELadministrator()
00521 
00522 
00523 ELadminDestroyer::ELadminDestroyer( ELadministrator * ad )  : admin_( ad )  {}
00524 
00525 
00526 ELadminDestroyer::~ELadminDestroyer()  {
00527 
00528   #ifdef ELadministratorCONSTRUCTOR_TRACE
00529     std::cerr << "~ELadminDestroyer: Deleting admin_\n";
00530   #endif
00531 
00532   delete admin_;
00533 
00534 }  // ~ELadminDestroyer()
00535 
00536 
00537 void ELadminDestroyer::setELadmin( ELadministrator * ad )  { admin_ = ad; }
00538 
00539 
00540 //-*****************************
00541 // The ELadminstrator destructor
00542 //-*****************************
00543 
00544 ELadministrator::~ELadministrator()  {
00545 
00546   #ifdef ELadministratorCONSTRUCTOR_TRACE
00547     std::cerr << "ELadministrator Destructor\n";
00548   #endif
00549 
00550   finishMsg();
00551 
00552   while ( sinks().size() > 0 )  {
00553     delete sinks().back();
00554     sinks().pop_back();
00555   }
00556 
00557   delete context_;
00558 
00559 }  // ~ELadministrator()
00560 
00561 
00562 
00563 // ----------------------------------------------------------------------
00564 
00565 
00566 } // end of namespace service  
00567 } // end of namespace edm  

Generated on Tue Jun 9 17:36:18 2009 for CMSSW by  doxygen 1.5.4