CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/FWCore/MessageService/src/ELtsErrorLog.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 // ELtsErrorLog.cc
00004 //
00005 // Created 5/31/01 mf   Base implementations for all the templates in
00006 //                      ThreadSafeErrorLog<Mutex>
00007 //
00008 // 11/15/01 mf  static_cast to unsigned int and long in comparisons in
00009 //              operator<<( ErrorLog & e, unsigned int n) and long, and
00010 //              also rwriting 0xFFFFFFFF as 0xFFFFFFFFL when comparing to a
00011 //              long.  THese cure warnings when -Wall -pedantic are turned on.
00012 // 3/06/01 mf   getELdestControl() which forwards to *a
00013 //
00014 // ----------------------------------------------------------------------
00015 
00016 #include "FWCore/MessageService/interface/ELtsErrorLog.h"
00017 #include "FWCore/MessageService/interface/ELadministrator.h"
00018 #include "FWCore/MessageService/interface/ELoutput.h"
00019 #include "FWCore/MessageService/interface/ELcontextSupplier.h"
00020 
00021 #include "FWCore/Utilities/interface/EDMException.h"
00022 
00023 #include <iostream>
00024 #include <iomanip>
00025 #include <sstream>
00026 
00027 
00028 // Possible Traces:
00029 // #define ErrorLogCONSTRUCTOR_TRACE
00030 // #define ErrorLogOUTPUT_TRACE
00031 // #define ErrorLogENDMSG_TRACE
00032 #ifdef ErrorLogEMIT_TRACE
00033   #include <string>
00034 #endif
00035 
00036 namespace edm {
00037 namespace service {
00038 
00039 
00040 // ----------------------------------------------------------------------
00041 // Birth and Death:
00042 // ----------------------------------------------------------------------
00043 
00044 ELtsErrorLog::ELtsErrorLog()
00045 : a( ELadministrator::instance() )
00046 , e()
00047 , process("")
00048 , msgIsActive (false)
00049 , msg(ELunspecified, "...")
00050 {
00051   #ifdef ErrorLogCONSTRUCTOR_TRACE
00052     std::cout << "Constructor for ThreadSafeErrorLog\n";
00053   #endif
00054 }
00055 
00056 ELtsErrorLog::ELtsErrorLog( const ELstring & pkgName )
00057 : a( ELadministrator::instance() )
00058 , e(pkgName)
00059 , process("")
00060 , msgIsActive (false)
00061 , msg(ELunspecified, "...")
00062 {
00063   #ifdef ErrorLogCONSTRUCTOR_TRACE
00064     std::cout << "Constructor for ThreadSafeErrorLog (with pkgName = "
00065                                                         << pkgName << ")\n";
00066   #endif
00067 }
00068 
00069 ELtsErrorLog::ELtsErrorLog( const ErrorLog & ee )
00070 : a( ELadministrator::instance() )
00071 , e(ee)
00072 , process("")
00073 , msgIsActive (false)
00074 , msg(ELunspecified, "...")
00075 {
00076   #ifdef ErrorLogCONSTRUCTOR_TRACE
00077     std::cout << "Constructor for ThreadSafeErrorLog from ErrorLog\n";
00078   #endif
00079 }
00080 
00081 ELtsErrorLog::ELtsErrorLog( const ELtsErrorLog & ee)
00082 : a( ELadministrator::instance() )
00083 , e(ee.e)
00084 , process(ee.process)
00085 , msgIsActive (ee.msgIsActive)
00086 , msg(ee.msg)
00087 {
00088   #ifdef ErrorLogCONSTRUCTOR_TRACE
00089     std::cout << "Copy constructor for ThreadSafeErrorLog \n";
00090   #endif
00091 }
00092 
00093 ELtsErrorLog::~ELtsErrorLog()
00094 {
00095   #ifdef ErrorLogCONSTRUCTOR_TRACE
00096     std::cout << "Destructor for ThreadSafeErrorLog\n";
00097   #endif
00098 }
00099 
00100 void ELtsErrorLog::swap( ELtsErrorLog& other )
00101 {
00102   std::swap(a, other.a);
00103   std::swap(e, other.e);
00104   process.swap(other.process);
00105   std::swap(msgIsActive, other.msgIsActive);
00106   msg.swap(other.msg);
00107 }
00108 
00109 ELtsErrorLog& ELtsErrorLog::operator=( const ELtsErrorLog& other)
00110 {
00111   ELtsErrorLog temp(other);
00112   this->swap(temp);
00113   return *this;
00114 }
00115 
00116 // ----------------------------------------------------------------------
00117 // Setup for preamble parts
00118 // ----------------------------------------------------------------------
00119 
00120 void ELtsErrorLog::setSubroutine( const ELstring & subName )  {
00121   e.setSubroutine (subName);
00122 }  // setSubroutine()
00123 
00124 void ELtsErrorLog::setModule( const ELstring & modName )  {
00125   e.setModule (modName);
00126 }  // setModule()
00127 
00128 void ELtsErrorLog::setPackage( const ELstring & pkgName )  {
00129   e.setModule (pkgName);
00130 }  // setPackage()
00131 
00132 void ELtsErrorLog::setProcess( const ELstring & procName )  {
00133   process = procName;
00134 }  // setProcess()
00135 
00136 int ELtsErrorLog::setHexTrigger (int trigger) {
00137   return e.setHexTrigger (trigger);
00138 }
00139 
00140 ELseverityLevel ELtsErrorLog::setDiscardThreshold (ELseverityLevel sev) {
00141   return e.setDiscardThreshold(sev);
00142 }
00143 
00144 void ELtsErrorLog::setDebugVerbosity (int debugVerbosity) {
00145   e.setDebugVerbosity (debugVerbosity);
00146 }
00147 
00148 void ELtsErrorLog::setDebugMessages (ELseverityLevel sev, ELstring id) {
00149   e.setDebugMessages (sev, id);
00150 }
00151 
00152 // ----------------------------------------------------------------------
00153 // Recovery of an ELdestControl handle
00154 // ----------------------------------------------------------------------
00155 
00156 bool ELtsErrorLog::getELdestControl (const ELstring & name,
00157                                      ELdestControl & theDestControl) const {
00158     return a->getELdestControl ( name, theDestControl );
00159 }
00160 
00161 
00162 // ----------------------------------------------------------------------
00163 // Message Initiation
00164 // ----------------------------------------------------------------------
00165 
00166 void ELtsErrorLog::initiateMsg(const ELseverityLevel& sev, const ELstring& id)
00167 {
00168   if ( sev < e.discardThreshold ) {
00169     e.discarding = true;
00170     return;
00171   }
00172   e.discarding = false;
00173   #ifdef ErrorLogENDMSG_TRACE
00174     std::cout << "=:=:=: precautionary endmsg ( "
00175               << sev.getName() << ", " << id << ")\n";
00176   #endif
00177 
00178   // Unlike the case for ErrorLog, it is not necessary to check
00179   // msgIsActive because the calling code was forced to do that
00180   // (since if it WAS active, ELtsErrorLog can't do the Mutex LOCK.)
00181 
00182   // -----  form ErrorObj for this new message:
00183   //
00184   msg.clear();
00185   msgIsActive = true;
00186   msg.set          ( sev, id );
00187   msg.setProcess   ( process );
00188   msg.setModule    ( e.module );
00189   msg.setSubroutine( e.subroutine );
00190   msg.setReactedTo ( false );
00191 
00192   return;
00193 
00194 }  // operator()( )
00195 
00196 // ----------------------------------------------------------------------
00197 // Message Continuation:
00198 //   item() method used by emitToken(string)
00199 // ----------------------------------------------------------------------
00200 
00201 
00202 void ELtsErrorLog::item ( const ELstring & s ) {
00203   if ( ! msgIsActive )
00204     initiateMsg ( ELunspecified, "..." );
00205   msg.emitToken( s );
00206 }  // emitToken()
00207 
00208 // ----------------------------------------------------------------------
00209 // Message Continuation:
00210 //   item() methods used by operator<< for integer types
00211 // ----------------------------------------------------------------------
00212 
00213 void ELtsErrorLog::item ( int n ) {
00214   if (e.discarding) return;
00215   std::ostringstream  ost;
00216   ost << n << ' ';
00217   int m = (n<0) ? -n : n;
00218   if ( (e.hexTrigger >= 0) && (m >= e.hexTrigger) ) {
00219     ost << "[0x"
00220         << std::hex << std::setw(8) << std::setfill('0')
00221         << n << "] ";
00222   }
00223   msg.emitToken( ost.str() );
00224 }
00225 
00226 void ELtsErrorLog::item ( unsigned int n )  {
00227   if (e.discarding) return;
00228   std::ostringstream  ost;
00229   ost << n << ' ';
00230   if ( (e.hexTrigger >= 0) &&
00231        (n >= static_cast<unsigned int>(e.hexTrigger)) ) {
00232     ost << "[0x"
00233         << std::hex << std::setw(8) << std::setfill('0')
00234         << n << "] ";
00235   }
00236   msg.emitToken( ost.str() );
00237 }
00238 
00239 void ELtsErrorLog::item ( long n )  {
00240   if (e.discarding) return;
00241   std::ostringstream  ost;
00242   ost << n << ' ';
00243   long m = (n<0) ? -n : n;
00244   if ( (e.hexTrigger >= 0) && (m >= e.hexTrigger) ) {
00245     int width = 8;
00246     if ( static_cast<unsigned long>(n) > 0xFFFFFFFFL ) width = 16;
00247     ost << "[0x"
00248         << std::hex << std::setw(width) << std::setfill('0')
00249         << n << "] ";
00250   }
00251   msg.emitToken( ost.str() );
00252 }
00253 
00254 void ELtsErrorLog::item ( unsigned long n )  {
00255   if (e.discarding) return;
00256   std::ostringstream  ost;
00257   ost << n << ' ';
00258   if ( (e.hexTrigger >= 0) &&
00259        (n >= static_cast<unsigned long>(e.hexTrigger)) ) {
00260     int width = 8;
00261     if ( n > 0xFFFFFFFFL ) width = 16;
00262     ost << "[0x"
00263         << std::hex << std::setw(width) << std::setfill('0')
00264         << n << "] ";
00265   }
00266   msg.emitToken( ost.str() );
00267 }
00268 
00269 void ELtsErrorLog::item ( short n )  {
00270   if (e.discarding) return;
00271   std::ostringstream  ost;
00272   ost << n << ' ';
00273   short m = (n<0) ? -n : n;
00274   if ( (e.hexTrigger >= 0) && (m >= e.hexTrigger) ) {
00275     ost << "[0x"
00276         << std::hex << std::setw(4) << std::setfill('0')
00277         << n << "] ";
00278   }
00279   msg.emitToken( ost.str() );
00280 }
00281 
00282 void ELtsErrorLog::item ( unsigned short n )  {
00283   if (e.discarding) return;
00284   std::ostringstream  ost;
00285   ost << n << ' ';
00286   if ( (e.hexTrigger >= 0) && (n >= e.hexTrigger) ) {
00287     ost << "[0x"
00288         << std::hex << std::setw(4) << std::setfill('0')
00289         << n << "] ";
00290   }
00291   msg.emitToken( ost.str() );
00292 }
00293 
00294 // ----------------------------------------------------------------------
00295 // Message Completion:
00296 // ----------------------------------------------------------------------
00297 
00298 bool ELtsErrorLog::pokeMsg ( edm::ErrorObj & msg )  {
00299 
00300   // -----  will we need to poke/restore info into the message?
00301   //
00302   bool updateProcess   ( msg.xid().process   .length() == 0 );
00303   bool updateModule    ( msg.xid().module    .length() == 0 );
00304   bool updateSubroutine( msg.xid().subroutine.length() == 0 );
00305 
00306   // -----  poke, if needed:
00307   //
00308   if ( updateProcess    )  msg.setProcess   ( process );
00309   if ( updateModule     )  msg.setModule    ( e.module );
00310   if ( updateSubroutine )  msg.setSubroutine( e.subroutine );
00311 
00312   return ( updateProcess || updateModule || updateSubroutine );
00313 
00314 }
00315 
00316 static inline void msgabort() {
00317   edm::Exception e(edm::errors::LogicError, 
00318         "msgabort - MessageLogger tsErrorLog requested to abort");
00319   throw e;
00320 }
00321 
00322 void ELtsErrorLog::dispatch ( edm::ErrorObj & msg )  {
00323 
00324   // NOTE -- this is never called except in cases where a <Mutex> LOCK
00325   //         is in scope.  That is, this code should be treated as a
00326   //         critical section.
00327 
00328   // severity level statistics keeping:
00329   int lev = msg.xid().severity.getLevel();
00330   ++ a->severityCounts_[lev];
00331   if ( lev > a->highSeverity_.getLevel() )
00332     a->highSeverity_ = msg.xid().severity;
00333 
00334   // context-based editing (if specified; usually just returns)
00335   a->context_->editErrorObj( msg );
00336 
00337   // -----  send the message to each destination:
00338   //
00339   if (a->sinks().begin() == a->sinks().end())  {
00340     std::cerr << "\nERROR LOGGED WITHOUT DESTINATION!\n";
00341     std::cerr << "Attaching destination \"cerr\" to ELadministrator by default\n"
00342               << std::endl;
00343     a->attach(ELoutput(std::cerr));
00344   }
00345   std::list<boost::shared_ptr<ELdestination> >::iterator d;
00346   for ( d = a->sinks().begin();  d != a->sinks().end();  ++d )
00347     if (  (*d)->log( msg )  )
00348       msg.setReactedTo (true );
00349 
00350 
00351   if ( msg.xid().severity.getLevel() >= a->abortThreshold().getLevel()
00352                        &&
00353         a->abortThreshold() != ELhighestSeverity) {
00354     msgabort();
00355   }
00356 
00357 }
00358 
00359 } // end of namespace service  
00360 } // end of namespace edm