rippled
Loading...
Searching...
No Matches
Journal.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <sstream>
6
7namespace beast {
8
10namespace severities {
25} // namespace severities
26
40{
41public:
42 class Sink;
43
44private:
45 // Severity level / threshold of a Journal message.
47
48 // Invariant: m_sink always points to a valid Sink
50
51public:
52 //--------------------------------------------------------------------------
53
55 class Sink
56 {
57 protected:
58 Sink() = delete;
59 explicit Sink(Sink const& sink) = default;
60 Sink(Severity thresh, bool console);
61 Sink&
62 operator=(Sink const& lhs) = delete;
63
64 public:
65 virtual ~Sink() = 0;
66
68 virtual bool
69 active(Severity level) const;
70
73 virtual bool
74 console() const;
75
78 virtual void
79 console(bool output);
80
82 virtual Severity
83 threshold() const;
84
86 virtual void
87 threshold(Severity thresh);
88
93 virtual void
94 write(Severity level, std::string const& text) = 0;
95
103 virtual void
104 writeAlways(Severity level, std::string const& text) = 0;
105
106 private:
109 };
110
111#ifndef __INTELLISENSE__
112 static_assert(std::is_default_constructible<Sink>::value == false, "");
113 static_assert(std::is_copy_constructible<Sink>::value == false, "");
114 static_assert(std::is_move_constructible<Sink>::value == false, "");
115 static_assert(std::is_copy_assignable<Sink>::value == false, "");
116 static_assert(std::is_move_assignable<Sink>::value == false, "");
117 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
118#endif
119
121 static Sink&
122 getNullSink();
123
124 //--------------------------------------------------------------------------
125
126 class Stream;
127
128 /* Scoped ostream-based container for writing messages to a Journal. */
130 {
131 public:
133 {
134 }
135
137
138 template <typename T>
139 ScopedStream(Stream const& stream, T const& t);
140
142
144 operator=(ScopedStream const&) = delete;
145
147
149 ostream() const
150 {
151 return m_ostream;
152 }
153
155 operator<<(std::ostream& manip(std::ostream&)) const;
156
157 template <typename T>
159 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 //--------------------------------------------------------------------------
177public:
179 class Stream
180 {
181 public:
183 explicit Stream() : m_sink(getNullSink()), m_level(severities::kDisabled)
184 {
185 }
186
192 {
193 XRPL_ASSERT(m_level < severities::kDisabled, "beast::Journal::Stream::Stream : maximum level");
194 }
195
197 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
198 {
199 }
200
201 Stream&
202 operator=(Stream const& other) = delete;
203
205 Sink&
206 sink() const
207 {
208 return m_sink;
209 }
210
213 level() const
214 {
215 return m_level;
216 }
217
220 bool
221 active() const
222 {
223 return m_sink.active(m_level);
224 }
225
226 explicit
227 operator bool() const
228 {
229 return active();
230 }
236 operator<<(std::ostream& manip(std::ostream&)) const;
237
238 template <typename T>
240 operator<<(T const& t) const;
243 private:
246 };
247
248#ifndef __INTELLISENSE__
249 static_assert(std::is_default_constructible<Stream>::value == true, "");
250 static_assert(std::is_copy_constructible<Stream>::value == true, "");
251 static_assert(std::is_move_constructible<Stream>::value == true, "");
252 static_assert(std::is_copy_assignable<Stream>::value == false, "");
253 static_assert(std::is_move_assignable<Stream>::value == false, "");
254 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
255#endif
256
257 //--------------------------------------------------------------------------
258
260 Journal() = delete;
261
263 explicit Journal(Sink& sink) : m_sink(&sink)
264 {
265 }
266
268 Sink&
269 sink() const
270 {
271 return *m_sink;
272 }
273
275 Stream
276 stream(Severity level) const
277 {
278 return Stream(*m_sink, level);
279 }
280
285 bool
286 active(Severity level) const
287 {
288 return m_sink->active(level);
289 }
290
293 Stream
294 trace() const
295 {
296 return {*m_sink, severities::kTrace};
297 }
298
299 Stream
300 debug() const
301 {
302 return {*m_sink, severities::kDebug};
303 }
304
305 Stream
306 info() const
307 {
308 return {*m_sink, severities::kInfo};
309 }
310
311 Stream
312 warn() const
313 {
314 return {*m_sink, severities::kWarning};
315 }
316
317 Stream
318 error() const
319 {
320 return {*m_sink, severities::kError};
321 }
322
323 Stream
324 fatal() const
325 {
326 return {*m_sink, severities::kFatal};
327 }
329};
330
331#ifndef __INTELLISENSE__
332static_assert(std::is_default_constructible<Journal>::value == false, "");
333static_assert(std::is_copy_constructible<Journal>::value == true, "");
334static_assert(std::is_move_constructible<Journal>::value == true, "");
335static_assert(std::is_copy_assignable<Journal>::value == true, "");
336static_assert(std::is_move_assignable<Journal>::value == true, "");
337static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
338#endif
339
340//------------------------------------------------------------------------------
341
342template <typename T>
344 : ScopedStream(stream.sink(), stream.level())
345{
346 m_ostream << t;
347}
348
349template <typename T>
351Journal::ScopedStream::operator<<(T const& t) const
352{
353 m_ostream << t;
354 return m_ostream;
355}
356
357//------------------------------------------------------------------------------
358
359template <typename T>
361Journal::Stream::operator<<(T const& t) const
362{
363 return ScopedStream(*this, t);
364}
365
366namespace detail {
367
368template <class CharT, class Traits = std::char_traits<CharT>>
369class logstream_buf : public std::basic_stringbuf<CharT, Traits>
370{
372
373 template <class T>
374 void
375 write(T const*) = delete;
376
377 void
378 write(char const* s)
379 {
380 if (strm_)
381 strm_ << s;
382 }
383
384 void
385 write(wchar_t const* s)
386 {
387 if (strm_)
388 strm_ << s;
389 }
390
391public:
392 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
393 {
394 }
395
397 {
398 sync();
399 }
400
401 int
402 sync() override
403 {
404 write(this->str().c_str());
405 this->str("");
406 return 0;
407 }
408};
409
410} // namespace detail
411
412template <class CharT, class Traits = std::char_traits<CharT>>
413class basic_logstream : public std::basic_ostream<CharT, Traits>
414{
415 typedef CharT char_type;
416 typedef Traits traits_type;
417 typedef typename traits_type::int_type int_type;
418 typedef typename traits_type::pos_type pos_type;
419 typedef typename traits_type::off_type off_type;
420
422
423public:
424 explicit basic_logstream(beast::Journal::Stream const& strm) : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
425 {
426 }
427};
428
431
432} // namespace beast
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
std::ostringstream & ostream() const
Definition Journal.h:149
std::ostringstream m_ostream
Definition Journal.h:164
ScopedStream(ScopedStream const &other)
Definition Journal.h:132
Abstraction for the underlying message destination.
Definition Journal.h:56
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:180
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition Journal.h:206
bool active() const
Returns true if sink logs anything at this stream's level.
Definition Journal.h:221
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition Journal.h:191
Stream(Stream const &other)
Construct or copy another Stream.
Definition Journal.h:197
Stream()
Create a stream which produces no output.
Definition Journal.h:183
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition Journal.h:213
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
A generic endpoint for log messages.
Definition Journal.h:40
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition Journal.h:263
Stream fatal() const
Definition Journal.h:324
Stream error() const
Definition Journal.h:318
Stream debug() const
Definition Journal.h:300
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:269
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition Journal.h:286
Stream info() const
Definition Journal.h:306
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition Journal.h:276
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:294
Stream warn() const
Definition Journal.h:312
Sink * m_sink
Definition Journal.h:49
traits_type::off_type off_type
Definition Journal.h:419
traits_type::int_type int_type
Definition Journal.h:417
detail::logstream_buf< CharT, Traits > buf_
Definition Journal.h:421
traits_type::pos_type pos_type
Definition Journal.h:418
basic_logstream(beast::Journal::Stream const &strm)
Definition Journal.h:424
void write(T const *)=delete
void write(char const *s)
Definition Journal.h:378
beast::Journal::Stream strm_
Definition Journal.h:371
logstream_buf(beast::Journal::Stream const &strm)
Definition Journal.h:392
void write(wchar_t const *s)
Definition Journal.h:385
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
STL namespace.