MessageLogger.h
Go to the documentation of this file.
1 #ifndef _utl_MessageLogger_h_
2 #define _utl_MessageLogger_h_
3 
10 #include <iomanip>
11 #include <iosfwd>
12 #include <iostream>
13 
14 
15 namespace utl {
16 
38  class MessageLogger {
39  public:
48  class Message {
58  template<typename T>
59  Message(MessageLogger& logger, const T& t, unsigned int l, bool e) :
60  fLogger(logger),
61  fMessageLevel(l),
62  fTrailEOL(e)
63  {
64  // Set & backup.
65  // See utl::MessageLogger::ApplyConfigurationOn.
66  fPrevPrec = logger.ResolveStream().precision(logger.GetNPrecDigits());
67  fPrevFlag = logger.ResolveStream().setf(std::ios::fixed, std::ios::floatfield);
68  fLogger.Log(t, l);
69  }
71  friend class MessageLogger;
72  public:
77  template<typename T>
78  Message&
79  operator()(const T& t)
80  {
83  return *this;
84  }
89  {
90  if (fTrailEOL)
91  fLogger.Log('\n', fMessageLevel);
92  // ... and restore (see constructor).
93  fLogger.ResolveStream().precision(fPrevPrec);
95  }
105  // Yep, always false: meant to be used in termination conditions.
106  explicit operator bool() const { return false; }
107  private:
109  unsigned int fMessageLevel;
110  bool fTrailEOL;
111  std::streamsize fPrevPrec;
112  std::ios_base::fmtflags fPrevFlag;
113  };
114 
124  MessageLogger(std::ostream& os, unsigned int l);
135  template<typename T>
136  Message operator()(const T& message, unsigned int level, bool trailEOL)
137  { return Message(*this, message, level, trailEOL); }
141  template<typename T>
142  void
143  Log(const T& message, unsigned int l)
144  {
145  if (l <= fLevel) {
146  // Increase first...
147  ++fFlushCounter;
148  // perform the output...
149  ResolveStream() << message;
150  // and then check if flush has to be performed
151  // (when zero, never flush).
152  if (fFlushCounter == fFlushPeriod) {
153  ResolveStream().flush();
154  // Reset, note that as we increase first the counter and then
155  // compare with the period we honor the criteria "no flush
156  // if period is zero).
157  fFlushCounter = 0;
158  }
159  }
160  }
164  unsigned int GetLevel() const { return fLevel; }
168  void SetLevel(unsigned int l) { fLevel = l; }
172  unsigned int GetNPrecDigits() const { return fNPrecDigits; }
176  void SetNPrecDigits(unsigned int n) { fNPrecDigits = n; }
182  unsigned int GetFlushPeriod() const { return fFlushPeriod; }
188  void SetFlushPeriod(unsigned int p) { fFlushPeriod = p; }
192  template<class S>
193  void
194  ApplyConfigurationOn(S& stream)
195  const
196  {
197  // See Message's constructor and detructor.
198  //
199  // This is done here with manipulators so as to be usable with
200  // streams like utl::TabularStream which doesn't have member functions to do this.
201  stream << std::setprecision(fNPrecDigits) << std::fixed;
202  }
203  private:
204  std::ostream&
206  {
207  // Allow the given stream no to be opened...
208  // is_open is strictly for ofstreams: use good that is included in ostream already.
209  return fStream.good() ? fStream : std::cout;
210  }
212  std::ostream& fStream;
214  unsigned int fLevel;
215  // The type is an implementation-defined synonymn of one of the signed basic integral types (generally signed int or signed long)
216  unsigned int fNPrecDigits;
218  unsigned int fFlushCounter;
220  unsigned int fFlushPeriod;
221  };
222 
223 }
224 
225 
226 #endif
void SetLevel(unsigned int l)
Set a new level.
Message operator()(const T &message, unsigned int level, bool trailEOL)
Start of message logging.
void Log(const T &message, unsigned int l)
Actual logging function against the given stream.
unsigned int fFlushPeriod
Flush the stream every this number of times of calls to Log (zero means no flush at all)...
unsigned int fFlushCounter
Current flush count.
MessageLogger(std::ostream &os, unsigned int l)
Construct a logger to handle the messages.
std::ostream & fStream
Output stream.
void SetNPrecDigits(unsigned int n)
Set a new number of digits.
unsigned int fNPrecDigits
#define S
void SetFlushPeriod(unsigned int p)
Set a new period.
unsigned int GetLevel() const
Return the current level set.
Message & operator()(const T &t)
Append a value separated from previous values.
Definition: MessageLogger.h:79
unsigned int fLevel
Threshold level for message output.
std::ostream & ResolveStream()
std::ios_base::fmtflags fPrevFlag
unsigned int GetFlushPeriod() const
Return the current period.
Message(MessageLogger &logger, const T &t, unsigned int l, bool e)
Construct a message with the initial value and the given level.
Definition: MessageLogger.h:59
Handle diagnosis messages output.
Definition: MessageLogger.h:38
unsigned int GetNPrecDigits() const
Return the number of digits for floating-point numbers.
~Message()
On destruction line termination is appended.
Definition: MessageLogger.h:88

, generated on Tue Sep 26 2023.