Belle II Software  release-08-01-10
LogSystem.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 
11 #include <framework/logging/LogConfig.h>
12 #include <framework/logging/LogMessage.h>
13 
14 #include <string>
15 #include <vector>
16 #include <map>
17 #include <unordered_map>
18 
19 
20 namespace Belle2 {
25  class LogConnectionBase;
26 
46  class LogSystem {
47  public:
48  static const unsigned int c_errorSummaryMaxLines = 10000;
55  static LogSystem& Instance();
56 
65  void addLogConnection(LogConnectionBase* logConnection);
66 
70  void resetLogConnections();
71 
73  void resetLogging();
74 
79 
87  void addPackageLogConfig(const std::string& package, const LogConfig& logConfig) { m_packageLogConfigs[package] = logConfig; }
88 
96  LogConfig& getPackageLogConfig(const std::string& package) { return m_packageLogConfigs[package]; }
97 
105  bool isLevelEnabled(LogConfig::ELogLevel level, int debugLevel = 0, const char* package = nullptr) const;
106 
116  bool sendMessage(LogMessage&& message);
117 
119  void resetMessageCounter();
120 
127  int getMessageCounter(LogConfig::ELogLevel logLevel) const;
128 
139  const LogConfig& getCurrentLogConfig(const char* package = nullptr) const;
140 
146  LogConfig::ELogLevel getCurrentLogLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getLogLevel(); }
147 
153  int getCurrentDebugLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getDebugLevel(); }
154 
160  unsigned getMaxMessageRepetitions() const { return m_maxErrorRepetition; }
161 
167  void setMaxMessageRepetitions(unsigned repetitions) { m_maxErrorRepetition = repetitions; }
168 
174  void printErrorSummary();
175 
181  void enableErrorSummary(bool on) { m_printErrorSummary = on; }
182 
191  void updateModule(const LogConfig* moduleLogConfig = nullptr, const std::string& moduleName = "") { m_moduleLogConfig = moduleLogConfig; m_moduleName = moduleName; }
192 
196  static void enableDebug() {s_debugEnabled = true;}
197 
201  inline static bool debugEnabled() {return s_debugEnabled;}
202 
203  private:
205  std::vector<LogConnectionBase*> m_logConnections;
211  std::string m_moduleName;
213  std::map<std::string, LogConfig> m_packageLogConfigs;
217  std::unordered_map<LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasher> m_messageLog{100};
219  unsigned int m_maxErrorRepetition{0};
221  unsigned int m_suppressedMessages{0};
225  static bool s_debugEnabled;
226 
228  LogSystem();
229 
231  LogSystem(const LogSystem&) = delete;
232 
234  LogSystem& operator=(const LogSystem&) = delete;
235 
237  ~LogSystem();
238 
245 
247  bool deliverMessageToConnections(const LogMessage& msg);
248 
255  void showText(LogConfig::ELogLevel level, const std::string& txt, int info = LogConfig::c_Message);
256  };
257 
258  inline const LogConfig& LogSystem::getCurrentLogConfig(const char* package) const
259  {
260  //module specific config?
262  return *m_moduleLogConfig;
263  }
264  //package specific config?
265  if (package && !m_packageLogConfigs.empty()) {
266  // cppcheck-suppress stlIfFind ; cppcheck doesn't like scoped variables in if statements
267  if (auto it = m_packageLogConfigs.find(package); it != m_packageLogConfigs.end()) {
268  const LogConfig& logConfig = it->second;
269  if (logConfig.getLogLevel() != LogConfig::c_Default)
270  return logConfig;
271  }
272  }
273  //global config
274  return m_logConfig;
275  }
276 
277  inline bool LogSystem::isLevelEnabled(LogConfig::ELogLevel level, int debugLevel, const char* package) const
278  {
279  const LogConfig& config = getCurrentLogConfig(package);
280  const LogConfig::ELogLevel logLevelLimit = config.getLogLevel();
281  const int debugLevelLimit = config.getDebugLevel();
282  return logLevelLimit <= level && (level != LogConfig::c_Debug || debugLevelLimit >= debugLevel);
283  }
284 
286 } //end of namespace Belle2
The LogConfig class.
Definition: LogConfig.h:22
int getDebugLevel() const
Returns the configured debug messaging level.
Definition: LogConfig.h:105
ELogLevel getLogLevel() const
Returns the configured log level.
Definition: LogConfig.h:91
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:26
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:26
@ c_Default
Default: use globally configured log level.
Definition: LogConfig.h:32
@ c_Message
Log message text.
Definition: LogConfig.h:37
Abstract base class for the different types of log connections.
The LogMessage class.
Definition: LogMessage.h:29
Class for logging debug, info and error messages.
Definition: LogSystem.h:46
LogConfig & getPackageLogConfig(const std::string &package)
Get the log configuration for the package with the given name.
Definition: LogSystem.h:96
unsigned int m_maxErrorRepetition
Maximum number to show the same message.
Definition: LogSystem.h:219
static bool s_debugEnabled
Global flag for fast checking if debug output is enabled.
Definition: LogSystem.h:225
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:221
~LogSystem()
The LogSystem destructor.
Definition: LogSystem.cc:292
LogConfig::ELogLevel getCurrentLogLevel(const char *package=nullptr) const
Returns the current log level used by the logging system.
Definition: LogSystem.h:146
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
Definition: LogSystem.cc:180
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:191
void addPackageLogConfig(const std::string &package, const LogConfig &logConfig)
Add the per package log configuration.
Definition: LogSystem.h:87
std::vector< LogConnectionBase * > m_logConnections
Stores the pointers to the log connection objects.
Definition: LogSystem.h:205
void printErrorSummary()
Print error/warning summary at end of execution.
Definition: LogSystem.cc:206
bool deliverMessageToConnections(const LogMessage &msg)
Do nothing else than to send the message to all connected connections.
Definition: LogSystem.cc:53
int m_messageCounter[LogConfig::c_Default]
Counts the number of messages sent per message level.
Definition: LogSystem.h:223
void resetMessageCounter()
Resets the message counter and error log by setting all message counts to 0.
Definition: LogSystem.cc:150
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:217
std::map< std::string, LogConfig > m_packageLogConfigs
Stores the log configuration objects for packages.
Definition: LogSystem.h:213
bool m_printErrorSummary
Wether to re-print errors-warnings encountered during execution at the end.
Definition: LogSystem.h:215
LogSystem & operator=(const LogSystem &)=delete
Disable/Hide the copy assignment operator.
void enableErrorSummary(bool on)
enable/disable error/warning summary after successful execution and B2FATAL.
Definition: LogSystem.h:181
bool sendMessage(LogMessage &&message)
Sends a log message using the log connection object.
Definition: LogSystem.cc:69
LogSystem()
The constructor is hidden to avoid that someone creates an instance of this class.
Definition: LogSystem.cc:171
void incMessageCounter(LogConfig::ELogLevel logLevel)
Increases the counter for the called message level by one.
Definition: LogSystem.cc:297
LogConfig * getLogConfig()
Returns global log system configuration.
Definition: LogSystem.h:78
void setMaxMessageRepetitions(unsigned repetitions)
Set maximum number of repetitions before silencing "identical" log messages.
Definition: LogSystem.h:167
LogConfig m_logConfig
The global log system configuration.
Definition: LogSystem.h:207
int getCurrentDebugLevel(const char *package=nullptr) const
Returns the current debug level used by the logging system.
Definition: LogSystem.h:153
const LogConfig * m_moduleLogConfig
log config of current module
Definition: LogSystem.h:209
std::string m_moduleName
The current module name.
Definition: LogSystem.h:211
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
Definition: LogSystem.cc:161
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:31
static const unsigned int c_errorSummaryMaxLines
Error log will contain at most this many lines.
Definition: LogSystem.h:48
static void enableDebug()
Enable debug output.
Definition: LogSystem.h:196
void resetLogConnections()
Removes all log connections.
Definition: LogSystem.cc:44
unsigned getMaxMessageRepetitions() const
Get maximum number of repetitions before silencing "identical" log messages.
Definition: LogSystem.h:160
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:62
static bool debugEnabled()
Is debug output enabled?
Definition: LogSystem.h:201
LogSystem(const LogSystem &)=delete
Disable/Hide the copy constructor.
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
Definition: LogSystem.cc:38
const LogConfig & getCurrentLogConfig(const char *package=nullptr) const
Returns the current LogConfig object used by the logging system.
Definition: LogSystem.h:258
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:277
Abstract base class for different kinds of events.