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(
194 m_level < severities::kDisabled, "beast::Journal::Stream::Stream : maximum level");
195 }
196
198 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
199 {
200 }
201
202 Stream&
203 operator=(Stream const& other) = delete;
204
206 Sink&
207 sink() const
208 {
209 return m_sink;
210 }
211
214 level() const
215 {
216 return m_level;
217 }
218
221 bool
222 active() const
223 {
224 return m_sink.active(m_level);
225 }
226
227 explicit
228 operator bool() const
229 {
230 return active();
231 }
237 operator<<(std::ostream& manip(std::ostream&)) const;
238
239 template <typename T>
241 operator<<(T const& t) const;
244 private:
247 };
248
249#ifndef __INTELLISENSE__
250 static_assert(std::is_default_constructible<Stream>::value == true, "");
251 static_assert(std::is_copy_constructible<Stream>::value == true, "");
252 static_assert(std::is_move_constructible<Stream>::value == true, "");
253 static_assert(std::is_copy_assignable<Stream>::value == false, "");
254 static_assert(std::is_move_assignable<Stream>::value == false, "");
255 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
256#endif
257
258 //--------------------------------------------------------------------------
259
261 Journal() = delete;
262
264 explicit Journal(Sink& sink) : m_sink(&sink)
265 {
266 }
267
269 Sink&
270 sink() const
271 {
272 return *m_sink;
273 }
274
276 Stream
277 stream(Severity level) const
278 {
279 return Stream(*m_sink, level);
280 }
281
286 bool
287 active(Severity level) const
288 {
289 return m_sink->active(level);
290 }
291
294 Stream
295 trace() const
296 {
297 return {*m_sink, severities::kTrace};
298 }
299
300 Stream
301 debug() const
302 {
303 return {*m_sink, severities::kDebug};
304 }
305
306 Stream
307 info() const
308 {
309 return {*m_sink, severities::kInfo};
310 }
311
312 Stream
313 warn() const
314 {
315 return {*m_sink, severities::kWarning};
316 }
317
318 Stream
319 error() const
320 {
321 return {*m_sink, severities::kError};
322 }
323
324 Stream
325 fatal() const
326 {
327 return {*m_sink, severities::kFatal};
328 }
330};
331
332#ifndef __INTELLISENSE__
333static_assert(std::is_default_constructible<Journal>::value == false, "");
334static_assert(std::is_copy_constructible<Journal>::value == true, "");
335static_assert(std::is_move_constructible<Journal>::value == true, "");
336static_assert(std::is_copy_assignable<Journal>::value == true, "");
337static_assert(std::is_move_assignable<Journal>::value == true, "");
338static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
339#endif
340
341//------------------------------------------------------------------------------
342
343template <typename T>
345 : ScopedStream(stream.sink(), stream.level())
346{
347 m_ostream << t;
348}
349
350template <typename T>
352Journal::ScopedStream::operator<<(T const& t) const
353{
354 m_ostream << t;
355 return m_ostream;
356}
357
358//------------------------------------------------------------------------------
359
360template <typename T>
362Journal::Stream::operator<<(T const& t) const
363{
364 return ScopedStream(*this, t);
365}
366
367namespace detail {
368
369template <class CharT, class Traits = std::char_traits<CharT>>
370class logstream_buf : public std::basic_stringbuf<CharT, Traits>
371{
373
374 template <class T>
375 void
376 write(T const*) = delete;
377
378 void
379 write(char const* s)
380 {
381 if (strm_)
382 strm_ << s;
383 }
384
385 void
386 write(wchar_t const* s)
387 {
388 if (strm_)
389 strm_ << s;
390 }
391
392public:
393 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
394 {
395 }
396
398 {
399 sync();
400 }
401
402 int
403 sync() override
404 {
405 write(this->str().c_str());
406 this->str("");
407 return 0;
408 }
409};
410
411} // namespace detail
412
413template <class CharT, class Traits = std::char_traits<CharT>>
414class basic_logstream : public std::basic_ostream<CharT, Traits>
415{
416 typedef CharT char_type;
417 typedef Traits traits_type;
418 typedef typename traits_type::int_type int_type;
419 typedef typename traits_type::pos_type pos_type;
420 typedef typename traits_type::off_type off_type;
421
423
424public:
426 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
427 {
428 }
429};
430
433
434} // 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:207
bool active() const
Returns true if sink logs anything at this stream's level.
Definition Journal.h:222
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:198
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:214
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:264
Stream fatal() const
Definition Journal.h:325
Stream error() const
Definition Journal.h:319
Stream debug() const
Definition Journal.h:301
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:270
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition Journal.h:287
Stream info() const
Definition Journal.h:307
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition Journal.h:277
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:295
Stream warn() const
Definition Journal.h:313
Sink * m_sink
Definition Journal.h:49
traits_type::off_type off_type
Definition Journal.h:420
traits_type::int_type int_type
Definition Journal.h:418
detail::logstream_buf< CharT, Traits > buf_
Definition Journal.h:422
traits_type::pos_type pos_type
Definition Journal.h:419
basic_logstream(beast::Journal::Stream const &strm)
Definition Journal.h:425
void write(T const *)=delete
void write(char const *s)
Definition Journal.h:379
beast::Journal::Stream strm_
Definition Journal.h:372
logstream_buf(beast::Journal::Stream const &strm)
Definition Journal.h:393
void write(wchar_t const *s)
Definition Journal.h:386
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
STL namespace.