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 { 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 
133  extern void setLogtagRank(int rank);
134 
136 
141  class WithLogtag {
142  std::shared_ptr<LogtagFormatterBase> savedFormatter;
143 
144  public:
145  template<class FormatFunc>
146  WithLogtag(const FormatFunc &formatFunc) :
147  savedFormatter(getLogtagFormatter())
148  { setLogtagFormatFunc(formatFunc); }
149 
150  ~WithLogtag();
151  };
152 
154 
180  class LocalTag {
181  std::string str_;
182 
183  public:
185  inline const std::string &str() const { return str_; }
186 
188  template<class V>
189  LocalTag &operator<<(const V &v) {
190  std::stringstream s;
191  s << v;
192  str_ += s.str();
193  return *this;
194  }
195  };
196 
198  inline std::ostream &operator<<(std::ostream &s, const LocalTag &tag)
199  { return s << logtag << tag.str(); }
200 
201  } // namespace PDELab
202 } // namespace Dune
203 
204 #endif // DUNE_PDELAB_COMMON_LOGTAG_HH
A log tag formatter that wraps a unary formatting function or functor.
Definition: logtag.hh:76
GeneralLogtagFormatter(const FormatFunc &formatFunc_)
constructor
Definition: logtag.hh:87
void setLogtagFormatter(const std::shared_ptr< LogtagFormatterBase > &formatter)
set a new log tag formatter to be used by logtag()
Definition: logtag.cc:51
const std::string s
Definition: function.hh:1101
Insert standard boilerplate into log messages.
Definition: logtag.hh:180
void setLogtagRank(int rank)
set the rank to be printed by logtag formatters
void logtagSetupMPI(bool syncWidthes)
collect MPI information for the logtag formatters
Definition: logtag.cc:90
const std::string & str() const
extract the static boilerplate message
Definition: logtag.hh:185
std::ostream & hostPidWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes host name, pid, wall time and CPU time
Definition: logtag.cc:192
void setLogtagFormatFunc(const FormatFunc &formatFunc)
set a new log tag format function to be used by logtag()
Definition: logtag.hh:121
LocalTag & operator<<(const V &v)
append something to the static boilerplate message
Definition: logtag.hh:189
WithLogtag(const FormatFunc &formatFunc)
Definition: logtag.hh:146
virtual base class for logger formatters
Definition: logtag.hh:64
std::ostream & hostRankWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes hostname, rank (if available), wall time and CPU time ...
Definition: logtag.cc:179
std::ostream & nullFormatFunc(std::ostream &s)
logtag format function that does not write anything
Definition: logtag.cc:202
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
temporarily use a different log tag format function
Definition: logtag.hh:141
std::shared_ptr< LogtagFormatterBase > makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream &))
Convenience function to create a GeneralLogtagFormatter.
Definition: logtag.cc:32
const std::shared_ptr< LogtagFormatterBase > & getLogtagFormatter()
get the log tag formatter currently used by logtag()
Definition: logtag.cc:48
std::ostream & logtag(std::ostream &s)
function that writes a log tag to some stream
Definition: logtag.cc:59
virtual void writeTag(std::ostream &s) const
write the tag to the stream
Definition: logtag.hh:94
std::ostream & operator<<(std::ostream &s, const TimeSpec &t)
insert a timespec into an output stream
Definition: clock.cc:39
virtual void writeTag(std::ostream &s) const =0
function that writes the tag to a stream