Belle II Software  release-05-02-19
LogSystem.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2020 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Thomas Kuhr, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/logging/LogConfig.h>
14 #include <framework/logging/LogMessage.h>
15 
16 #include <string>
17 #include <vector>
18 #include <map>
19 #include <unordered_map>
20 
21 
22 namespace Belle2 {
27  class LogConnectionBase;
28 
48  class LogSystem {
49  public:
50  static const unsigned int c_errorSummaryMaxLines = 10000;
57  static LogSystem& Instance();
58 
67  void addLogConnection(LogConnectionBase* logConnection);
68 
72  void resetLogConnections();
73 
75  void resetLogging();
76 
80  LogConfig* getLogConfig() {return &m_logConfig;}
81 
89  void addPackageLogConfig(const std::string& package, const LogConfig& logConfig) { m_packageLogConfigs[package] = logConfig; }
90 
98  LogConfig& getPackageLogConfig(const std::string& package) { return m_packageLogConfigs[package]; }
99 
107  bool isLevelEnabled(LogConfig::ELogLevel level, int debugLevel = 0, const char* package = nullptr) const;
108 
118  bool sendMessage(LogMessage&& message);
119 
121  void resetMessageCounter();
122 
129  int getMessageCounter(LogConfig::ELogLevel logLevel) const;
130 
141  const LogConfig& getCurrentLogConfig(const char* package = nullptr) const;
142 
148  LogConfig::ELogLevel getCurrentLogLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getLogLevel(); }
149 
155  int getCurrentDebugLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getDebugLevel(); }
156 
162  unsigned getMaxMessageRepetitions() const { return m_maxErrorRepetition; }
163 
169  void setMaxMessageRepetitions(unsigned repetitions) { m_maxErrorRepetition = repetitions; }
170 
176  void printErrorSummary();
177 
183  void enableErrorSummary(bool on) { m_printErrorSummary = on; }
184 
193  void updateModule(const LogConfig* moduleLogConfig = nullptr, const std::string& moduleName = "") { m_moduleLogConfig = moduleLogConfig; m_moduleName = moduleName; }
194 
198  static void enableDebug() {s_debugEnabled = true;}
199 
203  inline static bool debugEnabled() {return s_debugEnabled;}
204 
205  private:
207  std::vector<LogConnectionBase*> m_logConnections;
209  LogConfig m_logConfig;
213  std::string m_moduleName;
215  std::map<std::string, LogConfig> m_packageLogConfigs;
219  std::unordered_map<LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasher> m_messageLog{100};
221  unsigned int m_maxErrorRepetition{0};
223  unsigned int m_suppressedMessages{0};
227  static bool s_debugEnabled;
228 
230  LogSystem();
231 
233  LogSystem(const LogSystem&) = delete;
234 
236  LogSystem& operator=(const LogSystem&) = delete;
237 
239  ~LogSystem();
240 
247 
249  bool deliverMessageToConnections(const LogMessage& msg);
250 
257  void showText(LogConfig::ELogLevel level, const std::string& txt, int info = LogConfig::c_Message);
258  };
259 
260  inline const LogConfig& LogSystem::getCurrentLogConfig(const char* package) const
261  {
262  //module specific config?
264  return *m_moduleLogConfig;
265  }
266  //package specific config?
267  if (package && !m_packageLogConfigs.empty()) {
268  // cppcheck-suppress stlIfFind ; cppcheck doesn't like scoped variables in if statements
269  if (auto it = m_packageLogConfigs.find(package); it != m_packageLogConfigs.end()) {
270  const LogConfig& logConfig = it->second;
271  if (logConfig.getLogLevel() != LogConfig::c_Default)
272  return logConfig;
273  }
274  }
275  //global config
276  return m_logConfig;
277  }
278 
279  inline bool LogSystem::isLevelEnabled(LogConfig::ELogLevel level, int debugLevel, const char* package) const
280  {
281  const LogConfig& config = getCurrentLogConfig(package);
282  const LogConfig::ELogLevel logLevelLimit = config.getLogLevel();
283  const int debugLevelLimit = config.getDebugLevel();
284  return logLevelLimit <= level && (level != LogConfig::c_Debug || debugLevelLimit >= debugLevel);
285  }
286 
288 } //end of namespace Belle2
Belle2::LogSystem::addPackageLogConfig
void addPackageLogConfig(const std::string &package, const LogConfig &logConfig)
Add the per package log configuration.
Definition: LogSystem.h:97
Belle2::LogConfig::getDebugLevel
int getDebugLevel() const
Returns the configured debug messaging level.
Definition: LogConfig.h:101
Belle2::LogSystem::debugEnabled
static bool debugEnabled()
Is debug output enabled?
Definition: LogSystem.h:211
Belle2::LogSystem::m_suppressedMessages
unsigned int m_suppressedMessages
The amount of messages we have suppressed so far just to get an indication we print this from time to...
Definition: LogSystem.h:231
Belle2::LogSystem::enableDebug
static void enableDebug()
Enable debug output.
Definition: LogSystem.h:206
Belle2::LogSystem::resetLogConnections
void resetLogConnections()
Removes all log connections.
Definition: LogSystem.cc:46
Belle2::LogSystem::resetLogging
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
Definition: LogSystem.cc:184
Belle2::LogSystem::operator=
LogSystem & operator=(const LogSystem &)=delete
Disable/Hide the copy assignment operator.
Belle2::LogSystem::setMaxMessageRepetitions
void setMaxMessageRepetitions(unsigned repetitions)
Set maximum number of repetitions before silencing "identical" log messages.
Definition: LogSystem.h:177
Belle2::LogSystem::m_messageLog
std::unordered_map< LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasher > m_messageLog
Count of previous log messages for the summary and to suppress repetitive messages.
Definition: LogSystem.h:227
Belle2::LogSystem::m_packageLogConfigs
std::map< std::string, LogConfig > m_packageLogConfigs
Stores the log configuration objects for packages.
Definition: LogSystem.h:223
Belle2::LogSystem::sendMessage
bool sendMessage(LogMessage &&message)
Sends a log message using the log connection object.
Definition: LogSystem.cc:71
Belle2::LogSystem::getMaxMessageRepetitions
unsigned getMaxMessageRepetitions() const
Get maximum number of repetitions before silencing "identical" log messages.
Definition: LogSystem.h:170
Belle2::LogConfig::ELogLevel
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:36
Belle2::LogSystem::resetMessageCounter
void resetMessageCounter()
Resets the message counter and error log by setting all message counts to 0.
Definition: LogSystem.cc:154
Belle2::LogConnectionBase
Abstract base class for the different types of log connections.
Definition: LogConnectionBase.h:31
Belle2::LogSystem::getPackageLogConfig
LogConfig & getPackageLogConfig(const std::string &package)
Get the log configuration for the package with the given name.
Definition: LogSystem.h:106
Belle2::LogSystem::m_messageCounter
int m_messageCounter[LogConfig::c_Default]
Counts the number of messages sent per message level.
Definition: LogSystem.h:233
Belle2::LogSystem::m_logConfig
LogConfig m_logConfig
The global log system configuration.
Definition: LogSystem.h:217
Belle2::LogSystem::getLogConfig
LogConfig * getLogConfig()
Returns global log system configuration.
Definition: LogSystem.h:88
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::LogSystem
Class for logging debug, info and error messages.
Definition: LogSystem.h:56
Belle2::LogSystem::getCurrentLogLevel
LogConfig::ELogLevel getCurrentLogLevel(const char *package=nullptr) const
Returns the current log level used by the logging system.
Definition: LogSystem.h:156
Belle2::LogSystem::getCurrentLogConfig
const LogConfig & getCurrentLogConfig(const char *package=nullptr) const
Returns the current LogConfig object used by the logging system.
Definition: LogSystem.h:268
Belle2::LogSystem::enableErrorSummary
void enableErrorSummary(bool on)
enable/disable error/warning summary after successful execution and B2FATAL.
Definition: LogSystem.h:191
Belle2::LogSystem::updateModule
void updateModule(const LogConfig *moduleLogConfig=nullptr, const std::string &moduleName="")
Sets the log configuration to the given module log configuration and sets the module name This method...
Definition: LogSystem.h:201
Belle2::LogSystem::s_debugEnabled
static bool s_debugEnabled
Global flag for fast checking if debug output is enabled.
Definition: LogSystem.h:235
Belle2::LogSystem::m_maxErrorRepetition
unsigned int m_maxErrorRepetition
Maximum number to show the same message.
Definition: LogSystem.h:229
Belle2::LogSystem::getCurrentDebugLevel
int getCurrentDebugLevel(const char *package=nullptr) const
Returns the current debug level used by the logging system.
Definition: LogSystem.h:163
Belle2::LogSystem::m_moduleName
std::string m_moduleName
The current module name.
Definition: LogSystem.h:221
Belle2::LogSystem::deliverMessageToConnections
bool deliverMessageToConnections(const LogMessage &msg)
Do nothing else than to send the message to all connected connections.
Definition: LogSystem.cc:55
Belle2::LogSystem::Instance
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:33
Belle2::LogConfig::c_Message
@ c_Message
Log message text.
Definition: LogConfig.h:47
Belle2::LogSystem::m_logConnections
std::vector< LogConnectionBase * > m_logConnections
Stores the pointers to the log connection objects.
Definition: LogSystem.h:215
Belle2::LogSystem::LogSystem
LogSystem()
The constructor is hidden to avoid that someone creates an instance of this class.
Definition: LogSystem.cc:175
Belle2::LogSystem::c_errorSummaryMaxLines
static const unsigned int c_errorSummaryMaxLines
Error log will contain at most this many lines.
Definition: LogSystem.h:58
Belle2::LogSystem::m_printErrorSummary
bool m_printErrorSummary
Wether to re-print errors-warnings encountered during execution at the end.
Definition: LogSystem.h:225
Belle2::LogConfig::c_Debug
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:36
Belle2::LogSystem::showText
void showText(LogConfig::ELogLevel level, const std::string &txt, int info=LogConfig::c_Message)
Send a custom message which looks like a log message but should not be counted as such.
Definition: LogSystem.cc:64
Belle2::LogConfig::getLogLevel
ELogLevel getLogLevel() const
Returns the configured log level.
Definition: LogConfig.h:87
Belle2::LogSystem::addLogConnection
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
Definition: LogSystem.cc:40
Belle2::LogConfig::c_Default
@ c_Default
Default: use globally configured log level.
Definition: LogConfig.h:42
Belle2::LogSystem::~LogSystem
~LogSystem()
The LogSystem destructor.
Definition: LogSystem.cc:296
Belle2::LogSystem::isLevelEnabled
bool isLevelEnabled(LogConfig::ELogLevel level, int debugLevel=0, const char *package=nullptr) const
Returns true if the given log level is allowed by the log system (i.e.
Definition: LogSystem.h:287
Belle2::LogConfig
The LogConfig class.
Definition: LogConfig.h:32
Belle2::LogSystem::printErrorSummary
void printErrorSummary()
Print error/warning summary at end of execution.
Definition: LogSystem.cc:210
Belle2::LogSystem::m_moduleLogConfig
const LogConfig * m_moduleLogConfig
log config of current module
Definition: LogSystem.h:219
Belle2::LogSystem::getMessageCounter
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
Definition: LogSystem.cc:165
Belle2::LogMessage
The LogMessage class.
Definition: LogMessage.h:39
Belle2::LogSystem::incMessageCounter
void incMessageCounter(LogConfig::ELogLevel logLevel)
Increases the counter for the called message level by one.
Definition: LogSystem.cc:301