Belle II Software development
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
20namespace Belle2 {
26
46 class LogSystem {
47 public:
48 static const unsigned int c_errorSummaryMaxLines = 10000;
49
55 static LogSystem& Instance();
56
65 void addLogConnection(LogConnectionBase* logConnection);
66
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 LogConfig& getModuleLogConfig(const std::string& module) { return m_moduleLogConfigs[module]; }
106
114 bool isLevelEnabled(LogConfig::ELogLevel level, int debugLevel = 0, const char* package = nullptr) const;
115
125 bool sendMessage(LogMessage&& message);
126
128 void resetMessageCounter();
129
136 int getMessageCounter(LogConfig::ELogLevel logLevel) const;
137
148 const LogConfig& getCurrentLogConfig(const char* package = nullptr) const;
149
155 LogConfig::ELogLevel getCurrentLogLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getLogLevel(); }
156
162 int getCurrentDebugLevel(const char* package = nullptr) const { return getCurrentLogConfig(package).getDebugLevel(); }
163
170
176 void setMaxMessageRepetitions(unsigned repetitions) { m_maxErrorRepetition = repetitions; }
177
183 void printErrorSummary();
184
191
200 void updateModule(const LogConfig* moduleLogConfig = nullptr, const std::string& moduleName = "") { m_moduleLogConfig = moduleLogConfig; m_moduleName = moduleName; }
201
205 static void enableDebug() {s_debugEnabled = true;}
206
210 inline static bool debugEnabled() {return s_debugEnabled;}
211
212 private:
214 std::vector<LogConnectionBase*> m_logConnections;
220 std::string m_moduleName;
222 std::map<std::string, LogConfig> m_packageLogConfigs;
224 std::map<std::string, LogConfig> m_moduleLogConfigs;
228 std::unordered_map<LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasher> m_messageLog{100};
230 unsigned int m_maxErrorRepetition{0};
232 unsigned int m_suppressedMessages{0};
236 static bool s_debugEnabled;
237
239 LogSystem();
240
242 LogSystem(const LogSystem&) = delete;
243
245 LogSystem& operator=(const LogSystem&) = delete;
246
248 ~LogSystem();
249
256
259
266 void showText(LogConfig::ELogLevel level, const std::string& txt, int info = LogConfig::c_Message);
267 };
268
269 inline const LogConfig& LogSystem::getCurrentLogConfig(const char* package) const
270 {
271 //module specific config?
272 if (m_moduleLogConfig && (m_moduleLogConfig->getLogLevel() != LogConfig::c_Default)) {
273 return *m_moduleLogConfig;
274 }
275 //package specific config?
276 if (package && !m_packageLogConfigs.empty()) {
277 // cppcheck-suppress stlIfFind ; cppcheck doesn't like scoped variables in if statements
278 if (auto it = m_packageLogConfigs.find(package); it != m_packageLogConfigs.end()) {
279 const LogConfig& logConfig = it->second;
280 if (logConfig.getLogLevel() != LogConfig::c_Default)
281 return logConfig;
282 }
283 }
284 //global config
285 return m_logConfig;
286 }
287
288 inline bool LogSystem::isLevelEnabled(LogConfig::ELogLevel level, int debugLevel, const char* package) const
289 {
290 const LogConfig& config = getCurrentLogConfig(package);
291 const LogConfig::ELogLevel logLevelLimit = config.getLogLevel();
292 const int debugLevelLimit = config.getDebugLevel();
293 return logLevelLimit <= level && (level != LogConfig::c_Debug || debugLevelLimit >= debugLevel);
294 }
295
297} //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
unsigned int m_maxErrorRepetition
Maximum number to show the same message.
Definition LogSystem.h:230
static bool s_debugEnabled
Global flag for fast checking if debug output is enabled.
Definition LogSystem.h:236
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:232
~LogSystem()
The LogSystem destructor.
Definition LogSystem.cc:289
LogConfig::ELogLevel getCurrentLogLevel(const char *package=nullptr) const
Returns the current log level used by the logging system.
Definition LogSystem.h:155
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
Definition LogSystem.cc:177
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:200
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:214
void printErrorSummary()
Print error/warning summary at end of execution.
Definition LogSystem.cc:203
bool deliverMessageToConnections(const LogMessage &msg)
Do nothing else than to send the message to all connected connections.
Definition LogSystem.cc:50
int m_messageCounter[LogConfig::c_Default]
Counts the number of messages sent per message level.
Definition LogSystem.h:234
LogSystem & operator=(const LogSystem &)=delete
Disable/Hide the copy assignment operator.
void resetMessageCounter()
Resets the message counter and error log by setting all message counts to 0.
Definition LogSystem.cc:147
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:228
std::map< std::string, LogConfig > m_packageLogConfigs
Stores the log configuration objects for packages.
Definition LogSystem.h:222
bool m_printErrorSummary
Whether to re-print errors-warnings encountered during execution at the end.
Definition LogSystem.h:226
std::map< std::string, LogConfig > m_moduleLogConfigs
Stores the log configuration objects for module.
Definition LogSystem.h:224
LogConfig * getLogConfig()
Returns global log system configuration.
Definition LogSystem.h:78
LogConfig & getModuleLogConfig(const std::string &module)
Get the log configuration for the module with the given name.
Definition LogSystem.h:105
void enableErrorSummary(bool on)
enable/disable error/warning summary after successful execution and B2FATAL.
Definition LogSystem.h:190
bool sendMessage(LogMessage &&message)
Sends a log message using the log connection object.
Definition LogSystem.cc:66
LogSystem()
The constructor is hidden to avoid that someone creates an instance of this class.
Definition LogSystem.cc:168
void incMessageCounter(LogConfig::ELogLevel logLevel)
Increases the counter for the called message level by one.
Definition LogSystem.cc:294
void setMaxMessageRepetitions(unsigned repetitions)
Set maximum number of repetitions before silencing "identical" log messages.
Definition LogSystem.h:176
LogConfig m_logConfig
The global log system configuration.
Definition LogSystem.h:216
int getCurrentDebugLevel(const char *package=nullptr) const
Returns the current debug level used by the logging system.
Definition LogSystem.h:162
const LogConfig * m_moduleLogConfig
log config of current module
Definition LogSystem.h:218
std::string m_moduleName
The current module name.
Definition LogSystem.h:220
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
Definition LogSystem.cc:158
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition LogSystem.cc:28
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:205
void resetLogConnections()
Removes all log connections.
Definition LogSystem.cc:41
unsigned getMaxMessageRepetitions() const
Get maximum number of repetitions before silencing "identical" log messages.
Definition LogSystem.h:169
LogConfig & getPackageLogConfig(const std::string &package)
Get the log configuration for the package with the given name.
Definition LogSystem.h:96
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:59
static bool debugEnabled()
Is debug output enabled?
Definition LogSystem.h:210
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:35
const LogConfig & getCurrentLogConfig(const char *package=nullptr) const
Returns the current LogConfig object used by the logging system.
Definition LogSystem.h:269
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:288
Abstract base class for different kinds of events.