 |
Belle II Software
release-05-02-19
|
11 #include <framework/logging/LogSystem.h>
12 #include <framework/logging/LogMessage.h>
13 #include <framework/logging/LogConnectionBase.h>
14 #include <framework/logging/LogConnectionFilter.h>
15 #include <framework/logging/LogConnectionConsole.h>
16 #include <framework/datastore/DataStore.h>
23 #include <unordered_map>
42 m_logConnections.push_back(logConnection);
48 for (
auto connection : m_logConnections) {
51 m_logConnections.clear();
57 bool messageSent =
false;
58 for (
auto con : m_logConnections) {
59 messageSent |= con->sendMessage(message);
66 LogMessage customText(level, txt,
"",
"",
"", 0);
68 deliverMessageToConnections(customText);
74 auto packageLogConfig = m_packageLogConfigs.find(message.getPackage());
75 if ((packageLogConfig != m_packageLogConfigs.end()) && packageLogConfig->second.getLogInfo(logLevel)) {
76 message.setLogInfo(packageLogConfig->second.getLogInfo(logLevel));
77 }
else if (m_moduleLogConfig && m_moduleLogConfig->getLogInfo(logLevel)) {
78 message.setLogInfo(m_moduleLogConfig->getLogInfo(logLevel));
80 message.setLogInfo(m_logConfig.getLogInfo(logLevel));
83 message.setModule(m_moduleName);
86 incMessageCounter(logLevel);
90 if (m_printErrorSummary || m_maxErrorRepetition > 0) {
91 unsigned int repetition{0};
92 if (m_messageLog.size() >= c_errorSummaryMaxLines) {
96 if (
auto it = m_messageLog.find(message); it != m_messageLog.end()) {
97 repetition = ++(it->second);
102 repetition = ++m_messageLog[message];
104 lastTime = m_maxErrorRepetition > 0 and repetition == m_maxErrorRepetition;
105 if (m_maxErrorRepetition > 0 and repetition > m_maxErrorRepetition) {
108 ++m_suppressedMessages;
112 if ((m_suppressedMessages < 1000 and m_suppressedMessages % 100 == 0) or
113 (m_suppressedMessages < 10000 and m_suppressedMessages % 1000 == 0) or
114 (m_suppressedMessages % 10000 == 0)) {
115 showText(
LogConfig::c_Warning,
"... " + std::to_string(m_suppressedMessages) +
" log messages suppressed due to repetition ...");
122 bool messageSent = deliverMessageToConnections(message);
124 if (logLevel >= m_logConfig.getAbortLevel()) {
127 for (
auto connection : m_logConnections) {
128 connection->finalizeOnAbort();
135 signal(SIGSEGV,
nullptr);
143 if (messageSent and lastTime) {
145 "The previous message has occurred " + std::to_string(m_maxErrorRepetition) +
146 " times and will be suppressed in future",
156 for (
int& i : m_messageCounter) {
159 m_suppressedMessages = 0;
160 m_messageLog.clear();
161 m_messageLog.reserve(100);
167 return m_messageCounter[logLevel];
177 m_moduleLogConfig(nullptr),
178 m_printErrorSummary(false),
219 std::vector<std::pair<LogMessage, unsigned int>> messages;
222 messages.emplace_back(std::move(value));
225 std::stable_sort(messages.begin(), messages.end(), [](
const auto & a,
const auto & b) {
226 return a.first.getLogLevel() > b.first.getLogLevel() or
227 (a.first.getLogLevel() == b.first.getLogLevel() and a.second > b.second);
229 if (messages.size() == 0) {
236 const LogConfig* oldModuleConfig {
nullptr};
239 map<string, LogConfig> oldPackageConfig;
256 for (
auto & [msg, count] : messages) {
257 const bool multiple = count > 1;
unsigned int getLogInfo(ELogLevel logLevel) const
Returns the configured log information for the given level.
@ c_Result
Result: for informational summary messages, e.g.
unsigned int m_suppressedMessages
The amount of messages we have suppressed so far just to get an indication we print this from time to...
static DataStore & Instance()
Instance of singleton Store.
@ c_Fatal
Fatal: for situations were the program execution can not be continued.
void resetLogConnections()
Removes all log connections.
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
void setLogLevel(ELogLevel logLevel)
Configure the log level.
@ c_NoVariables
If set don't output any variables that are part of the message.
std::unordered_map< LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasher > m_messageLog
Count of previous log messages for the summary and to suppress repetitive messages.
std::map< std::string, LogConfig > m_packageLogConfigs
Stores the log configuration objects for packages.
bool sendMessage(LogMessage &&message)
Sends a log message using the log connection object.
@ c_Info
Info: for informational messages, e.g.
@ c_File
Source file in which the message was emitted.
void setAbortLevel(ELogLevel abortLevel)
Configure the abort level.
ELogLevel
Definition of the supported log levels.
void resetMessageCounter()
Resets the message counter and error log by setting all message counts to 0.
Abstract base class for the different types of log connections.
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
int m_messageCounter[LogConfig::c_Default]
Counts the number of messages sent per message level.
LogConfig m_logConfig
The global log system configuration.
@ c_Line
Line in source file in which the message was emitted.
Abstract base class for different kinds of events.
Class for logging debug, info and error messages.
@ c_Level
Log level of the message.
@ c_Error
Error: for things that went wrong and have to be fixed.
void setLogInfo(unsigned int logInfo)
Configure which information should be printed.
static bool s_debugEnabled
Global flag for fast checking if debug output is enabled.
bool deliverMessageToConnections(const LogMessage &msg)
Do nothing else than to send the message to all connected connections.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
@ c_Function
Function in which the message was emitted.
void setLogInfo(ELogLevel logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
@ c_Message
Log message text.
LogSystem()
The constructor is hidden to avoid that someone creates an instance of this class.
static const unsigned int c_errorSummaryMaxLines
Error log will contain at most this many lines.
bool m_printErrorSummary
Wether to re-print errors-warnings encountered during execution at the end.
Implements a log connection to an IO Stream.
@ c_Debug
Debug: for code development.
Implements a log connection that filters repeated messages.
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.
@ c_Warning
Warning: for potential problems that the user should pay attention to.
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
@ c_Default
Default: use globally configured log level.
~LogSystem()
The LogSystem destructor.
void printErrorSummary()
Print error/warning summary at end of execution.
const LogConfig * m_moduleLogConfig
log config of current module
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
static const int c_DefaultDebugLevel
Default debug level.
void incMessageCounter(LogConfig::ELogLevel logLevel)
Increases the counter for the called message level by one.
void setDebugLevel(int debugLevel)
Configure the debug messaging level.
@ c_Module
Module in which the message was emitted.