Belle II Software  release-05-02-19
LogMessage.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Thomas Kuhr, Thomas Hauth, 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/LogVariableStream.h>
15 
16 #include <string>
17 #include <iosfwd>
18 
19 
20 namespace Belle2 {
31  class LogMessage {
32  public:
33 
45  LogMessage(LogConfig::ELogLevel logLevel, const std::string& message, const char* package,
46  std::string function, std::string file, unsigned int line, int debugLevel = 0);
47 
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 
122  std::ostream& print(std::ostream& out) const;
123 
128  std::string toJSON(bool complete) const;
129 
131  const std::vector<LogVar>& getLogVariables() const { return m_message.getVariables(); }
132 
138  struct TextHasher {
140  std::size_t operator()(const LogMessage& msg) const
141  {
142  return msg.m_logLevel ^ std::hash<std::string>()(msg.m_message.getMessage());
143  }
145  bool operator()(const LogMessage& lhs, const LogMessage& rhs) const
146  {
147  return (lhs.m_logLevel == rhs.m_logLevel) &&
148  (lhs.m_message.getMessage() == rhs.m_message.getMessage());
149  }
150  };
151  private:
152 
155  std::string m_module;
156  std::string m_package;
157  std::string m_function;
158  std::string m_file;
159  unsigned int m_line;
160  int m_debugLevel;
162  unsigned int m_logInfo;
163  };
165 } // end namespace Belle2
166 
170 extern std::ostream& operator<< (std::ostream& out, const Belle2::LogMessage& logMessage);
Belle2::LogMessage::getPackage
const std::string & getPackage() const
Returns the package where the message was sent from.
Definition: LogMessage.h:106
LogVariableStream
Specialized implementation of an ostream-like class where the << operator can be used to insert value...
Definition: LogVariableStream.h:87
Belle2::LogMessage::m_debugLevel
int m_debugLevel
The debug level for messages with level=c_Debug.
Definition: LogMessage.h:168
Belle2::LogMessage::m_message
LogVariableStream m_message
The message stream which should be sent.
Definition: LogMessage.h:162
Belle2::LogMessage::operator==
bool operator==(const LogMessage &message) const
Compares two messages.
Definition: LogMessage.cc:53
Belle2::LogMessage::m_package
std::string m_package
The package name where the message was sent from.
Definition: LogMessage.h:164
LogVariableStream::str
std::string str(bool showVariables=true) const
Return the content of the stream as string.
Definition: LogVariableStream.h:189
Belle2::LogMessage::getLogVariables
const std::vector< LogVar > & getLogVariables() const
Return a reference to the log variables associated with this message.
Definition: LogMessage.h:139
Belle2::LogConfig::ELogLevel
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:36
Belle2::LogMessage::LogMessage
LogMessage(LogConfig::ELogLevel logLevel, const std::string &message, const char *package, std::string function, std::string file, unsigned int line, int debugLevel=0)
The LogMessage constructor.
Definition: LogMessage.cc:24
Belle2::LogMessage::operator!=
bool operator!=(const LogMessage &message) const
Check for inequality.
Definition: LogMessage.h:87
Belle2::LogMessage::operator=
LogMessage & operator=(const LogMessage &lvs)=default
Custom assignment-operator, thanks to stringsream's incompetence ...
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::LogMessage::m_logInfo
unsigned int m_logInfo
kind of information to show (ORed combination of LogConfig::ELogInfo flags).
Definition: LogMessage.h:170
Belle2::LogMessage::m_function
std::string m_function
The function name where the message was sent from.
Definition: LogMessage.h:165
LogVariableStream::getVariables
const std::vector< LogVar > & getVariables() const
Return the list of all defined variables.
Definition: LogVariableStream.h:213
Belle2::LogMessage::m_logLevel
LogConfig::ELogLevel m_logLevel
The log level of the message.
Definition: LogMessage.h:161
Belle2::LogMessage::m_module
std::string m_module
The module name where the message was sent from.
Definition: LogMessage.h:163
Belle2::OrcaKinFit::BaseConstraint::operator<<
std::ostream & operator<<(std::ostream &os, const BaseConstraint &bc)
Prints out a BaseConstraint, using its print method.
Definition: BaseConstraint.h:128
Belle2::LogMessage::setLogInfo
void setLogInfo(unsigned int logInfo)
Configure which information should be printed.
Definition: LogMessage.h:123
Belle2::LogMessage::toJSON
std::string toJSON(bool complete) const
Return a json string for the log message.
Definition: LogMessage.cc:64
Belle2::LogMessage::getLogLevel
LogConfig::ELogLevel getLogLevel() const
Returns the log level of the message.
Definition: LogMessage.h:99
Belle2::LogMessage::setModule
void setModule(const std::string &module)
Configure which information should be printed.
Definition: LogMessage.h:118
Belle2::LogMessage::m_file
std::string m_file
The file name where the message was sent from.
Definition: LogMessage.h:166
Belle2::LogMessage
The LogMessage class.
Definition: LogMessage.h:39
Belle2::LogMessage::TextHasher::operator()
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:148
Belle2::LogMessage::getMessage
const std::string getMessage() const
Returns the message text.
Definition: LogMessage.h:113
Belle2::LogMessage::m_line
unsigned int m_line
The line number in the source code where the message was sent from.
Definition: LogMessage.h:167
Belle2::LogMessage::print
std::ostream & print(std::ostream &out) const
Generate output stream.
Definition: LogMessage.cc:108