Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
// Include files
#include <cstdio>
#include <cstring>   // fix bug #58581
#include <iostream>  // fix bug #58581
#include <memory>

// Local include files
#include "CondCore/CondDB/interface/Logger.h"
#include "CoralMsgReporter.h"

cond::persistency::MsgDispatcher::MsgDispatcher(Logger& logger) { m_recipient = &logger; }

void cond::persistency::MsgDispatcher::unsubscribe() { m_recipient = nullptr; }

bool cond::persistency::MsgDispatcher::hasRecipient() { return m_recipient != nullptr; }

cond::persistency::Logger& cond::persistency::MsgDispatcher::recipient() { return *m_recipient; }

/// Default constructor
cond::persistency::CoralMsgReporter::CoralMsgReporter()
    : m_dispatcher(), m_level(coral::Error), m_format(0), m_mutex() {
  // Use a non-default format?
  //char* msgformat = getenv ( "CORAL_MSGFORMAT" );
  if (getenv("CORAL_MESSAGEREPORTER_FORMATTED"))
    m_format = 1;

  // Use a non-default message level?
  if (getenv("CORAL_MSGLEVEL")) {
    // Check only the first char of the environment variable
    [[clang::suppress]]
    switch (*getenv("CORAL_MSGLEVEL")) {
      case '0':
      case 'n':
      case 'N':
        m_level = coral::Nil;
        break;

      case '1':
      case 'v':
      case 'V':
        m_level = coral::Verbose;
        break;

      case '2':
      case 'd':
      case 'D':
        m_level = coral::Debug;
        break;

      case '3':
      case 'i':
      case 'I':
        m_level = coral::Info;
        break;

      case '4':
      case 'w':
      case 'W':
        m_level = coral::Warning;
        break;

      case '5':
      case 'e':
      case 'E':
        m_level = coral::Error;
        break;

      case '6':
      case 'f':
      case 'F':
        m_level = coral::Fatal;
        break;

      case '7':
      case 'a':
      case 'A':
        m_level = coral::Always;
        break;

      default:
        break;  // keep the default
    }
  }
}

/// Access output level
coral::MsgLevel cond::persistency::CoralMsgReporter::outputLevel() const { return m_level; }

/// Modify output level
void cond::persistency::CoralMsgReporter::setOutputLevel(coral::MsgLevel lvl) { m_level = lvl; }

void reportToRecipient(const std::string& msg, int lvl, cond::persistency::Logger& recipient) {
  switch (lvl) {
    case coral::Nil:
    case coral::Verbose:
    case coral::Debug:
      recipient.logDebug() << "CORAL: " << msg;
      break;
    case coral::Info:
      recipient.logInfo() << "CORAL: " << msg;
      break;
    case coral::Warning:
      recipient.logWarning() << "CORAL: " << msg;
      break;
    case coral::Error:
      recipient.logError() << "CORAL: " << msg;
      break;
  }
}

/// Report message to stdout
void cond::persistency::CoralMsgReporter::report(int lvl, const std::string&, const std::string& msg) {
  if (lvl < m_level)
    return;
  std::lock_guard<std::recursive_mutex> lock(m_mutex);

  if (m_dispatcher.get() && m_dispatcher->hasRecipient()) {
    reportToRecipient(msg, lvl, m_dispatcher->recipient());
  }
  // Default CORAL reporter
  std::string level("");
  switch (lvl) {
    case coral::Nil:
      level = "Nil";
      break;
    case coral::Verbose:
      level = "Verbose";
      break;
    case coral::Debug:
      level = "Debug";
      break;
    case coral::Info:
      level = "Info";
      break;
    case coral::Warning:
      level = "Warning";
      break;
    case coral::Error:
      level = "Error";
      break;
  }
  std::cout << msg << " " << level << " " << msg << std::endl;
}

void cond::persistency::CoralMsgReporter::subscribe(Logger& logger) {
  m_dispatcher = std::make_shared<MsgDispatcher>(logger);
  std::weak_ptr<MsgDispatcher> callBack(m_dispatcher);
  logger.subscribeCoralMessages(callBack);
}