dune-pdelab  2.0.0
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 <ostream>
8 #include <sstream>
9 #include <string>
10 
11 #include <dune/common/shared_ptr.hh>
12 
13 namespace Dune {
14  namespace PDELab {
15 
17 
30  extern std::ostream &logtag(std::ostream &s);
31 
34  extern std::ostream &hostPidWallUserLogtagFormatFunc(std::ostream &s);
35 
38 
43  extern std::ostream &hostRankWallUserLogtagFormatFunc(std::ostream &s);
44 
46  extern std::ostream &nullFormatFunc(std::ostream &s);
47 
49 
59  extern void logtagSetupMPI(bool syncWidthes = true);
60 
62 
67 
72  virtual void writeTag(std::ostream &s) const = 0;
73  };
74 
76  template<class FormatFunc>
78  public LogtagFormatterBase
79  {
80  FormatFunc formatFunc;
81 
82  public:
84 
88  GeneralLogtagFormatter(const FormatFunc &formatFunc_) :
89  formatFunc(formatFunc_)
90  { }
92 
95  virtual void writeTag(std::ostream &s) const { formatFunc(s); }
96  };
98  template<class FormatFunc>
99  shared_ptr<LogtagFormatterBase>
100  makeGeneralLogtagFormatter(const FormatFunc &formatFunc)
101  { return make_shared<GeneralLogtagFormatter<FormatFunc> >(formatFunc); }
103  extern shared_ptr<LogtagFormatterBase>
104  makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream&));
105 
107  extern const shared_ptr<LogtagFormatterBase> &getLogtagFormatter();
109 
113  extern void
114  setLogtagFormatter(const shared_ptr<LogtagFormatterBase> &formatter
115  = shared_ptr<LogtagFormatterBase>());
117 
121  template<class FormatFunc>
122  void setLogtagFormatFunc(const FormatFunc &formatFunc)
124 
126 
134  extern void setLogtagRank(int rank);
135 
137 
142  class WithLogtag {
143  shared_ptr<LogtagFormatterBase> savedFormatter;
144 
145  public:
146  template<class FormatFunc>
147  WithLogtag(const FormatFunc &formatFunc) :
148  savedFormatter(getLogtagFormatter())
149  { setLogtagFormatFunc(formatFunc); }
150 
151  ~WithLogtag();
152  };
153 
155 
181  class LocalTag {
182  std::string str_;
183 
184  public:
186  inline const std::string &str() const { return str_; }
187 
189  template<class V>
190  LocalTag &operator<<(const V &v) {
191  std::stringstream s;
192  s << v;
193  str_ += s.str();
194  return *this;
195  }
196  };
197 
199  inline std::ostream &operator<<(std::ostream &s, const LocalTag &tag)
200  { return s << logtag << tag.str(); }
201 
202  } // namespace PDELab
203 } // namespace Dune
204 
205 #endif // DUNE_PDELAB_COMMON_LOGTAG_HH
const shared_ptr< LogtagFormatterBase > & getLogtagFormatter()
get the log tag formatter currently used by logtag()
Definition: logtag.cc:48
void setLogtagFormatFunc(const FormatFunc &formatFunc)
set a new log tag format function to be used by logtag()
Definition: logtag.hh:122
~WithLogtag()
Definition: logtag.cc:65
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 & operator<<(std::ostream &s, const TimeSpec &t)
insert a timespec into an output stream
Definition: clock.cc:39
void setLogtagFormatter(const shared_ptr< LogtagFormatterBase > &formatter)
set a new log tag formatter to be used by logtag()
Definition: logtag.cc:51
shared_ptr< LogtagFormatterBase > makeGeneralLogtagFormatter(std::ostream &(&formatFunc)(std::ostream &))
Convenience function to create a GeneralLogtagFormatter.
Definition: logtag.cc:32
GeneralLogtagFormatter(const FormatFunc &formatFunc_)
constructor
Definition: logtag.hh:88
Insert standard boilerplate into log messages.
Definition: logtag.hh:181
LocalTag & operator<<(const V &v)
append something to the static boilerplate message
Definition: logtag.hh:190
std::ostream & nullFormatFunc(std::ostream &s)
logtag format function that does not write anything
Definition: logtag.cc:202
const std::string & str() const
extract the static boilerplate message
Definition: logtag.hh:186
A log tag formatter that wraps a unary formatting function or functor.
Definition: logtag.hh:77
virtual void writeTag(std::ostream &s) const
write the tag to the stream
Definition: logtag.hh:95
temporarily use a different log tag format function
Definition: logtag.hh:142
std::ostream & hostPidWallUserLogtagFormatFunc(std::ostream &s)
logtag format function that includes host name, pid, wall time and CPU time
Definition: logtag.cc:192
virtual void writeTag(std::ostream &s) const =0
function that writes the tag to a stream
void setLogtagRank(int rank)
set the rank to be printed by logtag formatters
WithLogtag(const FormatFunc &formatFunc)
Definition: logtag.hh:147
void logtagSetupMPI(bool syncWidthes)
collect MPI information for the logtag formatters
Definition: logtag.cc:90
std::ostream & logtag(std::ostream &s)
function that writes a log tag to some stream
Definition: logtag.cc:59
virtual base class for logger formatters
Definition: logtag.hh:65
const std::string s
Definition: function.hh:1103