Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:45:50

0001 // Include files
0002 #include <cstdio>
0003 #include <cstring>   // fix bug #58581
0004 #include <iostream>  // fix bug #58581
0005 
0006 // Local include files
0007 #include "CondCore/CondDB/interface/Logger.h"
0008 #include "CoralMsgReporter.h"
0009 
0010 cond::persistency::MsgDispatcher::MsgDispatcher(Logger& logger) { m_recipient = &logger; }
0011 
0012 void cond::persistency::MsgDispatcher::unsubscribe() { m_recipient = nullptr; }
0013 
0014 bool cond::persistency::MsgDispatcher::hasRecipient() { return m_recipient != nullptr; }
0015 
0016 cond::persistency::Logger& cond::persistency::MsgDispatcher::recipient() { return *m_recipient; }
0017 
0018 /// Default constructor
0019 cond::persistency::CoralMsgReporter::CoralMsgReporter()
0020     : m_dispatcher(), m_level(coral::Error), m_format(0), m_mutex() {
0021   // Use a non-default format?
0022   //char* msgformat = getenv ( "CORAL_MSGFORMAT" );
0023   if (getenv("CORAL_MESSAGEREPORTER_FORMATTED"))
0024     m_format = 1;
0025 
0026   // Use a non-default message level?
0027   if (getenv("CORAL_MSGLEVEL")) {
0028     // Check only the first char of the environment variable
0029     switch (*getenv("CORAL_MSGLEVEL")) {
0030       case '0':
0031       case 'n':
0032       case 'N':
0033         m_level = coral::Nil;
0034         break;
0035 
0036       case '1':
0037       case 'v':
0038       case 'V':
0039         m_level = coral::Verbose;
0040         break;
0041 
0042       case '2':
0043       case 'd':
0044       case 'D':
0045         m_level = coral::Debug;
0046         break;
0047 
0048       case '3':
0049       case 'i':
0050       case 'I':
0051         m_level = coral::Info;
0052         break;
0053 
0054       case '4':
0055       case 'w':
0056       case 'W':
0057         m_level = coral::Warning;
0058         break;
0059 
0060       case '5':
0061       case 'e':
0062       case 'E':
0063         m_level = coral::Error;
0064         break;
0065 
0066       case '6':
0067       case 'f':
0068       case 'F':
0069         m_level = coral::Fatal;
0070         break;
0071 
0072       case '7':
0073       case 'a':
0074       case 'A':
0075         m_level = coral::Always;
0076         break;
0077 
0078       default:
0079         break;  // keep the default
0080     }
0081   }
0082 }
0083 
0084 /// Access output level
0085 coral::MsgLevel cond::persistency::CoralMsgReporter::outputLevel() const { return m_level; }
0086 
0087 /// Modify output level
0088 void cond::persistency::CoralMsgReporter::setOutputLevel(coral::MsgLevel lvl) { m_level = lvl; }
0089 
0090 void reportToRecipient(const std::string& msg, int lvl, cond::persistency::Logger& recipient) {
0091   switch (lvl) {
0092     case coral::Nil:
0093     case coral::Verbose:
0094     case coral::Debug:
0095       recipient.logDebug() << "CORAL: " << msg;
0096       break;
0097     case coral::Info:
0098       recipient.logInfo() << "CORAL: " << msg;
0099       break;
0100     case coral::Warning:
0101       recipient.logWarning() << "CORAL: " << msg;
0102       break;
0103     case coral::Error:
0104       recipient.logError() << "CORAL: " << msg;
0105       break;
0106   }
0107 }
0108 
0109 /// Report message to stdout
0110 void cond::persistency::CoralMsgReporter::report(int lvl, const std::string&, const std::string& msg) {
0111   if (lvl < m_level)
0112     return;
0113   std::lock_guard<std::recursive_mutex> lock(m_mutex);
0114 
0115   if (m_dispatcher.get() && m_dispatcher->hasRecipient()) {
0116     reportToRecipient(msg, lvl, m_dispatcher->recipient());
0117   }
0118   // Default CORAL reporter
0119   std::string level("");
0120   switch (lvl) {
0121     case coral::Nil:
0122       level = "Nil";
0123       break;
0124     case coral::Verbose:
0125       level = "Verbose";
0126       break;
0127     case coral::Debug:
0128       level = "Debug";
0129       break;
0130     case coral::Info:
0131       level = "Info";
0132       break;
0133     case coral::Warning:
0134       level = "Warning";
0135       break;
0136     case coral::Error:
0137       level = "Error";
0138       break;
0139   }
0140   std::cout << msg << " " << level << " " << msg << std::endl;
0141 }
0142 
0143 void cond::persistency::CoralMsgReporter::subscribe(Logger& logger) {
0144   m_dispatcher.reset(new MsgDispatcher(logger));
0145   std::weak_ptr<MsgDispatcher> callBack(m_dispatcher);
0146   logger.subscribeCoralMessages(callBack);
0147 }