xrpld
Loading...
Searching...
No Matches
Journal.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <cstdint>
6#include <ostream>
7#include <sstream>
8#include <string>
9#include <type_traits>
10
11namespace beast {
12
16enum class Severity : std::uint8_t {
17 All = 0,
18
20 Debug = 1,
21 Info = 2,
23 Error = 4,
24 Fatal = 5,
25
28};
29
44{
45public:
46 class Sink;
47
48private:
49 // Invariant: sink_ always points to a valid Sink
51
52public:
53 //--------------------------------------------------------------------------
54
58 class Sink
59 {
60 protected:
61 explicit Sink(Sink const& sink) = default;
62 Sink(Severity thresh, bool console);
63
64 public:
65 virtual ~Sink() = 0;
66
67 Sink() = delete;
68 Sink&
69 operator=(Sink const& lhs) = delete;
70
74 [[nodiscard]] virtual bool
75 active(Severity level) const;
76
81 [[nodiscard]] virtual bool
82 console() const;
83
87 virtual void
88 console(bool output);
89
93 [[nodiscard]] virtual Severity
94 threshold() const;
95
99 virtual void
100 threshold(Severity thresh);
101
107 virtual void
108 write(Severity level, std::string const& text) = 0;
109
118 virtual void
119 writeAlways(Severity level, std::string const& text) = 0;
120
121 private:
124 };
125
126#ifndef __INTELLISENSE__
130 static_assert(!std::is_copy_assignable_v<Sink>);
131 static_assert(!std::is_move_assignable_v<Sink>);
133#endif
134
138 static Sink&
139 getNullSink();
140
141 //--------------------------------------------------------------------------
142
143 class Stream;
144
145 /* Scoped ostream-based container for writing messages to a Journal. */
147 {
148 public:
149 ScopedStream(ScopedStream const& other) : ScopedStream(other.sink_, other.level_)
150 {
151 }
152
154
155 template <typename T>
156 ScopedStream(Stream const& stream, T const& t);
157
159
161 operator=(ScopedStream const&) = delete;
162
164
166 ostream() const
167 {
168 return ostream_;
169 }
170
172 operator<<(std::ostream& manip(std::ostream&)) const;
173
174 template <typename T>
176 operator<<(T const& t) const;
177
178 private:
182 };
183
184#ifndef __INTELLISENSE__
191#endif
192
193 //--------------------------------------------------------------------------
194public:
198 class Stream
199 {
200 public:
205 {
206 }
207
214 {
215 XRPL_ASSERT(
216 level_ < Severity::Disabled, "beast::Journal::Stream::Stream : maximum level");
217 }
218
222 Stream(Stream const& other) : Stream(other.sink_, other.level_)
223 {
224 }
225
226 Stream&
227 operator=(Stream const& other) = delete;
228
232 [[nodiscard]] Sink&
233 sink() const
234 {
235 return sink_;
236 }
237
241 [[nodiscard]] Severity
242 level() const
243 {
244 return level_;
245 }
246
251 [[nodiscard]] bool
252 active() const
253 {
254 return sink_.active(level_);
255 }
256
257 explicit
258 operator bool() const
259 {
260 return active();
261 }
262
263
269 operator<<(std::ostream& manip(std::ostream&)) const;
270
271 template <typename T>
273 operator<<(T const& t) const;
275
276 private:
279 };
280
281#ifndef __INTELLISENSE__
285 static_assert(!std::is_copy_assignable_v<Stream>);
286 static_assert(!std::is_move_assignable_v<Stream>);
288#endif
289
290 //--------------------------------------------------------------------------
291
295 Journal() = delete;
296
300 explicit Journal(Sink& sink) : sink_(&sink)
301 {
302 }
303
307 [[nodiscard]] Sink&
308 sink() const
309 {
310 return *sink_;
311 }
312
316 [[nodiscard]] Stream
317 stream(Severity level) const
318 {
319 return Stream(*sink_, level);
320 }
321
327 [[nodiscard]] bool
328 active(Severity level) const
329 {
330 return sink_->active(level);
331 }
332
337 [[nodiscard]] Stream
338 trace() const
339 {
340 return {*sink_, Severity::Trace};
341 }
342
343 [[nodiscard]] Stream
344 debug() const
345 {
346 return {*sink_, Severity::Debug};
347 }
348
349 [[nodiscard]] Stream
350 info() const
351 {
352 return {*sink_, Severity::Info};
353 }
354
355 [[nodiscard]] Stream
356 warn() const
357 {
358 return {*sink_, Severity::Warning};
359 }
360
361 [[nodiscard]] Stream
362 error() const
363 {
364 return {*sink_, Severity::Error};
365 }
366
367 [[nodiscard]] Stream
368 fatal() const
369 {
370 return {*sink_, Severity::Fatal};
371 }
372
373};
374
375#ifndef __INTELLISENSE__
382#endif
383
384//------------------------------------------------------------------------------
385
386template <typename T>
388 : ScopedStream(stream.sink(), stream.level())
389{
390 ostream_ << t;
391}
392
393template <typename T>
395Journal::ScopedStream::operator<<(T const& t) const
396{
397 ostream_ << t;
398 return ostream_;
399}
400
401//------------------------------------------------------------------------------
402
403template <typename T>
405Journal::Stream::operator<<(T const& t) const
406{
407 return ScopedStream(*this, t);
408}
409
410namespace detail {
411
412template <class CharT, class Traits = std::char_traits<CharT>>
413class LogStreamBuf : public std::basic_stringbuf<CharT, Traits>
414{
416
417 void
418 write(char const* s)
419 {
420 if (strm_)
421 strm_ << s;
422 }
423
424 void
425 write(wchar_t const* s)
426 {
427 if (strm_)
428 strm_ << s;
429 }
430
431public:
432 explicit LogStreamBuf(beast::Journal::Stream const& strm) : strm_(strm)
433 {
434 }
435
436 ~LogStreamBuf() override
437 {
438 sync();
439 }
440
441 int
442 sync() override
443 {
444 write(this->str().c_str());
445 this->str("");
446 return 0;
447 }
448
449 template <class T>
450 void
451 write(T const*) = delete;
452};
453
454} // namespace detail
455
456template <class CharT, class Traits = std::char_traits<CharT>>
457class BasicLogstream : public std::basic_ostream<CharT, Traits>
458{
459 using char_type = CharT;
460 using traits_type = Traits;
461 using int_type = traits_type::int_type;
462 using pos_type = traits_type::pos_type;
463 using off_type = traits_type::off_type;
464
466
467public:
469 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
470 {
471 }
472};
473
476
477} // namespace beast
detail::LogStreamBuf< char, std::char_traits< char > > buf_
Definition Journal.h:465
traits_type::off_type off_type
Definition Journal.h:463
traits_type::int_type int_type
Definition Journal.h:461
BasicLogstream(beast::Journal::Stream const &strm)
Definition Journal.h:468
traits_type::pos_type pos_type
Definition Journal.h:462
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
std::ostringstream & ostream() const
Definition Journal.h:166
std::ostringstream ostream_
Definition Journal.h:181
ScopedStream(ScopedStream const &other)
Definition Journal.h:149
Abstraction for the underlying message destination.
Definition Journal.h:59
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
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 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:199
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:252
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition Journal.h:213
Stream(Stream const &other)
Construct or copy another Stream.
Definition Journal.h:222
Stream()
Create a stream which produces no output.
Definition Journal.h:204
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition Journal.h:242
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition Journal.h:300
Stream fatal() const
Definition Journal.h:368
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:308
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition Journal.h:328
Sink * sink_
Definition Journal.h:50
Stream info() const
Definition Journal.h:350
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition Journal.h:317
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:338
Stream warn() const
Definition Journal.h:356
void write(wchar_t const *s)
Definition Journal.h:425
beast::Journal::Stream strm_
Definition Journal.h:415
LogStreamBuf(beast::Journal::Stream const &strm)
Definition Journal.h:432
void write(char const *s)
Definition Journal.h:418
void write(T const *)=delete
T is_copy_assignable_v
T is_copy_constructible_v
T is_default_constructible_v
T is_nothrow_destructible_v
T is_move_assignable_v
T is_move_constructible_v
BasicLogstream< wchar_t > logwstream
Definition Journal.h:475
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:16
BasicLogstream< char > logstream
Definition Journal.h:474
STL namespace.