rippled
Journal.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED
21 #define BEAST_UTILITY_JOURNAL_H_INCLUDED
22 
23 #include <cassert>
24 #include <sstream>
25 
26 namespace beast {
27 
29 namespace severities {
31 enum Severity {
32  kAll = 0,
33 
40 
43 };
44 } // namespace severities
45 
58 class Journal
59 {
60 public:
61  class Sink;
62 
63 private:
64  // Severity level / threshold of a Journal message.
66 
67  // Invariant: m_sink always points to a valid Sink
69 
70 public:
71  //--------------------------------------------------------------------------
72 
74  class Sink
75  {
76  protected:
77  Sink() = delete;
78  explicit Sink(Sink const& sink) = default;
79  Sink(Severity thresh, bool console);
80  Sink&
81  operator=(Sink const& lhs) = delete;
82 
83  public:
84  virtual ~Sink() = 0;
85 
87  virtual bool
88  active(Severity level) const;
89 
92  virtual bool
93  console() const;
94 
97  virtual void
98  console(bool output);
99 
101  virtual Severity
102  threshold() const;
103 
105  virtual void
106  threshold(Severity thresh);
107 
112  virtual void
113  write(Severity level, std::string const& text) = 0;
114 
115  private:
117  bool m_console;
118  };
119 
120 #ifndef __INTELLISENSE__
121  static_assert(std::is_default_constructible<Sink>::value == false, "");
122  static_assert(std::is_copy_constructible<Sink>::value == false, "");
123  static_assert(std::is_move_constructible<Sink>::value == false, "");
124  static_assert(std::is_copy_assignable<Sink>::value == false, "");
125  static_assert(std::is_move_assignable<Sink>::value == false, "");
126  static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
127 #endif
128 
130  static Sink&
131  getNullSink();
132 
133  //--------------------------------------------------------------------------
134 
135  class Stream;
136 
137  /* Scoped ostream-based container for writing messages to a Journal. */
139  {
140  public:
142  : ScopedStream(other.m_sink, other.m_level)
143  {
144  }
145 
146  ScopedStream(Sink& sink, Severity level);
147 
148  template <typename T>
149  ScopedStream(Stream const& stream, T const& t);
150 
152 
153  ScopedStream&
154  operator=(ScopedStream const&) = delete;
155 
156  ~ScopedStream();
157 
159  ostream() const
160  {
161  return m_ostream;
162  }
163 
164  std::ostream&
165  operator<<(std::ostream& manip(std::ostream&)) const;
166 
167  template <typename T>
168  std::ostream&
169  operator<<(T const& t) const;
170 
171  private:
175  };
176 
177 #ifndef __INTELLISENSE__
178  static_assert(
180  "");
181  static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
182  static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
183  static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
184  static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
185  static_assert(
187  "");
188 #endif
189 
190  //--------------------------------------------------------------------------
191 public:
193  class Stream
194  {
195  public:
197  explicit Stream()
198  : m_sink(getNullSink()), m_level(severities::kDisabled)
199  {
200  }
201 
207  {
208  assert(m_level < severities::kDisabled);
209  }
210 
212  Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
213  {
214  }
215 
216  Stream&
217  operator=(Stream const& other) = delete;
218 
220  Sink&
221  sink() const
222  {
223  return m_sink;
224  }
225 
227  Severity
228  level() const
229  {
230  return m_level;
231  }
232 
235  bool
236  active() const
237  {
238  return m_sink.active(m_level);
239  }
240 
241  explicit operator bool() const
242  {
243  return active();
244  }
250  operator<<(std::ostream& manip(std::ostream&)) const;
251 
252  template <typename T>
254  operator<<(T const& t) const;
257  private:
260  };
261 
262 #ifndef __INTELLISENSE__
263  static_assert(std::is_default_constructible<Stream>::value == true, "");
264  static_assert(std::is_copy_constructible<Stream>::value == true, "");
265  static_assert(std::is_move_constructible<Stream>::value == true, "");
266  static_assert(std::is_copy_assignable<Stream>::value == false, "");
267  static_assert(std::is_move_assignable<Stream>::value == false, "");
268  static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
269 #endif
270 
271  //--------------------------------------------------------------------------
272 
274  Journal() = delete;
275 
277  explicit Journal(Sink& sink) : m_sink(&sink)
278  {
279  }
280 
282  Sink&
283  sink() const
284  {
285  return *m_sink;
286  }
287 
289  Stream
290  stream(Severity level) const
291  {
292  return Stream(*m_sink, level);
293  }
294 
299  bool
300  active(Severity level) const
301  {
302  return m_sink->active(level);
303  }
304 
307  Stream
308  trace() const
309  {
310  return {*m_sink, severities::kTrace};
311  }
312 
313  Stream
314  debug() const
315  {
316  return {*m_sink, severities::kDebug};
317  }
318 
319  Stream
320  info() const
321  {
322  return {*m_sink, severities::kInfo};
323  }
324 
325  Stream
326  warn() const
327  {
328  return {*m_sink, severities::kWarning};
329  }
330 
331  Stream
332  error() const
333  {
334  return {*m_sink, severities::kError};
335  }
336 
337  Stream
338  fatal() const
339  {
340  return {*m_sink, severities::kFatal};
341  }
343 };
344 
345 #ifndef __INTELLISENSE__
346 static_assert(std::is_default_constructible<Journal>::value == false, "");
347 static_assert(std::is_copy_constructible<Journal>::value == true, "");
348 static_assert(std::is_move_constructible<Journal>::value == true, "");
349 static_assert(std::is_copy_assignable<Journal>::value == true, "");
350 static_assert(std::is_move_assignable<Journal>::value == true, "");
351 static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
352 #endif
353 
354 //------------------------------------------------------------------------------
355 
356 template <typename T>
358  : ScopedStream(stream.sink(), stream.level())
359 {
360  m_ostream << t;
361 }
362 
363 template <typename T>
366 {
367  m_ostream << t;
368  return m_ostream;
369 }
370 
371 //------------------------------------------------------------------------------
372 
373 template <typename T>
376 {
377  return ScopedStream(*this, t);
378 }
379 
380 namespace detail {
381 
382 template <class CharT, class Traits = std::char_traits<CharT>>
383 class logstream_buf : public std::basic_stringbuf<CharT, Traits>
384 {
386 
387  template <class T>
388  void
389  write(T const*) = delete;
390 
391  void
392  write(char const* s)
393  {
394  if (strm_)
395  strm_ << s;
396  }
397 
398  void
399  write(wchar_t const* s)
400  {
401  if (strm_)
402  strm_ << s;
403  }
404 
405 public:
406  explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
407  {
408  }
409 
411  {
412  sync();
413  }
414 
415  int
416  sync() override
417  {
418  write(this->str().c_str());
419  this->str("");
420  return 0;
421  }
422 };
423 
424 } // namespace detail
425 
426 template <class CharT, class Traits = std::char_traits<CharT>>
427 class basic_logstream : public std::basic_ostream<CharT, Traits>
428 {
429  typedef CharT char_type;
430  typedef Traits traits_type;
431  typedef typename traits_type::int_type int_type;
432  typedef typename traits_type::pos_type pos_type;
433  typedef typename traits_type::off_type off_type;
434 
436 
437 public:
439  : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
440  {
441  }
442 };
443 
446 
447 } // namespace beast
448 
449 #endif
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:338
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
beast::severities::kTrace
@ kTrace
Definition: Journal.h:34
sstream
beast::Journal::ScopedStream::~ScopedStream
~ScopedStream()
Definition: beast_Journal.cpp:134
std::basic_stringbuf
beast::Journal::Stream::Stream
Stream(Stream const &other)
Construct or copy another Stream.
Definition: Journal.h:212
beast::Journal::Sink::operator=
Sink & operator=(Sink const &lhs)=delete
std::string
STL class.
beast::severities::kDisabled
@ kDisabled
Definition: Journal.h:41
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:308
beast::Journal::ScopedStream::m_ostream
std::ostringstream m_ostream
Definition: Journal.h:174
beast::Journal::ScopedStream::ScopedStream
ScopedStream(ScopedStream const &other)
Definition: Journal.h:141
std::is_nothrow_destructible
beast::Journal::Stream::level
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:228
beast::severities::kAll
@ kAll
Definition: Journal.h:32
beast::Journal::warn
Stream warn() const
Definition: Journal.h:326
std::is_default_constructible
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
std::is_move_constructible
beast::Journal::ScopedStream::m_level
const Severity m_level
Definition: Journal.h:173
beast::Journal::Journal
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:277
beast::Journal::Stream::sink
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:221
beast::Journal::Stream::operator<<
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
Definition: beast_Journal.cpp:155
beast::Journal::Stream::Stream
Stream()
Create a stream which produces no output.
Definition: Journal.h:197
beast::detail::logstream_buf::write
void write(char const *s)
Definition: Journal.h:392
beast::Journal::Sink::~Sink
virtual ~Sink()=0
std::ostream
STL class.
beast::Journal::ScopedStream::operator<<
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
Definition: beast_Journal.cpp:147
beast::Journal::sink
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:283
beast::Journal::active
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:300
beast::Journal::Sink::active
virtual bool active(Severity level) const
Returns true if text at the passed severity produces output.
Definition: beast_Journal.cpp:88
beast::Journal::stream
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:290
beast::Journal::Sink::Sink
Sink()=delete
beast::Journal::m_sink
Sink * m_sink
Definition: Journal.h:68
beast::Journal::Sink::console
virtual bool console() const
Returns true if a message is also written to the Output Window (MSVC).
Definition: beast_Journal.cpp:94
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:193
beast::Journal::error
Stream error() const
Definition: Journal.h:332
beast::Journal::info
Stream info() const
Definition: Journal.h:320
beast::basic_logstream
Definition: Journal.h:427
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
beast::basic_logstream::traits_type
Traits traits_type
Definition: Journal.h:430
beast::Journal::Journal
Journal()=delete
Journal has no default constructor.
beast::Journal::Stream::active
bool active() const
Returns true if sink logs anything at this stream's level.
Definition: Journal.h:236
beast::Journal::ScopedStream
Definition: Journal.h:138
beast::severities::kInfo
@ kInfo
Definition: Journal.h:36
beast::Journal::Sink::m_console
bool m_console
Definition: Journal.h:117
beast::Journal::Stream::m_level
Severity m_level
Definition: Journal.h:259
beast::basic_logstream::pos_type
traits_type::pos_type pos_type
Definition: Journal.h:432
beast::Journal::Stream::Stream
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:206
beast::severities::kNone
@ kNone
Definition: Journal.h:42
std::ostringstream
STL class.
beast::severities::kError
@ kError
Definition: Journal.h:38
std::is_copy_constructible
std::is_move_assignable
beast::basic_logstream::buf_
detail::logstream_buf< CharT, Traits > buf_
Definition: Journal.h:435
beast::basic_logstream::off_type
traits_type::off_type off_type
Definition: Journal.h:433
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:106
beast::Journal::Stream::m_sink
Sink & m_sink
Definition: Journal.h:258
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
beast::detail::logstream_buf
Definition: Journal.h:383
beast::Journal::ScopedStream::operator=
ScopedStream & operator=(ScopedStream const &)=delete
beast::detail::logstream_buf::write
void write(wchar_t const *s)
Definition: Journal.h:399
std
STL namespace.
beast::severities::kWarning
@ kWarning
Definition: Journal.h:37
cassert
beast::basic_logstream::basic_logstream
basic_logstream(beast::Journal::Stream const &strm)
Definition: Journal.h:438
beast::basic_logstream::int_type
traits_type::int_type int_type
Definition: Journal.h:431
beast::detail::logstream_buf::sync
int sync() override
Definition: Journal.h:416
beast::severities::kDebug
@ kDebug
Definition: Journal.h:35
beast::Journal::Stream::operator=
Stream & operator=(Stream const &other)=delete
beast::Journal::debug
Stream debug() const
Definition: Journal.h:314
beast::detail::logstream_buf::logstream_buf
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:406
std::is_copy_assignable
beast::detail::logstream_buf::strm_
beast::Journal::Stream strm_
Definition: Journal.h:385
beast::severities::kFatal
@ kFatal
Definition: Journal.h:39
beast::Journal::Sink::thresh_
Severity thresh_
Definition: Journal.h:116
beast::Journal::ScopedStream::m_sink
Sink & m_sink
Definition: Journal.h:172
beast::Journal::Sink::write
virtual void write(Severity level, std::string const &text)=0
Write text to the sink at the specified severity.
beast::basic_logstream::char_type
CharT char_type
Definition: Journal.h:429
beast::Journal::ScopedStream::ostream
std::ostringstream & ostream() const
Definition: Journal.h:159
beast::detail::logstream_buf::~logstream_buf
~logstream_buf()
Definition: Journal.h:410
beast
Definition: base_uint.h:641