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 private:
138  /* Scoped ostream-based container for writing messages to a Journal. */
140  {
141  public:
143  : ScopedStream(other.m_sink, other.m_level)
144  {
145  }
146 
147  ScopedStream(Sink& sink, Severity level);
148 
149  template <typename T>
150  ScopedStream(Stream const& stream, T const& t);
151 
153 
154  ScopedStream&
155  operator=(ScopedStream const&) = delete;
156 
157  ~ScopedStream();
158 
160  ostream() const
161  {
162  return m_ostream;
163  }
164 
165  std::ostream&
166  operator<<(std::ostream& manip(std::ostream&)) const;
167 
168  template <typename T>
169  std::ostream&
170  operator<<(T const& t) const;
171 
172  private:
176  };
177 
178 #ifndef __INTELLISENSE__
179  static_assert(
181  "");
182  static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
183  static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
184  static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
185  static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
186  static_assert(
188  "");
189 #endif
190 
191  //--------------------------------------------------------------------------
192 public:
194  class Stream
195  {
196  public:
198  explicit Stream()
199  : m_sink(getNullSink()), m_level(severities::kDisabled)
200  {
201  }
202 
208  {
209  assert(m_level < severities::kDisabled);
210  }
211 
213  Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
214  {
215  }
216 
217  Stream&
218  operator=(Stream const& other) = delete;
219 
221  Sink&
222  sink() const
223  {
224  return m_sink;
225  }
226 
228  Severity
229  level() const
230  {
231  return m_level;
232  }
233 
236  bool
237  active() const
238  {
239  return m_sink.active(m_level);
240  }
241 
242  explicit operator bool() const
243  {
244  return active();
245  }
251  operator<<(std::ostream& manip(std::ostream&)) const;
252 
253  template <typename T>
255  operator<<(T const& t) const;
258  private:
261  };
262 
263 #ifndef __INTELLISENSE__
264  static_assert(std::is_default_constructible<Stream>::value == true, "");
265  static_assert(std::is_copy_constructible<Stream>::value == true, "");
266  static_assert(std::is_move_constructible<Stream>::value == true, "");
267  static_assert(std::is_copy_assignable<Stream>::value == false, "");
268  static_assert(std::is_move_assignable<Stream>::value == false, "");
269  static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
270 #endif
271 
272  //--------------------------------------------------------------------------
273 
275  Journal() = delete;
276 
278  explicit Journal(Sink& sink) : m_sink(&sink)
279  {
280  }
281 
283  Sink&
284  sink() const
285  {
286  return *m_sink;
287  }
288 
290  Stream
291  stream(Severity level) const
292  {
293  return Stream(*m_sink, level);
294  }
295 
300  bool
301  active(Severity level) const
302  {
303  return m_sink->active(level);
304  }
305 
308  Stream
309  trace() const
310  {
311  return {*m_sink, severities::kTrace};
312  }
313 
314  Stream
315  debug() const
316  {
317  return {*m_sink, severities::kDebug};
318  }
319 
320  Stream
321  info() const
322  {
323  return {*m_sink, severities::kInfo};
324  }
325 
326  Stream
327  warn() const
328  {
329  return {*m_sink, severities::kWarning};
330  }
331 
332  Stream
333  error() const
334  {
335  return {*m_sink, severities::kError};
336  }
337 
338  Stream
339  fatal() const
340  {
341  return {*m_sink, severities::kFatal};
342  }
344 };
345 
346 #ifndef __INTELLISENSE__
347 static_assert(std::is_default_constructible<Journal>::value == false, "");
348 static_assert(std::is_copy_constructible<Journal>::value == true, "");
349 static_assert(std::is_move_constructible<Journal>::value == true, "");
350 static_assert(std::is_copy_assignable<Journal>::value == true, "");
351 static_assert(std::is_move_assignable<Journal>::value == true, "");
352 static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
353 #endif
354 
355 //------------------------------------------------------------------------------
356 
357 template <typename T>
359  : ScopedStream(stream.sink(), stream.level())
360 {
361  m_ostream << t;
362 }
363 
364 template <typename T>
367 {
368  m_ostream << t;
369  return m_ostream;
370 }
371 
372 //------------------------------------------------------------------------------
373 
374 template <typename T>
377 {
378  return ScopedStream(*this, t);
379 }
380 
381 namespace detail {
382 
383 template <class CharT, class Traits = std::char_traits<CharT>>
384 class logstream_buf : public std::basic_stringbuf<CharT, Traits>
385 {
387 
388  template <class T>
389  void
390  write(T const*) = delete;
391 
392  void
393  write(char const* s)
394  {
395  if (strm_)
396  strm_ << s;
397  }
398 
399  void
400  write(wchar_t const* s)
401  {
402  if (strm_)
403  strm_ << s;
404  }
405 
406 public:
407  explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
408  {
409  }
410 
412  {
413  sync();
414  }
415 
416  int
417  sync() override
418  {
419  write(this->str().c_str());
420  this->str("");
421  return 0;
422  }
423 };
424 
425 } // namespace detail
426 
427 template <class CharT, class Traits = std::char_traits<CharT>>
428 class basic_logstream : public std::basic_ostream<CharT, Traits>
429 {
430  typedef CharT char_type;
431  typedef Traits traits_type;
432  typedef typename traits_type::int_type int_type;
433  typedef typename traits_type::pos_type pos_type;
434  typedef typename traits_type::off_type off_type;
435 
437 
438 public:
440  : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
441  {
442  }
443 };
444 
447 
448 } // namespace beast
449 
450 #endif
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
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:213
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:309
beast::Journal::ScopedStream::m_ostream
std::ostringstream m_ostream
Definition: Journal.h:175
beast::Journal::ScopedStream::ScopedStream
ScopedStream(ScopedStream const &other)
Definition: Journal.h:142
std::is_nothrow_destructible
beast::Journal::Stream::level
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:229
beast::severities::kAll
@ kAll
Definition: Journal.h:32
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
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:174
beast::Journal::Journal
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:278
beast::Journal::Stream::sink
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:222
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:198
beast::detail::logstream_buf::write
void write(char const *s)
Definition: Journal.h:393
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:284
beast::Journal::active
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:301
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:291
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:194
beast::Journal::error
Stream error() const
Definition: Journal.h:333
beast::Journal::info
Stream info() const
Definition: Journal.h:321
beast::basic_logstream
Definition: Journal.h:428
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
beast::basic_logstream::traits_type
Traits traits_type
Definition: Journal.h:431
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:237
beast::Journal::ScopedStream
Definition: Journal.h:139
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:260
beast::basic_logstream::pos_type
traits_type::pos_type pos_type
Definition: Journal.h:433
beast::Journal::Stream::Stream
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:207
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:436
beast::basic_logstream::off_type
traits_type::off_type off_type
Definition: Journal.h:434
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:259
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
beast::detail::logstream_buf
Definition: Journal.h:384
beast::Journal::ScopedStream::operator=
ScopedStream & operator=(ScopedStream const &)=delete
beast::detail::logstream_buf::write
void write(wchar_t const *s)
Definition: Journal.h:400
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:439
beast::basic_logstream::int_type
traits_type::int_type int_type
Definition: Journal.h:432
beast::detail::logstream_buf::sync
int sync() override
Definition: Journal.h:417
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:315
beast::detail::logstream_buf::logstream_buf
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:407
std::is_copy_assignable
beast::detail::logstream_buf::strm_
beast::Journal::Stream strm_
Definition: Journal.h:386
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:173
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:430
beast::Journal::ScopedStream::ostream
std::ostringstream & ostream() const
Definition: Journal.h:160
beast::detail::logstream_buf::~logstream_buf
~logstream_buf()
Definition: Journal.h:411
beast
Definition: base_uint.h:641