dune-pdelab  2.7-git
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  virtual ~LogtagFormatterBase() = default;
74 
75  };
76 
78  template<class FormatFunc>
80  public LogtagFormatterBase
81  {
82  FormatFunc formatFunc;
83 
84  public:
86 
90  GeneralLogtagFormatter(const FormatFunc &formatFunc_) :
91  formatFunc(formatFunc_)
92  { }
94 
97  virtual void writeTag(std::ostream &s) const override { formatFunc(s); }
98  };
100  template<class FormatFunc>
101  std::shared_ptr<LogtagFormatterBase>
102  makeGeneralLogtagFormatter(const FormatFunc &formatFunc)
103  { return std::make_shared<GeneralLogtagFormatter<FormatFunc> >(formatFunc); }
105  extern std::shared_ptr<LogtagFormatterBase>
106  makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream&));
107 
109  extern const std::shared_ptr<LogtagFormatterBase> &getLogtagFormatter();
111 
115  extern void
116  setLogtagFormatter(const std::shared_ptr<LogtagFormatterBase> &formatter
117  = std::shared_ptr<LogtagFormatterBase>());
119 
123  template<class FormatFunc>
124  void setLogtagFormatFunc(const FormatFunc &formatFunc)
126 
128 
133  class WithLogtag {
134  std::shared_ptr<LogtagFormatterBase> savedFormatter;
135 
136  public:
137  template<class FormatFunc>
138  WithLogtag(const FormatFunc &formatFunc) :
139  savedFormatter(getLogtagFormatter())
140  { setLogtagFormatFunc(formatFunc); }
141 
142  ~WithLogtag();
143  };
144 
146 
172  class LocalTag {
173  std::string str_;
174 
175  public:
177  inline const std::string &str() const { return str_; }
178 
180  template<class V>
181  LocalTag &operator<<(const V &v) {
182  std::stringstream s;
183  s << v;
184  str_ += s.str();
185  return *this;
186  }
187  };
188 
190  inline std::ostream &operator<<(std::ostream &s, const LocalTag &tag)
191  { return s << logtag << tag.str(); }
192 
193  } // namespace PDELab
194 } // namespace Dune
195 
196 #endif // DUNE_PDELAB_COMMON_LOGTAG_HH
const std::string s
Definition: function.hh:843
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
std::ostream & operator<<(std::ostream &os, const StatisticsResult< T > &result)
Write statistics result to out stream.
Definition: solverstatistics.hh:140
std::ostream & hostRankWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes hostname, rank (if available), wall time and CPU time
Definition: logtag.cc:179
void logtagSetupMPI(bool syncWidthes)
collect MPI information for the logtag formatters
Definition: logtag.cc:88
std::ostream & nullFormatFunc(std::ostream &s)
logtag format function that does not write anything
Definition: logtag.cc:202
std::ostream & hostPidWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes host name, pid, wall time and CPU time
Definition: logtag.cc:192
void setLogtagFormatter(const std::shared_ptr< LogtagFormatterBase > &formatter)
set a new log tag formatter to be used by logtag()
Definition: logtag.cc:51
std::ostream & logtag(std::ostream &s)
function that writes a log tag to some stream
Definition: logtag.cc:59
const std::shared_ptr< LogtagFormatterBase > & getLogtagFormatter()
get the log tag formatter currently used by logtag()
Definition: logtag.cc:48
std::shared_ptr< LogtagFormatterBase > makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream &))
Convenience function to create a GeneralLogtagFormatter.
Definition: logtag.cc:32
void setLogtagFormatFunc(const FormatFunc &formatFunc)
set a new log tag format function to be used by logtag()
Definition: logtag.hh:124
virtual base class for logger formatters
Definition: logtag.hh:64
virtual ~LogtagFormatterBase()=default
virtual void writeTag(std::ostream &s) const =0
function that writes the tag to a stream
A log tag formatter that wraps a unary formatting function or functor.
Definition: logtag.hh:81
virtual void writeTag(std::ostream &s) const override
write the tag to the stream
Definition: logtag.hh:97
GeneralLogtagFormatter(const FormatFunc &formatFunc_)
constructor
Definition: logtag.hh:90
temporarily use a different log tag format function
Definition: logtag.hh:133
~WithLogtag()
Definition: logtag.cc:65
WithLogtag(const FormatFunc &formatFunc)
Definition: logtag.hh:138
Insert standard boilerplate into log messages.
Definition: logtag.hh:172
const std::string & str() const
extract the static boilerplate message
Definition: logtag.hh:177
LocalTag & operator<<(const V &v)
append something to the static boilerplate message
Definition: logtag.hh:181