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
25#include <sstream>
26
27namespace beast {
28
30namespace severities {
33 kAll = 0,
34
41
44};
45} // namespace severities
46
60{
61public:
62 class Sink;
63
64private:
65 // Severity level / threshold of a Journal message.
67
68 // Invariant: m_sink always points to a valid Sink
70
71public:
72 //--------------------------------------------------------------------------
73
75 class Sink
76 {
77 protected:
78 Sink() = delete;
79 explicit Sink(Sink const& sink) = default;
80 Sink(Severity thresh, bool console);
81 Sink&
82 operator=(Sink const& lhs) = delete;
83
84 public:
85 virtual ~Sink() = 0;
86
88 virtual bool
89 active(Severity level) const;
90
93 virtual bool
94 console() const;
95
98 virtual void
99 console(bool output);
100
102 virtual Severity
103 threshold() const;
104
106 virtual void
107 threshold(Severity thresh);
108
113 virtual void
114 write(Severity level, std::string const& text) = 0;
115
123 virtual void
124 writeAlways(Severity level, std::string const& text) = 0;
125
126 private:
129 };
130
131#ifndef __INTELLISENSE__
132 static_assert(std::is_default_constructible<Sink>::value == false, "");
133 static_assert(std::is_copy_constructible<Sink>::value == false, "");
134 static_assert(std::is_move_constructible<Sink>::value == false, "");
135 static_assert(std::is_copy_assignable<Sink>::value == false, "");
136 static_assert(std::is_move_assignable<Sink>::value == false, "");
137 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
138#endif
139
141 static Sink&
142 getNullSink();
143
144 //--------------------------------------------------------------------------
145
146 class Stream;
147
148 /* Scoped ostream-based container for writing messages to a Journal. */
150 {
151 public:
153 : ScopedStream(other.m_sink, other.m_level)
154 {
155 }
156
158
159 template <typename T>
160 ScopedStream(Stream const& stream, T const& t);
161
163
165 operator=(ScopedStream const&) = delete;
166
168
170 ostream() const
171 {
172 return m_ostream;
173 }
174
176 operator<<(std::ostream& manip(std::ostream&)) const;
177
178 template <typename T>
180 operator<<(T const& t) const;
181
182 private:
186 };
187
188#ifndef __INTELLISENSE__
189 static_assert(
191 "");
192 static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
193 static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
194 static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
195 static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
196 static_assert(
198 "");
199#endif
200
201 //--------------------------------------------------------------------------
202public:
204 class Stream
205 {
206 public:
208 explicit Stream()
209 : m_sink(getNullSink()), m_level(severities::kDisabled)
210 {
211 }
212
218 {
219 XRPL_ASSERT(
221 "beast::Journal::Stream::Stream : maximum level");
222 }
223
225 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
226 {
227 }
228
229 Stream&
230 operator=(Stream const& other) = delete;
231
233 Sink&
234 sink() const
235 {
236 return m_sink;
237 }
238
241 level() const
242 {
243 return m_level;
244 }
245
248 bool
249 active() const
250 {
251 return m_sink.active(m_level);
252 }
253
254 explicit
255 operator bool() const
256 {
257 return active();
258 }
264 operator<<(std::ostream& manip(std::ostream&)) const;
265
266 template <typename T>
268 operator<<(T const& t) const;
271 private:
274 };
275
276#ifndef __INTELLISENSE__
277 static_assert(std::is_default_constructible<Stream>::value == true, "");
278 static_assert(std::is_copy_constructible<Stream>::value == true, "");
279 static_assert(std::is_move_constructible<Stream>::value == true, "");
280 static_assert(std::is_copy_assignable<Stream>::value == false, "");
281 static_assert(std::is_move_assignable<Stream>::value == false, "");
282 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
283#endif
284
285 //--------------------------------------------------------------------------
286
288 Journal() = delete;
289
291 explicit Journal(Sink& sink) : m_sink(&sink)
292 {
293 }
294
296 Sink&
297 sink() const
298 {
299 return *m_sink;
300 }
301
303 Stream
304 stream(Severity level) const
305 {
306 return Stream(*m_sink, level);
307 }
308
313 bool
314 active(Severity level) const
315 {
316 return m_sink->active(level);
317 }
318
321 Stream
322 trace() const
323 {
324 return {*m_sink, severities::kTrace};
325 }
326
327 Stream
328 debug() const
329 {
330 return {*m_sink, severities::kDebug};
331 }
332
333 Stream
334 info() const
335 {
336 return {*m_sink, severities::kInfo};
337 }
338
339 Stream
340 warn() const
341 {
342 return {*m_sink, severities::kWarning};
343 }
344
345 Stream
346 error() const
347 {
348 return {*m_sink, severities::kError};
349 }
350
351 Stream
352 fatal() const
353 {
354 return {*m_sink, severities::kFatal};
355 }
357};
358
359#ifndef __INTELLISENSE__
360static_assert(std::is_default_constructible<Journal>::value == false, "");
361static_assert(std::is_copy_constructible<Journal>::value == true, "");
362static_assert(std::is_move_constructible<Journal>::value == true, "");
363static_assert(std::is_copy_assignable<Journal>::value == true, "");
364static_assert(std::is_move_assignable<Journal>::value == true, "");
365static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
366#endif
367
368//------------------------------------------------------------------------------
369
370template <typename T>
372 : ScopedStream(stream.sink(), stream.level())
373{
374 m_ostream << t;
375}
376
377template <typename T>
379Journal::ScopedStream::operator<<(T const& t) const
380{
381 m_ostream << t;
382 return m_ostream;
383}
384
385//------------------------------------------------------------------------------
386
387template <typename T>
389Journal::Stream::operator<<(T const& t) const
390{
391 return ScopedStream(*this, t);
392}
393
394namespace detail {
395
396template <class CharT, class Traits = std::char_traits<CharT>>
397class logstream_buf : public std::basic_stringbuf<CharT, Traits>
398{
400
401 template <class T>
402 void
403 write(T const*) = delete;
404
405 void
406 write(char const* s)
407 {
408 if (strm_)
409 strm_ << s;
410 }
411
412 void
413 write(wchar_t const* s)
414 {
415 if (strm_)
416 strm_ << s;
417 }
418
419public:
420 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
421 {
422 }
423
425 {
426 sync();
427 }
428
429 int
430 sync() override
431 {
432 write(this->str().c_str());
433 this->str("");
434 return 0;
435 }
436};
437
438} // namespace detail
439
440template <class CharT, class Traits = std::char_traits<CharT>>
441class basic_logstream : public std::basic_ostream<CharT, Traits>
442{
443 typedef CharT char_type;
444 typedef Traits traits_type;
445 typedef typename traits_type::int_type int_type;
446 typedef typename traits_type::pos_type pos_type;
447 typedef typename traits_type::off_type off_type;
448
450
451public:
453 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
454 {
455 }
456};
457
460
461} // namespace beast
462
463#endif
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
Severity const m_level
Definition: Journal.h:184
std::ostringstream & ostream() const
Definition: Journal.h:170
std::ostringstream m_ostream
Definition: Journal.h:185
ScopedStream(ScopedStream const &other)
Definition: Journal.h:152
Abstraction for the underlying message destination.
Definition: Journal.h:76
Severity thresh_
Definition: Journal.h:127
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:205
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:234
bool active() const
Returns true if sink logs anything at this stream's level.
Definition: Journal.h:249
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:217
Stream(Stream const &other)
Construct or copy another Stream.
Definition: Journal.h:225
Stream()
Create a stream which produces no output.
Definition: Journal.h:208
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:241
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
A generic endpoint for log messages.
Definition: Journal.h:60
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:291
Stream fatal() const
Definition: Journal.h:352
Stream error() const
Definition: Journal.h:346
Stream debug() const
Definition: Journal.h:328
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:297
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:314
Stream info() const
Definition: Journal.h:334
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:304
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:322
Stream warn() const
Definition: Journal.h:340
Sink * m_sink
Definition: Journal.h:69
traits_type::off_type off_type
Definition: Journal.h:447
traits_type::int_type int_type
Definition: Journal.h:445
detail::logstream_buf< CharT, Traits > buf_
Definition: Journal.h:449
traits_type::pos_type pos_type
Definition: Journal.h:446
basic_logstream(beast::Journal::Stream const &strm)
Definition: Journal.h:452
void write(T const *)=delete
void write(char const *s)
Definition: Journal.h:406
beast::Journal::Stream strm_
Definition: Journal.h:399
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:420
void write(wchar_t const *s)
Definition: Journal.h:413
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
STL namespace.