Belle II Software  release-05-02-19
LogVariableStream.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Hauth, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <sstream>
14 #include <vector>
15 #include <string>
16 #include <map>
17 #include <type_traits>
18 #include <boost/lexical_cast.hpp>
19 
24 class LogVar {
25 public:
32  template<class TVarType>
33  LogVar(const std::string& name, const TVarType& v) :
34  m_name(name),
35  m_value(boost::lexical_cast<std::string>(v))
36  {
37  }
38 
42  std::string getValue() const
43  {
44  return m_value;
45  }
46 
50  std::string getName() const
51  {
52  return m_name;
53  }
54 
58  bool operator==(const LogVar& lv) const
59  {
60  return (lv.m_name == this->m_name) && (lv.m_value == this->m_value);
61  }
62 
63 private:
66  std::string m_name;
67 
70  std::string m_value;
71 };
72 
88 public:
89 
91  typedef std::basic_ostream<std::stringstream::char_type, std::stringstream::traits_type > __basic_ostream_type;
92 
94  LogVariableStream() = default;
95 
100 
105  {
106  // copy manually because stringstream has no copy-constructor
107  m_stringStream << other.m_stringStream.str();
108  }
109 
113  explicit LogVariableStream(std::string const& text, std::map<std::string, std::string> variables = {})
114  {
115  m_stringStream << text;
116  for (auto const& kv : variables) {
117  m_variables.emplace_back(LogVar(kv.first, kv.second));
118  }
119  }
120 
125  // cppcheck-suppress constParameter ; no, this cannot be const otherwise e.g. std::endl doesn't work
127  {
128  // execute provided function on the string stream
129  __pf(m_stringStream);
130  return *this;
131  }
132 
138  {
139  m_variables.push_back(var);
140  return *this;
141  }
142 
147  template<class TText>
148  typename std::enable_if<not std::is_fundamental<TText>::value, LogVariableStream&>::type operator<<(TText const& text)
149  {
150  this->m_stringStream << text;
151  return *this;
152  }
153 
159  template<class PODTYPE>
160  typename std::enable_if<std::is_fundamental<PODTYPE>::value, LogVariableStream&>::type operator<<(PODTYPE pod)
161  {
162  this->m_stringStream << pod;
163  return *this;
164  }
165 
169  bool operator==(const LogVariableStream& lvs) const
170  {
171  return (lvs.m_variables == this->m_variables) && (lvs.m_stringStream.str() == this->m_stringStream.str());
172  }
173 
178  {
179  this->m_stringStream = std::stringstream();
180  this->m_stringStream << lvs.m_stringStream.str();
181  this->m_variables = lvs.m_variables;
182  return *this;
183  }
184 
189  std::string str(bool showVariables = true) const
190  {
191  // little optimization, so we don't need to copy the whole string
192  // in the cases where there are no variables ...
193  if (m_variables.size() == 0 or !showVariables) {
194  return m_stringStream.str();
195  }
196 
197  std::stringstream tmpBuffer;
198  // put the string first
199  tmpBuffer << m_stringStream.str();
200  for (auto const& v : m_variables) {
201  tmpBuffer << std::endl << "\t" << v.getName() << " = " << v.getValue();
202  }
203  return tmpBuffer.str();
204  }
205 
207  std::string getMessage() const
208  {
209  return m_stringStream.str();
210  }
211 
213  const std::vector<LogVar>& getVariables() const
214  {
215  return m_variables;
216  }
217 
218 private:
219 
221  std::stringstream m_stringStream;
222 
224  std::vector<LogVar> m_variables;
225 };
LogVariableStream::getMessage
std::string getMessage() const
Return the constant message part without the variables.
Definition: LogVariableStream.h:207
LogVariableStream::m_stringStream
std::stringstream m_stringStream
All non-LogVar items are directly forwarded to this stringstream.
Definition: LogVariableStream.h:221
LogVariableStream
Specialized implementation of an ostream-like class where the << operator can be used to insert value...
Definition: LogVariableStream.h:87
LogVariableStream::operator<<
LogVariableStream & operator<<(__basic_ostream_type &(*__pf)(__basic_ostream_type &))
operator override for ostream modifier functions like std::endl who are directly applied to the under...
Definition: LogVariableStream.h:126
LogVar::getValue
std::string getValue() const
Returns the value stored for this variable.
Definition: LogVariableStream.h:50
LogVariableStream::LogVariableStream
LogVariableStream(const LogVariableStream &other)
Implement custom copy-constructor, because stringstream's one is deleted.
Definition: LogVariableStream.h:104
LogVariableStream::operator==
bool operator==(const LogVariableStream &lvs) const
Custom comparison operator.
Definition: LogVariableStream.h:169
LogVariableStream::LogVariableStream
LogVariableStream(std::string const &text, std::map< std::string, std::string > variables={})
Constructor which sets an initial text for this stream.
Definition: LogVariableStream.h:113
LogVariableStream::operator<<
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.
Definition: LogVariableStream.h:160
LogVariableStream::str
std::string str(bool showVariables=true) const
Return the content of the stream as string.
Definition: LogVariableStream.h:189
LogVar::getName
std::string getName() const
Returns the name stored for this variable.
Definition: LogVariableStream.h:58
LogVariableStream::operator=
LogVariableStream & operator=(const LogVariableStream &lvs)
Custom assignment-operator, thanks to stringsream's non-copy policy.
Definition: LogVariableStream.h:177
LogVar::operator==
bool operator==(const LogVar &lv) const
Custom comparison operator.
Definition: LogVariableStream.h:66
LogVar::m_value
std::string m_value
String conversion of the value.
Definition: LogVariableStream.h:78
LogVariableStream::getVariables
const std::vector< LogVar > & getVariables() const
Return the list of all defined variables.
Definition: LogVariableStream.h:213
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
LogVariableStream::LogVariableStream
LogVariableStream()=default
Default constructor with empty text and no variables.
LogVariableStream::m_variables
std::vector< LogVar > m_variables
List of LogVars which were accepted so far.
Definition: LogVariableStream.h:224
LogVariableStream::__basic_ostream_type
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: LogVariableStream.h:91
LogVar::m_name
std::string m_name
Stores the name of the variable.
Definition: LogVariableStream.h:74
LogVariableStream::operator<<
std::enable_if< not std::is_fundamental< TText >::value, LogVariableStream & >::type operator<<(TText const &text)
Templated operator which will be used for all non-fundamental types.
Definition: LogVariableStream.h:148
LogVar::LogVar
LogVar(const std::string &name, const TVarType &v)
Constructor which accepts any type as value and relies on boost lexical cast.
Definition: LogVariableStream.h:41
LogVariableStream::operator<<
LogVariableStream & operator<<(LogVar const &var)
Operator override which stores the LogVar information instead of putting it directly in the sstream.
Definition: LogVariableStream.h:137