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
30 {
32  enum Severity
33  {
34  kAll = 0,
35 
42 
45  };
46 }
47 
60 class Journal
61 {
62 public:
63  class Sink;
64 
65 private:
66  // Severity level / threshold of a Journal message.
68 
69  // Invariant: m_sink always points to a valid Sink
71 
72 public:
73  //--------------------------------------------------------------------------
74 
76  class Sink
77  {
78  protected:
79  Sink () = delete;
80  explicit Sink(Sink const& sink) = default;
81  Sink (Severity thresh, bool console);
82  Sink& operator= (Sink const& lhs) = delete;
83 
84  public:
85  virtual ~Sink () = 0;
86 
88  virtual bool active (Severity level) const;
89 
91  virtual bool console () const;
92 
94  virtual void console (bool output);
95 
97  virtual Severity threshold() const;
98 
100  virtual void threshold (Severity thresh);
101 
106  virtual void write (Severity level, std::string const& text) = 0;
107 
108  private:
110  bool m_console;
111  };
112 
113 #ifndef __INTELLISENSE__
114 static_assert(std::is_default_constructible<Sink>::value == false, "");
115 static_assert(std::is_copy_constructible<Sink>::value == false, "");
116 static_assert(std::is_move_constructible<Sink>::value == false, "");
117 static_assert(std::is_copy_assignable<Sink>::value == false, "");
118 static_assert(std::is_move_assignable<Sink>::value == false, "");
119 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
120 #endif
121 
123  static Sink& getNullSink ();
124 
125  //--------------------------------------------------------------------------
126 
127  class Stream;
128 
129 private:
130  /* Scoped ostream-based container for writing messages to a Journal. */
132  {
133  public:
134  ScopedStream (ScopedStream const& other)
135  : ScopedStream (other.m_sink, other.m_level)
136  { }
137 
138  ScopedStream (Sink& sink, Severity level);
139 
140  template <typename T>
141  ScopedStream (Stream const& stream, T const& t);
142 
143  ScopedStream (
144  Stream const& stream, std::ostream& manip (std::ostream&));
145 
146  ScopedStream& operator= (ScopedStream const&) = delete;
147 
148  ~ScopedStream ();
149 
151  {
152  return m_ostream;
153  }
154 
156  std::ostream& manip (std::ostream&)) const;
157 
158  template <typename T>
159  std::ostream& operator<< (T const& t) const;
160 
161  private:
165  };
166 
167 #ifndef __INTELLISENSE__
168 static_assert(std::is_default_constructible<ScopedStream>::value == false, "");
169 static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
170 static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
171 static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
172 static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
173 static_assert(std::is_nothrow_destructible<ScopedStream>::value == true, "");
174 #endif
175 
176  //--------------------------------------------------------------------------
177 public:
179  class Stream
180  {
181  public:
183  explicit Stream ()
184  : m_sink (getNullSink())
185  , m_level (severities::kDisabled)
186  { }
187 
193  : m_sink (sink)
194  , m_level (level)
195  {
196  assert (m_level < severities::kDisabled);
197  }
198 
200  Stream (Stream const& other)
201  : Stream (other.m_sink, other.m_level)
202  { }
203 
204  Stream& operator= (Stream const& other) = delete;
205 
207  Sink& sink() const
208  {
209  return m_sink;
210  }
211 
213  Severity level() const
214  {
215  return m_level;
216  }
217 
220  bool active() const
221  {
222  return m_sink.active (m_level);
223  }
224 
225  explicit
226  operator bool() const
227  {
228  return active();
229  }
235 
236  template <typename T>
237  ScopedStream operator<< (T const& t) const;
240  private:
243  };
244 
245 #ifndef __INTELLISENSE__
246 static_assert(std::is_default_constructible<Stream>::value == true, "");
247 static_assert(std::is_copy_constructible<Stream>::value == true, "");
248 static_assert(std::is_move_constructible<Stream>::value == true, "");
249 static_assert(std::is_copy_assignable<Stream>::value == false, "");
250 static_assert(std::is_move_assignable<Stream>::value == false, "");
251 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
252 #endif
253 
254  //--------------------------------------------------------------------------
255 
257  Journal () = delete;
258 
260  explicit Journal (Sink& sink)
261  : m_sink (&sink)
262  { }
263 
265  Sink& sink() const
266  {
267  return *m_sink;
268  }
269 
271  Stream stream (Severity level) const
272  {
273  return Stream (*m_sink, level);
274  }
275 
280  bool active (Severity level) const
281  {
282  return m_sink->active (level);
283  }
284 
287  Stream trace() const
288  {
289  return { *m_sink, severities::kTrace };
290  }
291 
292  Stream debug() const
293  {
294  return { *m_sink, severities::kDebug };
295  }
296 
297  Stream info() const
298  {
299  return { *m_sink, severities::kInfo };
300  }
301 
302  Stream warn() const
303  {
304  return { *m_sink, severities::kWarning };
305  }
306 
307  Stream error() const
308  {
309  return { *m_sink, severities::kError };
310  }
311 
312  Stream fatal() const
313  {
314  return { *m_sink, severities::kFatal };
315  }
317 };
318 
319 #ifndef __INTELLISENSE__
320 static_assert(std::is_default_constructible<Journal>::value == false, "");
321 static_assert(std::is_copy_constructible<Journal>::value == true, "");
322 static_assert(std::is_move_constructible<Journal>::value == true, "");
323 static_assert(std::is_copy_assignable<Journal>::value == true, "");
324 static_assert(std::is_move_assignable<Journal>::value == true, "");
325 static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
326 #endif
327 
328 //------------------------------------------------------------------------------
329 
330 template <typename T>
332  : ScopedStream (stream.sink(), stream.level())
333 {
334  m_ostream << t;
335 }
336 
337 template <typename T>
340 {
341  m_ostream << t;
342  return m_ostream;
343 }
344 
345 //------------------------------------------------------------------------------
346 
347 template <typename T>
350 {
351  return ScopedStream (*this, t);
352 }
353 
354 namespace detail {
355 
356 template<class CharT, class Traits = std::char_traits<CharT>>
358  : public std::basic_stringbuf<CharT, Traits>
359 {
361 
362  template<class T>
363  void write(T const*) = delete;
364 
365  void write(char const* s)
366  {
367  if(strm_)
368  strm_ << s;
369  }
370 
371  void write(wchar_t const* s)
372  {
373  if(strm_)
374  strm_ << s;
375  }
376 
377 public:
378  explicit
380  : strm_(strm)
381  {
382  }
383 
385  {
386  sync();
387  }
388 
389  int
390  sync() override
391  {
392  write(this->str().c_str());
393  this->str("");
394  return 0;
395  }
396 };
397 
398 } // detail
399 
400 template<
401  class CharT,
402  class Traits = std::char_traits<CharT>
403 >
405  : public std::basic_ostream<CharT, Traits>
406 {
407  typedef CharT char_type;
408  typedef Traits traits_type;
409  typedef typename traits_type::int_type int_type;
410  typedef typename traits_type::pos_type pos_type;
411  typedef typename traits_type::off_type off_type;
412 
414 public:
415  explicit
417  : std::basic_ostream<CharT, Traits>(&buf_)
418  , buf_(strm)
419  {
420  }
421 };
422 
425 
426 } // beast
427 
428 #endif
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:312
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:76
beast::severities::kTrace
@ kTrace
Definition: Journal.h:36
sstream
beast::Journal::ScopedStream::~ScopedStream
~ScopedStream()
Definition: beast_Journal.cpp:127
std::basic_stringbuf
beast::Journal::Stream::Stream
Stream(Stream const &other)
Construct or copy another Stream.
Definition: Journal.h:200
beast::Journal::Sink::operator=
Sink & operator=(Sink const &lhs)=delete
std::string
STL class.
beast::severities::kDisabled
@ kDisabled
Definition: Journal.h:43
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:287
beast::Journal::ScopedStream::m_ostream
std::ostringstream m_ostream
Definition: Journal.h:164
beast::Journal::ScopedStream::ScopedStream
ScopedStream(ScopedStream const &other)
Definition: Journal.h:134
std::is_nothrow_destructible
beast::Journal::Stream::level
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:213
beast::severities::kAll
@ kAll
Definition: Journal.h:34
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
std::is_default_constructible
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:67
std::is_move_constructible
beast::Journal::ScopedStream::m_level
const Severity m_level
Definition: Journal.h:163
beast::Journal::Journal
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:260
beast::Journal::Stream::sink
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:207
std::char_traits
beast::Journal::Stream::operator<<
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
Definition: beast_Journal.cpp:146
beast::Journal::Stream::Stream
Stream()
Create a stream which produces no output.
Definition: Journal.h:183
beast::detail::logstream_buf::write
void write(char const *s)
Definition: Journal.h:365
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:139
beast::Journal::sink
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:265
beast::Journal::active
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:280
beast::Journal::Sink::active
virtual bool active(Severity level) const
Returns true if text at the passed severity produces output.
Definition: beast_Journal.cpp:83
beast::Journal::stream
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:271
beast::Journal::Sink::Sink
Sink()=delete
beast::Journal::m_sink
Sink * m_sink
Definition: Journal.h:70
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:88
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:179
beast::Journal::error
Stream error() const
Definition: Journal.h:307
beast::Journal::info
Stream info() const
Definition: Journal.h:297
beast::basic_logstream
Definition: Journal.h:404
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
beast::basic_logstream::traits_type
Traits traits_type
Definition: Journal.h:408
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:220
beast::Journal::ScopedStream
Definition: Journal.h:131
beast::severities::kInfo
@ kInfo
Definition: Journal.h:38
beast::Journal::Sink::m_console
bool m_console
Definition: Journal.h:110
beast::Journal::Stream::m_level
Severity m_level
Definition: Journal.h:242
beast::basic_logstream::pos_type
traits_type::pos_type pos_type
Definition: Journal.h:410
beast::Journal::Stream::Stream
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:192
beast::severities::kNone
@ kNone
Definition: Journal.h:44
std::ostringstream
STL class.
beast::severities::kError
@ kError
Definition: Journal.h:40
std::is_copy_constructible
std::is_move_assignable
beast::basic_logstream::buf_
detail::logstream_buf< CharT, Traits > buf_
Definition: Journal.h:413
beast::basic_logstream::off_type
traits_type::off_type off_type
Definition: Journal.h:411
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:98
beast::Journal::Stream::m_sink
Sink & m_sink
Definition: Journal.h:241
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
beast::detail::logstream_buf
Definition: Journal.h:357
beast::Journal::ScopedStream::operator=
ScopedStream & operator=(ScopedStream const &)=delete
beast::detail::logstream_buf::write
void write(wchar_t const *s)
Definition: Journal.h:371
std
STL namespace.
beast::severities::kWarning
@ kWarning
Definition: Journal.h:39
cassert
beast::basic_logstream::basic_logstream
basic_logstream(beast::Journal::Stream const &strm)
Definition: Journal.h:416
beast::basic_logstream::int_type
traits_type::int_type int_type
Definition: Journal.h:409
beast::detail::logstream_buf::sync
int sync() override
Definition: Journal.h:390
beast::severities::kDebug
@ kDebug
Definition: Journal.h:37
beast::Journal::Stream::operator=
Stream & operator=(Stream const &other)=delete
beast::Journal::debug
Stream debug() const
Definition: Journal.h:292
beast::detail::logstream_buf::logstream_buf
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:379
std::is_copy_assignable
beast::detail::logstream_buf::strm_
beast::Journal::Stream strm_
Definition: Journal.h:360
beast::severities::kFatal
@ kFatal
Definition: Journal.h:41
beast::Journal::Sink::thresh_
Severity thresh_
Definition: Journal.h:109
beast::Journal::ScopedStream::m_sink
Sink & m_sink
Definition: Journal.h:162
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:407
beast::Journal::ScopedStream::ostream
std::ostringstream & ostream() const
Definition: Journal.h:150
beast::detail::logstream_buf::~logstream_buf
~logstream_buf()
Definition: Journal.h:384
beast
Definition: base_uint.h:582