Belle II Software development
LogMessage.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/LogVariableStream.h>
13
14#include <string>
15#include <iosfwd>
16
17
18namespace Belle2 {
29 class LogMessage {
30 public:
31
44 LogMessage(LogConfig::ELogLevel logLevel, const std::string& message, const char* package,
45 std::string function, std::string file, unsigned int line, int debugLevel = 0);
46
58 LogMessage(LogConfig::ELogLevel logLevel, LogVariableStream&& messageStream, const char* package,
59 std::string function, std::string file, unsigned int line, int debugLevel = 0);
60
64 LogMessage(LogMessage&&) = default;
65
69 LogMessage(LogMessage const& lm) = default;
70
76 bool operator==(const LogMessage& message) const;
77
79 bool operator!=(const LogMessage& message) const { return !(*this == message); }
80
84 LogMessage& operator=(const LogMessage& lvs) = default;
85
92
98 const std::string& getPackage() const {return m_package; }
99
105 const std::string getMessage() const { return m_message.str(); }
106
110 void setModule(const std::string& module) {m_module = module; }
111
115 void setLogInfo(unsigned int logInfo) {m_logInfo = logInfo; }
116
120 void setCount(int count) {m_count = count; }
121
127 std::ostream& print(std::ostream& out) const;
128
133 std::string toJSON(bool complete) const;
134
136 const std::vector<LogVar>& getLogVariables() const { return m_message.getVariables(); }
137
143 struct TextHasher {
145 std::size_t operator()(const LogMessage& msg) const
146 {
147 return msg.m_logLevel ^ std::hash<std::string>()(msg.m_message.getMessage());
148 }
150 bool operator()(const LogMessage& lhs, const LogMessage& rhs) const
151 {
152 return (lhs.m_logLevel == rhs.m_logLevel) &&
153 (lhs.m_message.getMessage() == rhs.m_message.getMessage());
154 }
155 };
156 private:
157
160 std::string m_module;
161 std::string m_package;
162 std::string m_function;
163 std::string m_file;
164 unsigned int m_line;
167 unsigned int m_logInfo;
169 };
171} // end namespace Belle2
172
176extern std::ostream& operator<< (std::ostream& out, const Belle2::LogMessage& logMessage);
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:26
The LogMessage class.
Definition: LogMessage.h:29
std::string toJSON(bool complete) const
Return a json string for the log message.
Definition: LogMessage.cc:68
int m_debugLevel
The debug level for messages with level=c_Debug.
Definition: LogMessage.h:165
void setLogInfo(unsigned int logInfo)
Configure which information should be printed.
Definition: LogMessage.h:115
bool operator!=(const LogMessage &message) const
Check for inequality.
Definition: LogMessage.h:79
unsigned int m_logInfo
kind of information to show (ORed combination of LogConfig::ELogInfo flags).
Definition: LogMessage.h:167
const std::vector< LogVar > & getLogVariables() const
Return a reference to the log variables associated with this message.
Definition: LogMessage.h:136
LogVariableStream m_message
The message stream which should be sent.
Definition: LogMessage.h:159
const std::string getMessage() const
Returns the message text.
Definition: LogMessage.h:105
void setCount(int count)
Set the number of occurrences.
Definition: LogMessage.h:120
unsigned int m_line
The line number in the source code where the message was sent from.
Definition: LogMessage.h:164
const std::string & getPackage() const
Returns the package where the message was sent from.
Definition: LogMessage.h:98
std::string m_file
The file name where the message was sent from.
Definition: LogMessage.h:163
bool operator==(const LogMessage &message) const
Compares two messages.
Definition: LogMessage.cc:57
std::string m_module
The module name where the message was sent from.
Definition: LogMessage.h:160
LogConfig::ELogLevel getLogLevel() const
Returns the log level of the message.
Definition: LogMessage.h:91
LogMessage(LogMessage const &lm)=default
Provide copy-constructor.
std::ostream & print(std::ostream &out) const
Generate output stream.
Definition: LogMessage.cc:128
LogMessage & operator=(const LogMessage &lvs)=default
Custom assignment-operator, thanks to stringsream's incompetence ...
std::string m_function
The function name where the message was sent from.
Definition: LogMessage.h:162
LogConfig::ELogLevel m_logLevel
The log level of the message.
Definition: LogMessage.h:158
std::string m_package
The package name where the message was sent from.
Definition: LogMessage.h:161
void setModule(const std::string &module)
Configure which information should be printed.
Definition: LogMessage.h:110
int m_count
Number of occurrences of the same message.
Definition: LogMessage.h:168
LogMessage(LogMessage &&)=default
Provide move constructor.
Specialized implementation of an ostream-like class where the << operator can be used to insert value...
const std::vector< LogVar > & getVariables() const
Return the list of all defined variables.
std::string getMessage() const
Return the constant message part without the variables.
std::string str(bool showVariables=true) const
Return the content of the stream as string.
Abstract base class for different kinds of events.
Helper struct to hash and compare messages only by log level and message content.
Definition: LogMessage.h:143
bool operator()(const LogMessage &lhs, const LogMessage &rhs) const
Check if log level and message text are identical.
Definition: LogMessage.h:150
std::size_t operator()(const LogMessage &msg) const
Return a hash for the given log message just based on the log level and the message.
Definition: LogMessage.h:145