dune-pdelab  2.5-dev
logtag.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_PDELAB_COMMON_LOGTAG_HH
5 #define DUNE_PDELAB_COMMON_LOGTAG_HH
6 
7 #include <memory>
8 #include <ostream>
9 #include <sstream>
10 #include <string>
11 
12 namespace Dune {
13  namespace PDELab {
14 
16 
29  extern std::ostream &logtag(std::ostream &s);
30 
33  extern std::ostream &hostPidWallUserLogtagFormatFunc(std::ostream &s);
34 
37 
42  extern std::ostream &hostRankWallUserLogtagFormatFunc(std::ostream &s);
43 
45  extern std::ostream &nullFormatFunc(std::ostream &s);
46 
48 
58  extern void logtagSetupMPI(bool syncWidthes = true);
59 
61 
66 
71  virtual void writeTag(std::ostream &s) const = 0;
72  };
73 
75  template<class FormatFunc>
77  public LogtagFormatterBase
78  {
79  FormatFunc formatFunc;
80 
81  public:
83 
87  GeneralLogtagFormatter(const FormatFunc &formatFunc_) :
88  formatFunc(formatFunc_)
89  { }
91 
94  virtual void writeTag(std::ostream &s) const override { formatFunc(s); }
95  };
97  template<class FormatFunc>
98  std::shared_ptr<LogtagFormatterBase>
99  makeGeneralLogtagFormatter(const FormatFunc &formatFunc)
100  { return std::make_shared<GeneralLogtagFormatter<FormatFunc> >(formatFunc); }
102  extern std::shared_ptr<LogtagFormatterBase>
103  makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream&));
104 
106  extern const std::shared_ptr<LogtagFormatterBase> &getLogtagFormatter();
108 
112  extern void
113  setLogtagFormatter(const std::shared_ptr<LogtagFormatterBase> &formatter
114  = std::shared_ptr<LogtagFormatterBase>());
116 
120  template<class FormatFunc>
121  void setLogtagFormatFunc(const FormatFunc &formatFunc)
123 
125 
130  class WithLogtag {
131  std::shared_ptr<LogtagFormatterBase> savedFormatter;
132 
133  public:
134  template<class FormatFunc>
135  WithLogtag(const FormatFunc &formatFunc) :
136  savedFormatter(getLogtagFormatter())
137  { setLogtagFormatFunc(formatFunc); }
138 
139  ~WithLogtag();
140  };
141 
143 
169  class LocalTag {
170  std::string str_;
171 
172  public:
174  inline const std::string &str() const { return str_; }
175 
177  template<class V>
178  LocalTag &operator<<(const V &v) {
179  std::stringstream s;
180  s << v;
181  str_ += s.str();
182  return *this;
183  }
184  };
185 
187  inline std::ostream &operator<<(std::ostream &s, const LocalTag &tag)
188  { return s << logtag << tag.str(); }
189 
190  } // namespace PDELab
191 } // namespace Dune
192 
193 #endif // DUNE_PDELAB_COMMON_LOGTAG_HH
Dune::PDELab::setLogtagFormatFunc
void setLogtagFormatFunc(const FormatFunc &formatFunc)
set a new log tag format function to be used by logtag()
Definition: logtag.hh:121
Dune::PDELab::nullFormatFunc
std::ostream & nullFormatFunc(std::ostream &s)
logtag format function that does not write anything
Definition: logtag.cc:202
Dune::PDELab::getLogtagFormatter
const std::shared_ptr< LogtagFormatterBase > & getLogtagFormatter()
get the log tag formatter currently used by logtag()
Definition: logtag.cc:48
Dune::PDELab::logtag
std::ostream & logtag(std::ostream &s)
function that writes a log tag to some stream
Definition: logtag.cc:59
Dune::PDELab::LocalTag::str
const std::string & str() const
extract the static boilerplate message
Definition: logtag.hh:174
Dune::PDELab::WithLogtag
temporarily use a different log tag format function
Definition: logtag.hh:130
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::LogtagFormatterBase
virtual base class for logger formatters
Definition: logtag.hh:64
Dune::PDELab::GeneralLogtagFormatter::writeTag
virtual void writeTag(std::ostream &s) const override
write the tag to the stream
Definition: logtag.hh:94
Dune::PDELab::hostRankWallUserLogtagFormatFunc
std::ostream & hostRankWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes hostname, rank (if available), wall time and CPU time
Definition: logtag.cc:179
Dune::PDELab::WithLogtag::~WithLogtag
~WithLogtag()
Definition: logtag.cc:65
Dune::PDELab::setLogtagFormatter
void setLogtagFormatter(const std::shared_ptr< LogtagFormatterBase > &formatter)
set a new log tag formatter to be used by logtag()
Definition: logtag.cc:51
Dune::PDELab::GeneralLogtagFormatter::GeneralLogtagFormatter
GeneralLogtagFormatter(const FormatFunc &formatFunc_)
constructor
Definition: logtag.hh:87
Dune::PDELab::hostPidWallUserLogtagFormatFunc
std::ostream & hostPidWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes host name, pid, wall time and CPU time
Definition: logtag.cc:192
Dune::PDELab::LogtagFormatterBase::writeTag
virtual void writeTag(std::ostream &s) const =0
function that writes the tag to a stream
Dune::PDELab::operator<<
std::ostream & operator<<(std::ostream &s, const TimeSpec &t)
insert a timespec into an output stream
Definition: clock.cc:39
Dune::PDELab::GeneralLogtagFormatter
A log tag formatter that wraps a unary formatting function or functor.
Definition: logtag.hh:76
s
const std::string s
Definition: function.hh:830
Dune::PDELab::LocalTag::operator<<
LocalTag & operator<<(const V &v)
append something to the static boilerplate message
Definition: logtag.hh:178
Dune::PDELab::WithLogtag::WithLogtag
WithLogtag(const FormatFunc &formatFunc)
Definition: logtag.hh:135
Dune::PDELab::LocalTag
Insert standard boilerplate into log messages.
Definition: logtag.hh:169
Dune::PDELab::logtagSetupMPI
void logtagSetupMPI(bool syncWidthes)
collect MPI information for the logtag formatters
Definition: logtag.cc:88
Dune::PDELab::makeGeneralLogtagFormatter
std::shared_ptr< LogtagFormatterBase > makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream &))
Convenience function to create a GeneralLogtagFormatter.
Definition: logtag.cc:32