Belle II Software development
LogMessage Class Reference

The LogMessage class. More...

#include <LogMessage.h>

Classes

struct  TextHasher
 Helper struct to hash and compare messages only by log level and message content. More...
 

Public Member Functions

 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.
 
 LogMessage (LogConfig::ELogLevel logLevel, LogVariableStream &&messageStream, const char *package, std::string function, std::string file, unsigned int line, int debugLevel=0)
 The LogMessage constructor taking a LogVariableStream which can contains name/value pairs.
 
 LogMessage (LogMessage &&)=default
 Provide move constructor.
 
 LogMessage (LogMessage const &lm)=default
 Provide copy-constructor.
 
bool operator== (const LogMessage &message) const
 Compares two messages.
 
bool operator!= (const LogMessage &message) const
 Check for inequality.
 
LogMessageoperator= (const LogMessage &lvs)=default
 Custom assignment-operator, thanks to stringsream's incompetence ...
 
LogConfig::ELogLevel getLogLevel () const
 Returns the log level of the message.
 
const std::string & getPackage () const
 Returns the package where the message was sent from.
 
const std::string getMessage () const
 Returns the message text.
 
void setModule (const std::string &module)
 Configure which information should be printed.
 
void setLogInfo (unsigned int logInfo)
 Configure which information should be printed.
 
void setCount (int count)
 Set the number of occurrences.
 
std::ostream & print (std::ostream &out) const
 Generate output stream.
 
std::string toJSON (bool complete) const
 Return a json string for the log message.
 
const std::vector< LogVar > & getLogVariables () const
 Return a reference to the log variables associated with this message.
 

Private Attributes

LogConfig::ELogLevel m_logLevel
 The log level of the message.
 
LogVariableStream m_message
 The message stream which should be sent.
 
std::string m_module
 The module name where the message was sent from.
 
std::string m_package
 The package name where the message was sent from.
 
std::string m_function
 The function name where the message was sent from.
 
std::string m_file
 The file name where the message was sent from.
 
unsigned int m_line
 The line number in the source code where the message was sent from.
 
int m_debugLevel
 The debug level for messages with level=c_Debug.
 
unsigned int m_logInfo
 kind of information to show (ORed combination of LogConfig::ELogInfo flags).
 
int m_count
 Number of occurrences of the same message.
 

Detailed Description

The LogMessage class.

This class encapsulates a logging message.

Definition at line 29 of file LogMessage.h.

Constructor & Destructor Documentation

◆ LogMessage() [1/2]

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.

Parameters
logLevelThe log level of the message (e.g. debug, info, warning, error, fatal).
messageThe message string which should be send. This will be internally converted to a LogVariableStream with the text message and 0 variables.
packageThe package name where the message was sent from (can be NULL)
functionThe function name where the message was sent from.
fileThe file name where the message was sent from.
lineThe line number in the source code where the message was sent from.
debugLevelThe debug level of the message if the logLevel is c_Debug

Definition at line 25 of file LogMessage.cc.

26 :
27 m_logLevel(logLevel),
28 m_message(message),
29 m_module(""),
30 m_package(package ? package : ""),
31 m_function(std::move(function)),
32 m_file(std::move(file)),
33 m_line(line),
34 m_debugLevel(debugLevel),
35 m_logInfo(0),
36 m_count(0)
37{
38}
int m_debugLevel
The debug level for messages with level=c_Debug.
Definition: LogMessage.h:165
unsigned int m_logInfo
kind of information to show (ORed combination of LogConfig::ELogInfo flags).
Definition: LogMessage.h:167
LogVariableStream m_message
The message stream which should be sent.
Definition: LogMessage.h:159
unsigned int m_line
The line number in the source code where the message was sent from.
Definition: LogMessage.h:164
std::string m_file
The file name where the message was sent from.
Definition: LogMessage.h:163
std::string m_module
The module name where the message was sent from.
Definition: LogMessage.h:160
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
int m_count
Number of occurrences of the same message.
Definition: LogMessage.h:168

◆ LogMessage() [2/2]

LogMessage ( LogConfig::ELogLevel  logLevel,
LogVariableStream &&  messageStream,
const char *  package,
std::string  function,
std::string  file,
unsigned int  line,
int  debugLevel = 0 
)

The LogMessage constructor taking a LogVariableStream which can contains name/value pairs.

Parameters
logLevelThe log level of the message (e.g. debug, info, warning, error, fatal).
messageStreamThe LogVariableStream which should be send.
packageThe package name where the message was sent from (can be NULL)
functionThe function name where the message was sent from.
fileThe file name where the message was sent from.
lineThe line number in the source code where the message was sent from.
debugLevelThe debug level of the message if the logLevel is c_Debug

Definition at line 40 of file LogMessage.cc.

41 :
42 m_logLevel(logLevel),
43 m_message(std::move(messageStream)),
44 m_module(""),
45 m_package(package ? package : ""),
46 m_function(std::move(function)),
47 m_file(std::move(file)),
48 m_line(line),
49 m_debugLevel(debugLevel),
50 m_logInfo(0),
51 m_count(0)
52{
54}
void adjustLogLevel(Belle2::LogConfig::ELogLevel &logLevel) const
Adjust the log level in case of a realm dependent modification.

Member Function Documentation

◆ getLogLevel()

LogConfig::ELogLevel getLogLevel ( ) const
inline

Returns the log level of the message.

Returns
Returns the log level of the message.

Definition at line 91 of file LogMessage.h.

91{return m_logLevel; }

◆ getLogVariables()

const std::vector< LogVar > & getLogVariables ( ) const
inline

Return a reference to the log variables associated with this message.

Definition at line 136 of file LogMessage.h.

136{ return m_message.getVariables(); }
const std::vector< LogVar > & getVariables() const
Return the list of all defined variables.

◆ getMessage()

const std::string getMessage ( ) const
inline

Returns the message text.

Returns
Returns the message text;

Definition at line 105 of file LogMessage.h.

105{ return m_message.str(); }
std::string str(bool showVariables=true) const
Return the content of the stream as string.

◆ getPackage()

const std::string & getPackage ( ) const
inline

Returns the package where the message was sent from.

Returns
Returns the package where the message was sent from.

Definition at line 98 of file LogMessage.h.

98{return m_package; }

◆ operator!=()

bool operator!= ( const LogMessage message) const
inline

Check for inequality.

Definition at line 79 of file LogMessage.h.

79{ return !(*this == message); }

◆ operator==()

bool operator== ( const LogMessage message) const

Compares two messages.

Returns
true if the message contents (including module, .cc line etc.) are equal.

Definition at line 57 of file LogMessage.cc.

58{
59 return ((m_logLevel == message.m_logLevel) &&
60 (m_line == message.m_line) &&
61 (m_message == message.m_message) &&
62 (m_module == message.m_module) &&
63 (m_package == message.m_package) &&
64 (m_function == message.m_function) &&
65 (m_file == message.m_file));
66}

◆ print()

std::ostream & print ( std::ostream &  out) const

Generate output stream.

Parameters
outThe output stream.

Definition at line 128 of file LogMessage.cc.

129{
130 int logInfo = m_logInfo ? m_logInfo :
133 if (logInfo & LogConfig::c_Timestamp) {
134 static const double startClock = Utils::getClock();
135 const auto flags = out.flags();
136 const int oldprecision = out.precision(3);
137 out << std::fixed << (Utils::getClock() - startClock) / Unit::s << ": ";
138 out.precision(oldprecision);
139 out.flags(flags);
140 }
141 if (logInfo & LogConfig::c_Level) {
142 const std::string debugLevel = (m_logLevel == LogConfig::c_Debug) ? (":" + std::to_string(m_debugLevel)) : "";
143 out << "[" << LogConfig::logLevelToString(m_logLevel) << debugLevel << "] ";
144 }
145 if (ProcHandler::EvtProcID() != -1) {
146 //which process is this?
147 out << "(" << ProcHandler::EvtProcID() << ") ";
148 }
149 if (logInfo & LogConfig::c_Message) {
150 out << m_message.str(!(logInfo & LogConfig::c_NoVariables));
151 }
152 // if there is no module or package or similar there's no need to print them
153 if (m_module.empty()) logInfo &= ~LogConfig::c_Module;
154 if (m_package.empty()) logInfo &= ~LogConfig::c_Package;
155 if (m_function.empty()) logInfo &= ~LogConfig::c_Function;
156 // line number without filename is useless as well so disable both in one go
157 if (m_file.empty()) logInfo &= ~(LogConfig::c_File | LogConfig::c_Line);
158 // is there any location string left to print?
159 bool printLocation = logInfo & (LogConfig::c_Module | LogConfig::c_Package | LogConfig::c_Function |
161 if (printLocation) {
162 out << " {";
163 }
164 if (logInfo & LogConfig::c_Module) {
165 out << " module: " << m_module;
166 }
167 if (logInfo & LogConfig::c_Package) {
168 out << " package: " << m_package;
169 }
170 if (logInfo & LogConfig::c_Function) {
171 out << " function: " << m_function;
172 }
173 if (logInfo & LogConfig::c_File) {
174 out << " @" << m_file;
175 }
176 if (logInfo & LogConfig::c_Line) {
177 out << ":" << m_line;
178 }
179 if (printLocation) {
180 out << " }";
181 }
182 out << endl;
183 return out;
184}
The LogConfig class.
Definition: LogConfig.h:22
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:26
@ c_Module
Module in which the message was emitted.
Definition: LogConfig.h:38
@ c_File
Source file in which the message was emitted.
Definition: LogConfig.h:41
@ c_Function
Function in which the message was emitted.
Definition: LogConfig.h:40
@ c_Line
Line in source file in which the message was emitted.
Definition: LogConfig.h:42
@ c_Level
Log level of the message.
Definition: LogConfig.h:36
@ c_Package
Package in which the message was emitted.
Definition: LogConfig.h:39
@ c_Message
Log message text.
Definition: LogConfig.h:37
@ c_NoVariables
If set don't output any variables that are part of the message.
Definition: LogConfig.h:44
@ c_Timestamp
Time at which the message was emitted.
Definition: LogConfig.h:43
static const char * logLevelToString(ELogLevel logLevelType)
Converts a log level type to a string.
Definition: LogConfig.cc:42
static int EvtProcID()
Return ID of the current process.
Definition: ProcHandler.cc:248
static const double s
[second]
Definition: Unit.h:95
double getClock()
Return current value of the real-time clock.
Definition: Utils.cc:66

◆ setCount()

void setCount ( int  count)
inline

Set the number of occurrences.

Definition at line 120 of file LogMessage.h.

120{m_count = count; }

◆ setLogInfo()

void setLogInfo ( unsigned int  logInfo)
inline

Configure which information should be printed.

Definition at line 115 of file LogMessage.h.

115{m_logInfo = logInfo; }

◆ setModule()

void setModule ( const std::string &  module)
inline

Configure which information should be printed.

Definition at line 110 of file LogMessage.h.

110{m_module = module; }

◆ toJSON()

std::string toJSON ( bool  complete) const

Return a json string for the log message.

The returned string will be a json object containing the full log message on one line

Parameters
completeif true include all fields independent of the logInfo settings

Definition at line 68 of file LogMessage.cc.

69{
70 using namespace boost::property_tree::json_parser;
71 std::stringstream buffer;
72 static const double startClock = Utils::getClock();
73 double time = (Utils::getClock() - startClock) / Unit::s;
74 int logInfo = (m_logInfo and not complete) ? m_logInfo :
77 // in JSON we always output level, independent of what the log info says, otherwise it is hard to parse
78 buffer << R"({"level":")" << LogConfig::logLevelToString(m_logLevel) << '"';
79 if (logInfo & LogConfig::c_Message) {
80 buffer << R"(,"message":")" << create_escapes(m_message.getMessage()) << '"';
81 const auto& vars = m_message.getVariables();
82 if ((vars.size() > 0 or complete) and !(logInfo & LogConfig::c_NoVariables)) {
83 buffer << ",\"variables\":{";
84 bool first{true};
85 for (const auto& v : vars) {
86 if (!first) buffer << ",";
87 buffer << '"' << create_escapes(v.getName()) << "\":\"" << create_escapes(v.getValue()) << '"';
88 first = false;
89 }
90 buffer << '}';
91 }
92 }
93 if (logInfo & LogConfig::c_Module)
94 buffer << R"(,"module":")" << create_escapes(m_module) << '"';
95 if (logInfo & LogConfig::c_Package)
96 buffer << R"(,"package":")" << create_escapes(m_package) << '"';
97 if (logInfo & LogConfig::c_Function)
98 buffer << R"(,"function":")" << create_escapes(m_function) << '"';
99 if (logInfo & LogConfig::c_File)
100 buffer << R"(,"file":")" << create_escapes(m_file) << '"';
101 if (logInfo & LogConfig::c_Line)
102 buffer << ",\"line\":" << m_line;
103 if (logInfo & LogConfig::c_Timestamp)
104 buffer << ",\"timestamp\":" << std::fixed << std::setprecision(3) << time;
105 if (ProcHandler::EvtProcID() != -1 or complete)
106 buffer << ",\"proc\":" << ProcHandler::EvtProcID();
107 if (complete) {
108 buffer << ",\"count\":" << m_count;
109 StoreObjPtr<EventMetaData> eventMetaData;
110 if (eventMetaData.isValid()) {
111 buffer << ",\"experiment\":" << eventMetaData->getExperiment();
112 buffer << ",\"run\":" << eventMetaData->getRun();
113 buffer << ",\"subrun\":" << eventMetaData->getSubrun();
114 buffer << ",\"event\":" << eventMetaData->getEvent();
115 }
117 if (processStatistics.isValid()) {
118 const auto& stats = processStatistics->getGlobal();
119 buffer << ",\"nruns\":" << int(stats.getCalls(ModuleStatistics::EStatisticCounters::c_BeginRun));
120 buffer << ",\"nevents\":" << int(stats.getCalls());
121 }
122 buffer << ",\"initialize\":" << ((DataStore::Instance().getInitializeActive()) ? "true" : "false");
123 }
124 buffer << "}\n";
125 return buffer.str();
126}
bool getInitializeActive() const
Are we currently initializing modules?
Definition: DataStore.h:502
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
@ c_BeginRun
Counting time/calls in beginRun()
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
std::string getMessage() const
Return the constant message part without the variables.

Member Data Documentation

◆ m_count

int m_count
private

Number of occurrences of the same message.

Definition at line 168 of file LogMessage.h.

◆ m_debugLevel

int m_debugLevel
private

The debug level for messages with level=c_Debug.

Definition at line 165 of file LogMessage.h.

◆ m_file

std::string m_file
private

The file name where the message was sent from.

Definition at line 163 of file LogMessage.h.

◆ m_function

std::string m_function
private

The function name where the message was sent from.

Definition at line 162 of file LogMessage.h.

◆ m_line

unsigned int m_line
private

The line number in the source code where the message was sent from.

Definition at line 164 of file LogMessage.h.

◆ m_logInfo

unsigned int m_logInfo
private

kind of information to show (ORed combination of LogConfig::ELogInfo flags).

Definition at line 167 of file LogMessage.h.

◆ m_logLevel

LogConfig::ELogLevel m_logLevel
private

The log level of the message.

Definition at line 158 of file LogMessage.h.

◆ m_message

LogVariableStream m_message
private

The message stream which should be sent.

Definition at line 159 of file LogMessage.h.

◆ m_module

std::string m_module
private

The module name where the message was sent from.

Definition at line 160 of file LogMessage.h.

◆ m_package

std::string m_package
private

The package name where the message was sent from.

Definition at line 161 of file LogMessage.h.


The documentation for this class was generated from the following files: