xrpld
Loading...
Searching...
No Matches
Journal.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <cstdint>
6#include <sstream>
7
8namespace beast {
9
11enum class Severity : std::uint8_t {
12 All = 0,
13
15 Debug = 1,
16 Info = 2,
18 Error = 4,
19 Fatal = 5,
20
23};
24
38{
39public:
40 class Sink;
41
42private:
43 // Invariant: sink_ always points to a valid Sink
45
46public:
47 //--------------------------------------------------------------------------
48
50 class Sink
51 {
52 protected:
53 explicit Sink(Sink const& sink) = default;
54 Sink(Severity thresh, bool console);
55
56 public:
57 virtual ~Sink() = 0;
58
59 Sink() = delete;
60 Sink&
61 operator=(Sink const& lhs) = delete;
62
64 [[nodiscard]] virtual bool
65 active(Severity level) const;
66
69 [[nodiscard]] virtual bool
70 console() const;
71
74 virtual void
75 console(bool output);
76
78 [[nodiscard]] virtual Severity
79 threshold() const;
80
82 virtual void
83 threshold(Severity thresh);
84
89 virtual void
90 write(Severity level, std::string const& text) = 0;
91
99 virtual void
100 writeAlways(Severity level, std::string const& text) = 0;
101
102 private:
105 };
106
107#ifndef __INTELLISENSE__
108 static_assert(!std::is_default_constructible_v<Sink>, "");
109 static_assert(!std::is_copy_constructible_v<Sink>, "");
110 static_assert(!std::is_move_constructible_v<Sink>, "");
111 static_assert(!std::is_copy_assignable_v<Sink>, "");
112 static_assert(!std::is_move_assignable_v<Sink>, "");
113 static_assert(std::is_nothrow_destructible_v<Sink>, "");
114#endif
115
117 static Sink&
118 getNullSink();
119
120 //--------------------------------------------------------------------------
121
122 class Stream;
123
124 /* Scoped ostream-based container for writing messages to a Journal. */
126 {
127 public:
128 ScopedStream(ScopedStream const& other) : ScopedStream(other.sink_, other.level_)
129 {
130 }
131
133
134 template <typename T>
135 ScopedStream(Stream const& stream, T const& t);
136
138
140 operator=(ScopedStream const&) = delete;
141
143
145 ostream() const
146 {
147 return ostream_;
148 }
149
151 operator<<(std::ostream& manip(std::ostream&)) const;
152
153 template <typename T>
155 operator<<(T const& t) const;
156
157 private:
161 };
162
163#ifndef __INTELLISENSE__
167 static_assert(!std::is_copy_assignable_v<ScopedStream>, "");
168 static_assert(!std::is_move_assignable_v<ScopedStream>, "");
170#endif
171
172 //--------------------------------------------------------------------------
173public:
175 class Stream
176 {
177 public:
180 {
181 }
182
188 {
189 XRPL_ASSERT(
190 level_ < Severity::Disabled, "beast::Journal::Stream::Stream : maximum level");
191 }
192
194 Stream(Stream const& other) : Stream(other.sink_, other.level_)
195 {
196 }
197
198 Stream&
199 operator=(Stream const& other) = delete;
200
202 [[nodiscard]] Sink&
203 sink() const
204 {
205 return sink_;
206 }
207
209 [[nodiscard]] Severity
210 level() const
211 {
212 return level_;
213 }
214
217 [[nodiscard]] bool
218 active() const
219 {
220 return sink_.active(level_);
221 }
222
223 explicit
224 operator bool() const
225 {
226 return active();
227 }
228
229
233 operator<<(std::ostream& manip(std::ostream&)) const;
234
235 template <typename T>
237 operator<<(T const& t) const;
239
240 private:
243 };
244
245#ifndef __INTELLISENSE__
247 static_assert(std::is_copy_constructible_v<Stream>, "");
248 static_assert(std::is_move_constructible_v<Stream>, "");
249 static_assert(!std::is_copy_assignable_v<Stream>, "");
250 static_assert(!std::is_move_assignable_v<Stream>, "");
252#endif
253
254 //--------------------------------------------------------------------------
255
257 Journal() = delete;
258
260 explicit Journal(Sink& sink) : sink_(&sink)
261 {
262 }
263
265 [[nodiscard]] Sink&
266 sink() const
267 {
268 return *sink_;
269 }
270
272 [[nodiscard]] Stream
273 stream(Severity level) const
274 {
275 return Stream(*sink_, level);
276 }
277
282 [[nodiscard]] bool
283 active(Severity level) const
284 {
285 return sink_->active(level);
286 }
287
290 [[nodiscard]] Stream
291 trace() const
292 {
293 return {*sink_, Severity::Trace};
294 }
295
296 [[nodiscard]] Stream
297 debug() const
298 {
299 return {*sink_, Severity::Debug};
300 }
301
302 [[nodiscard]] Stream
303 info() const
304 {
305 return {*sink_, Severity::Info};
306 }
307
308 [[nodiscard]] Stream
309 warn() const
310 {
311 return {*sink_, Severity::Warning};
312 }
313
314 [[nodiscard]] Stream
315 error() const
316 {
317 return {*sink_, Severity::Error};
318 }
319
320 [[nodiscard]] Stream
321 fatal() const
322 {
323 return {*sink_, Severity::Fatal};
324 }
325
326};
327
328#ifndef __INTELLISENSE__
332static_assert(std::is_copy_assignable_v<Journal>, "");
333static_assert(std::is_move_assignable_v<Journal>, "");
335#endif
336
337//------------------------------------------------------------------------------
338
339template <typename T>
341 : ScopedStream(stream.sink(), stream.level())
342{
343 ostream_ << t;
344}
345
346template <typename T>
348Journal::ScopedStream::operator<<(T const& t) const
349{
350 ostream_ << t;
351 return ostream_;
352}
353
354//------------------------------------------------------------------------------
355
356template <typename T>
358Journal::Stream::operator<<(T const& t) const
359{
360 return ScopedStream(*this, t);
361}
362
363namespace detail {
364
365template <class CharT, class Traits = std::char_traits<CharT>>
366class LogStreamBuf : public std::basic_stringbuf<CharT, Traits>
367{
369
370 void
371 write(char const* s)
372 {
373 if (strm_)
374 strm_ << s;
375 }
376
377 void
378 write(wchar_t const* s)
379 {
380 if (strm_)
381 strm_ << s;
382 }
383
384public:
385 explicit LogStreamBuf(beast::Journal::Stream const& strm) : strm_(strm)
386 {
387 }
388
389 ~LogStreamBuf() override
390 {
391 sync();
392 }
393
394 int
395 sync() override
396 {
397 write(this->str().c_str());
398 this->str("");
399 return 0;
400 }
401
402 template <class T>
403 void
404 write(T const*) = delete;
405};
406
407} // namespace detail
408
409template <class CharT, class Traits = std::char_traits<CharT>>
410class BasicLogstream : public std::basic_ostream<CharT, Traits>
411{
412 using char_type = CharT;
413 using traits_type = Traits;
414 using int_type = traits_type::int_type;
415 using pos_type = traits_type::pos_type;
416 using off_type = traits_type::off_type;
417
419
420public:
422 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
423 {
424 }
425};
426
429
430} // namespace beast
detail::LogStreamBuf< char, std::char_traits< char > > buf_
Definition Journal.h:418
traits_type::off_type off_type
Definition Journal.h:416
traits_type::int_type int_type
Definition Journal.h:414
BasicLogstream(beast::Journal::Stream const &strm)
Definition Journal.h:421
traits_type::pos_type pos_type
Definition Journal.h:415
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
std::ostringstream & ostream() const
Definition Journal.h:145
std::ostringstream ostream_
Definition Journal.h:160
ScopedStream(ScopedStream const &other)
Definition Journal.h:128
Abstraction for the underlying message destination.
Definition Journal.h:51
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:176
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition Journal.h:203
bool active() const
Returns true if sink logs anything at this stream's level.
Definition Journal.h:218
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition Journal.h:187
Stream(Stream const &other)
Construct or copy another Stream.
Definition Journal.h:194
Stream()
Create a stream which produces no output.
Definition Journal.h:179
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition Journal.h:210
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:260
Stream fatal() const
Definition Journal.h:321
Stream error() const
Definition Journal.h:315
Stream debug() const
Definition Journal.h:297
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:266
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition Journal.h:283
Sink * sink_
Definition Journal.h:44
Stream info() const
Definition Journal.h:303
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition Journal.h:273
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:291
Stream warn() const
Definition Journal.h:309
void write(wchar_t const *s)
Definition Journal.h:378
beast::Journal::Stream strm_
Definition Journal.h:368
LogStreamBuf(beast::Journal::Stream const &strm)
Definition Journal.h:385
void write(char const *s)
Definition Journal.h:371
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:428
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:11
BasicLogstream< char > logstream
Definition Journal.h:427
STL namespace.