Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-04 00:29:23

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