rippled
Loading...
Searching...
No Matches
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 <xrpl/beast/utility/instrumentation.h>
24#include <sstream>
25
26namespace beast {
27
29namespace severities {
32 kAll = 0,
33
40
43};
44} // namespace severities
45
59{
60public:
61 class Sink;
62
63private:
64 // Severity level / threshold of a Journal message.
66
67 // Invariant: m_sink always points to a valid Sink
69
70public:
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
122 virtual void
123 writeAlways(Severity level, std::string const& text) = 0;
124
125 private:
128 };
129
130#ifndef __INTELLISENSE__
131 static_assert(std::is_default_constructible<Sink>::value == false, "");
132 static_assert(std::is_copy_constructible<Sink>::value == false, "");
133 static_assert(std::is_move_constructible<Sink>::value == false, "");
134 static_assert(std::is_copy_assignable<Sink>::value == false, "");
135 static_assert(std::is_move_assignable<Sink>::value == false, "");
136 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
137#endif
138
140 static Sink&
141 getNullSink();
142
143 //--------------------------------------------------------------------------
144
145 class Stream;
146
147 /* Scoped ostream-based container for writing messages to a Journal. */
149 {
150 public:
152 : ScopedStream(other.m_sink, other.m_level)
153 {
154 }
155
157
158 template <typename T>
159 ScopedStream(Stream const& stream, T const& t);
160
162
164 operator=(ScopedStream const&) = delete;
165
167
169 ostream() const
170 {
171 return m_ostream;
172 }
173
175 operator<<(std::ostream& manip(std::ostream&)) const;
176
177 template <typename T>
179 operator<<(T const& t) const;
180
181 private:
185 };
186
187#ifndef __INTELLISENSE__
188 static_assert(
190 "");
191 static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
192 static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
193 static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
194 static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
195 static_assert(
197 "");
198#endif
199
200 //--------------------------------------------------------------------------
201public:
203 class Stream
204 {
205 public:
207 explicit Stream()
208 : m_sink(getNullSink()), m_level(severities::kDisabled)
209 {
210 }
211
217 {
218 XRPL_ASSERT(
220 "beast::Journal::Stream::Stream : maximum level");
221 }
222
224 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
225 {
226 }
227
228 Stream&
229 operator=(Stream const& other) = delete;
230
232 Sink&
233 sink() const
234 {
235 return m_sink;
236 }
237
240 level() const
241 {
242 return m_level;
243 }
244
247 bool
248 active() const
249 {
250 return m_sink.active(m_level);
251 }
252
253 explicit
254 operator bool() const
255 {
256 return active();
257 }
263 operator<<(std::ostream& manip(std::ostream&)) const;
264
265 template <typename T>
267 operator<<(T const& t) const;
270 private:
273 };
274
275#ifndef __INTELLISENSE__
276 static_assert(std::is_default_constructible<Stream>::value == true, "");
277 static_assert(std::is_copy_constructible<Stream>::value == true, "");
278 static_assert(std::is_move_constructible<Stream>::value == true, "");
279 static_assert(std::is_copy_assignable<Stream>::value == false, "");
280 static_assert(std::is_move_assignable<Stream>::value == false, "");
281 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
282#endif
283
284 //--------------------------------------------------------------------------
285
287 Journal() = delete;
288
290 explicit Journal(Sink& sink) : m_sink(&sink)
291 {
292 }
293
295 Sink&
296 sink() const
297 {
298 return *m_sink;
299 }
300
302 Stream
303 stream(Severity level) const
304 {
305 return Stream(*m_sink, level);
306 }
307
312 bool
313 active(Severity level) const
314 {
315 return m_sink->active(level);
316 }
317
320 Stream
321 trace() const
322 {
323 return {*m_sink, severities::kTrace};
324 }
325
326 Stream
327 debug() const
328 {
329 return {*m_sink, severities::kDebug};
330 }
331
332 Stream
333 info() const
334 {
335 return {*m_sink, severities::kInfo};
336 }
337
338 Stream
339 warn() const
340 {
341 return {*m_sink, severities::kWarning};
342 }
343
344 Stream
345 error() const
346 {
347 return {*m_sink, severities::kError};
348 }
349
350 Stream
351 fatal() const
352 {
353 return {*m_sink, severities::kFatal};
354 }
356};
357
358#ifndef __INTELLISENSE__
359static_assert(std::is_default_constructible<Journal>::value == false, "");
360static_assert(std::is_copy_constructible<Journal>::value == true, "");
361static_assert(std::is_move_constructible<Journal>::value == true, "");
362static_assert(std::is_copy_assignable<Journal>::value == true, "");
363static_assert(std::is_move_assignable<Journal>::value == true, "");
364static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
365#endif
366
367//------------------------------------------------------------------------------
368
369template <typename T>
371 : ScopedStream(stream.sink(), stream.level())
372{
373 m_ostream << t;
374}
375
376template <typename T>
378Journal::ScopedStream::operator<<(T const& t) const
379{
380 m_ostream << t;
381 return m_ostream;
382}
383
384//------------------------------------------------------------------------------
385
386template <typename T>
388Journal::Stream::operator<<(T const& t) const
389{
390 return ScopedStream(*this, t);
391}
392
393namespace detail {
394
395template <class CharT, class Traits = std::char_traits<CharT>>
396class logstream_buf : public std::basic_stringbuf<CharT, Traits>
397{
399
400 template <class T>
401 void
402 write(T const*) = delete;
403
404 void
405 write(char const* s)
406 {
407 if (strm_)
408 strm_ << s;
409 }
410
411 void
412 write(wchar_t const* s)
413 {
414 if (strm_)
415 strm_ << s;
416 }
417
418public:
419 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
420 {
421 }
422
424 {
425 sync();
426 }
427
428 int
429 sync() override
430 {
431 write(this->str().c_str());
432 this->str("");
433 return 0;
434 }
435};
436
437} // namespace detail
438
439template <class CharT, class Traits = std::char_traits<CharT>>
440class basic_logstream : public std::basic_ostream<CharT, Traits>
441{
442 typedef CharT char_type;
443 typedef Traits traits_type;
444 typedef typename traits_type::int_type int_type;
445 typedef typename traits_type::pos_type pos_type;
446 typedef typename traits_type::off_type off_type;
447
449
450public:
452 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
453 {
454 }
455};
456
459
460} // namespace beast
461
462#endif
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
Severity const m_level
Definition: Journal.h:183
std::ostringstream & ostream() const
Definition: Journal.h:169
std::ostringstream m_ostream
Definition: Journal.h:184
ScopedStream(ScopedStream const &other)
Definition: Journal.h:151
Abstraction for the underlying message destination.
Definition: Journal.h:75
Severity thresh_
Definition: Journal.h:126
Sink(Sink const &sink)=default
Sink & operator=(Sink const &lhs)=delete
virtual bool active(Severity level) const
Returns true if text at the passed severity produces output.
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
virtual bool console() const
Returns true if a message is also written to the Output Window (MSVC).
virtual void write(Severity level, std::string const &text)=0
Write text to the sink at the specified severity.
virtual void writeAlways(Severity level, std::string const &text)=0
Bypass filter and write text to the sink at the specified severity.
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:204
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:233
bool active() const
Returns true if sink logs anything at this stream's level.
Definition: Journal.h:248
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:216
Stream(Stream const &other)
Construct or copy another Stream.
Definition: Journal.h:224
Stream()
Create a stream which produces no output.
Definition: Journal.h:207
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:240
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
A generic endpoint for log messages.
Definition: Journal.h:59
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:290
Stream fatal() const
Definition: Journal.h:351
Stream error() const
Definition: Journal.h:345
Stream debug() const
Definition: Journal.h:327
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:296
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:313
Stream info() const
Definition: Journal.h:333
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:303
static Sink & getNullSink()
Returns a Sink which does nothing.
Journal()=delete
Journal has no default constructor.
Stream trace() const
Severity stream access functions.
Definition: Journal.h:321
Stream warn() const
Definition: Journal.h:339
Sink * m_sink
Definition: Journal.h:68
traits_type::off_type off_type
Definition: Journal.h:446
traits_type::int_type int_type
Definition: Journal.h:444
detail::logstream_buf< CharT, Traits > buf_
Definition: Journal.h:448
traits_type::pos_type pos_type
Definition: Journal.h:445
basic_logstream(beast::Journal::Stream const &strm)
Definition: Journal.h:451
void write(T const *)=delete
void write(char const *s)
Definition: Journal.h:405
beast::Journal::Stream strm_
Definition: Journal.h:398
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:419
void write(wchar_t const *s)
Definition: Journal.h:412
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
STL namespace.