Belle II Software development
LogVariableStream Class Reference

Specialized implementation of an ostream-like class where the << operator can be used to insert values. More...

#include <LogVariableStream.h>

Public Types

typedef std::basic_ostream< std::stringstream::char_type, std::stringstream::traits_type > __basic_ostream_type
 basic_ofstream which is used with ostream's utility functions
 

Public Member Functions

 LogVariableStream ()=default
 Default constructor with empty text and no variables.
 
 LogVariableStream (LogVariableStream &&)=default
 Provide default move constructor.
 
 LogVariableStream (const LogVariableStream &other)
 Implement custom copy-constructor, because stringstream's one is deleted.
 
 LogVariableStream (std::string const &text, std::map< std::string, std::string > variables={})
 Constructor which sets an initial text for this stream.
 
LogVariableStreamoperator<< (__basic_ostream_type &(*__pf)(__basic_ostream_type &))
 operator override for ostream modifier functions like std::endl who are directly applied to the underlying string stream.
 
LogVariableStreamoperator<< (LogVar const &var)
 Operator override which stores the LogVar information instead of putting it directly in the sstream.
 
LogVariableStreamoperator<< (LogModRealm const &var)
 Operator override which sets a realm dependent log level.
 
template<class TText >
std::enable_if< notstd::is_fundamental< TText >::value, LogVariableStream & >::type operator<< (TText const &text)
 Templated operator which will be used for all non-fundamental types.
 
template<class PODTYPE >
std::enable_if< std::is_fundamental< PODTYPE >::value, LogVariableStream & >::type operator<< (PODTYPE pod)
 Templated operator which will be used for POD types (especially integers) and uses by-value.
 
bool operator== (const LogVariableStream &lvs) const
 Custom comparison operator.
 
LogVariableStreamoperator= (const LogVariableStream &lvs)
 Custom assignment-operator, thanks to stringsream's non-copy policy.
 
std::string str (bool showVariables=true) const
 Return the content of the stream as string.
 
std::string getMessage () const
 Return the constant message part without the variables.
 
const std::vector< LogVar > & getVariables () const
 Return the list of all defined variables.
 
void adjustLogLevel (Belle2::LogConfig::ELogLevel &logLevel) const
 Adjust the log level in case of a realm dependent modification.
 

Private Attributes

std::stringstream m_stringStream
 All non-LogVar items are directly forwarded to this stringstream.
 
std::vector< LogVarm_variables
 List of LogVars which were accepted so far.
 
Belle2::LogConfig::ELogLevel m_logLevelOverride = Belle2::LogConfig::c_Default
 Adjusted log level.
 

Detailed Description

Specialized implementation of an ostream-like class where the << operator can be used to insert values.

In addition to the regular ostream usage, this class also accepts the LogVar class, which contains the name of a variable and its value. If string part and variable part of a log message are separated in that manner, it is much easier to filter and aggregate messages.

Here is an example on the usage: LogVariableStream lvs; lvs << "Inconsistent data size between COPPER and CDC FEE." << LogVar("data length", dataLength) << LogVar("nWord", nWord) << LogVar("Node ID", iNode) << LogVar("Finness ID", iFiness)); std::string lvsAsString = lvs.str();

Definition at line 127 of file LogVariableStream.h.

Member Typedef Documentation

◆ __basic_ostream_type

typedef std::basic_ostream<std::stringstream::char_type, std::stringstream::traits_type > __basic_ostream_type

basic_ofstream which is used with ostream's utility functions

Definition at line 131 of file LogVariableStream.h.

Constructor & Destructor Documentation

◆ LogVariableStream() [1/2]

LogVariableStream ( const LogVariableStream other)
inline

Implement custom copy-constructor, because stringstream's one is deleted.

Definition at line 144 of file LogVariableStream.h.

145 {
146 // copy manually because stringstream has no copy-constructor
147 m_stringStream << other.m_stringStream.str();
148 }
Belle2::LogConfig::ELogLevel m_logLevelOverride
Adjusted log level.
std::vector< LogVar > m_variables
List of LogVars which were accepted so far.
std::stringstream m_stringStream
All non-LogVar items are directly forwarded to this stringstream.

◆ LogVariableStream() [2/2]

LogVariableStream ( std::string const &  text,
std::map< std::string, std::string >  variables = {} 
)
inlineexplicit

Constructor which sets an initial text for this stream.

Parameters
textInitial text.
variablesMap of variables' names and values.

Definition at line 154 of file LogVariableStream.h.

154 {})
155 {
156 m_stringStream << text;
157 for (auto const& kv : variables) {
158 m_variables.emplace_back(LogVar(kv.first, kv.second));
159 }
160 }
Class to store variables with their name which were sent to the logging service.

Member Function Documentation

◆ adjustLogLevel()

void adjustLogLevel ( Belle2::LogConfig::ELogLevel logLevel) const
inline

Adjust the log level in case of a realm dependent modification.

Definition at line 265 of file LogVariableStream.h.

266 {
268 logLevel = m_logLevelOverride;
269 }
270 }
@ c_Default
Default: use globally configured log level.
Definition: LogConfig.h:32

◆ getMessage()

std::string getMessage ( ) const
inline

Return the constant message part without the variables.

Definition at line 253 of file LogVariableStream.h.

254 {
255 return m_stringStream.str();
256 }

◆ getVariables()

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

Return the list of all defined variables.

Definition at line 259 of file LogVariableStream.h.

260 {
261 return m_variables;
262 }

◆ operator<<() [1/5]

LogVariableStream & operator<< ( __basic_ostream_type &(*)(__basic_ostream_type &)  __pf)
inline

operator override for ostream modifier functions like std::endl who are directly applied to the underlying string stream.

Definition at line 167 of file LogVariableStream.h.

168 {
169 // execute provided function on the string stream
170 __pf(m_stringStream);
171 return *this;
172 }

◆ operator<<() [2/5]

LogVariableStream & operator<< ( LogModRealm const &  var)

Operator override which sets a realm dependent log level.

Definition at line 12 of file LogVariableStream.cc.

13{
14 if (var.getRealm() == Belle2::Environment::Instance().getRealm()) {
15 m_logLevelOverride = var.getLogLevel();
16 }
17 return *this;
18}
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
LogConfig::ELogRealm getRealm() const
Get the basf2 execution realm.
Definition: Environment.h:211

◆ operator<<() [3/5]

LogVariableStream & operator<< ( LogVar const &  var)
inline

Operator override which stores the LogVar information instead of putting it directly in the sstream.

Definition at line 178 of file LogVariableStream.h.

179 {
180 m_variables.push_back(var);
181 return *this;
182 }

◆ operator<<() [4/5]

std::enable_if< std::is_fundamental< PODTYPE >::value, LogVariableStream & >::type operator<< ( PODTYPE  pod)
inline

Templated operator which will be used for POD types (especially integers) and uses by-value.

For cases where constants are declared "static const int Name = 23;" in header files but the .cc file contains no definition. In these cases, by-ref cannot be used because no memory location exists to get the reference.

Definition at line 206 of file LogVariableStream.h.

207 {
208 this->m_stringStream << pod;
209 return *this;
210 }

◆ operator<<() [5/5]

std::enable_if< notstd::is_fundamental< TText >::value, LogVariableStream & >::type operator<< ( TText const &  text)
inline

Templated operator which will be used for all non-fundamental types.

This types can be accepted via const& and need no copy.

Definition at line 194 of file LogVariableStream.h.

195 {
196 this->m_stringStream << text;
197 return *this;
198 }

◆ operator=()

LogVariableStream & operator= ( const LogVariableStream lvs)
inline

Custom assignment-operator, thanks to stringsream's non-copy policy.

Definition at line 223 of file LogVariableStream.h.

224 {
225 this->m_stringStream = std::stringstream();
226 this->m_stringStream << lvs.m_stringStream.str();
227 this->m_variables = lvs.m_variables;
228 return *this;
229 }

◆ operator==()

bool operator== ( const LogVariableStream lvs) const
inline

Custom comparison operator.

Definition at line 215 of file LogVariableStream.h.

216 {
217 return (lvs.m_variables == this->m_variables) && (lvs.m_stringStream.str() == this->m_stringStream.str());
218 }

◆ str()

std::string str ( bool  showVariables = true) const
inline

Return the content of the stream as string.

First the stringstream part and then a list of the variables

Definition at line 235 of file LogVariableStream.h.

236 {
237 // little optimization, so we don't need to copy the whole string
238 // in the cases where there are no variables ...
239 if (m_variables.size() == 0 or !showVariables) {
240 return m_stringStream.str();
241 }
242
243 std::stringstream tmpBuffer;
244 // put the string first
245 tmpBuffer << m_stringStream.str();
246 for (auto const& v : m_variables) {
247 tmpBuffer << std::endl << "\t" << v.getName() << " = " << v.getValue();
248 }
249 return tmpBuffer.str();
250 }

Member Data Documentation

◆ m_logLevelOverride

Adjusted log level.

Definition at line 281 of file LogVariableStream.h.

◆ m_stringStream

std::stringstream m_stringStream
private

All non-LogVar items are directly forwarded to this stringstream.

Definition at line 275 of file LogVariableStream.h.

◆ m_variables

std::vector<LogVar> m_variables
private

List of LogVars which were accepted so far.

Definition at line 278 of file LogVariableStream.h.


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