rippled
Loading...
Searching...
No Matches
Journal.h
1#ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED
2#define BEAST_UTILITY_JOURNAL_H_INCLUDED
3
4#include <xrpl/beast/utility/instrumentation.h>
5
6#include <sstream>
7
8namespace beast {
9
11namespace severities {
26} // namespace severities
27
41{
42public:
43 class Sink;
44
45private:
46 // Severity level / threshold of a Journal message.
48
49 // Invariant: m_sink always points to a valid Sink
51
52public:
53 //--------------------------------------------------------------------------
54
56 class Sink
57 {
58 protected:
59 Sink() = delete;
60 explicit Sink(Sink const& sink) = default;
61 Sink(Severity thresh, bool console);
62 Sink&
63 operator=(Sink const& lhs) = delete;
64
65 public:
66 virtual ~Sink() = 0;
67
69 virtual bool
70 active(Severity level) const;
71
74 virtual bool
75 console() const;
76
79 virtual void
80 console(bool output);
81
83 virtual Severity
84 threshold() const;
85
87 virtual void
88 threshold(Severity thresh);
89
94 virtual void
95 write(Severity level, std::string const& text) = 0;
96
104 virtual void
105 writeAlways(Severity level, std::string const& text) = 0;
106
107 private:
110 };
111
112#ifndef __INTELLISENSE__
113 static_assert(std::is_default_constructible<Sink>::value == false, "");
114 static_assert(std::is_copy_constructible<Sink>::value == false, "");
115 static_assert(std::is_move_constructible<Sink>::value == false, "");
116 static_assert(std::is_copy_assignable<Sink>::value == false, "");
117 static_assert(std::is_move_assignable<Sink>::value == false, "");
118 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
119#endif
120
122 static Sink&
123 getNullSink();
124
125 //--------------------------------------------------------------------------
126
127 class Stream;
128
129 /* Scoped ostream-based container for writing messages to a Journal. */
131 {
132 public:
134 : ScopedStream(other.m_sink, other.m_level)
135 {
136 }
137
139
140 template <typename T>
141 ScopedStream(Stream const& stream, T const& t);
142
144
146 operator=(ScopedStream const&) = delete;
147
149
151 ostream() const
152 {
153 return m_ostream;
154 }
155
157 operator<<(std::ostream& manip(std::ostream&)) const;
158
159 template <typename T>
161 operator<<(T const& t) const;
162
163 private:
167 };
168
169#ifndef __INTELLISENSE__
170 static_assert(
172 "");
173 static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
174 static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
175 static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
176 static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
177 static_assert(
179 "");
180#endif
181
182 //--------------------------------------------------------------------------
183public:
185 class Stream
186 {
187 public:
189 explicit Stream()
190 : m_sink(getNullSink()), m_level(severities::kDisabled)
191 {
192 }
193
199 {
200 XRPL_ASSERT(
202 "beast::Journal::Stream::Stream : maximum level");
203 }
204
206 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
207 {
208 }
209
210 Stream&
211 operator=(Stream const& other) = delete;
212
214 Sink&
215 sink() const
216 {
217 return m_sink;
218 }
219
222 level() const
223 {
224 return m_level;
225 }
226
229 bool
230 active() const
231 {
232 return m_sink.active(m_level);
233 }
234
235 explicit
236 operator bool() const
237 {
238 return active();
239 }
245 operator<<(std::ostream& manip(std::ostream&)) const;
246
247 template <typename T>
249 operator<<(T const& t) const;
252 private:
255 };
256
257#ifndef __INTELLISENSE__
258 static_assert(std::is_default_constructible<Stream>::value == true, "");
259 static_assert(std::is_copy_constructible<Stream>::value == true, "");
260 static_assert(std::is_move_constructible<Stream>::value == true, "");
261 static_assert(std::is_copy_assignable<Stream>::value == false, "");
262 static_assert(std::is_move_assignable<Stream>::value == false, "");
263 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
264#endif
265
266 //--------------------------------------------------------------------------
267
269 Journal() = delete;
270
272 explicit Journal(Sink& sink) : m_sink(&sink)
273 {
274 }
275
277 Sink&
278 sink() const
279 {
280 return *m_sink;
281 }
282
284 Stream
285 stream(Severity level) const
286 {
287 return Stream(*m_sink, level);
288 }
289
294 bool
295 active(Severity level) const
296 {
297 return m_sink->active(level);
298 }
299
302 Stream
303 trace() const
304 {
305 return {*m_sink, severities::kTrace};
306 }
307
308 Stream
309 debug() const
310 {
311 return {*m_sink, severities::kDebug};
312 }
313
314 Stream
315 info() const
316 {
317 return {*m_sink, severities::kInfo};
318 }
319
320 Stream
321 warn() const
322 {
323 return {*m_sink, severities::kWarning};
324 }
325
326 Stream
327 error() const
328 {
329 return {*m_sink, severities::kError};
330 }
331
332 Stream
333 fatal() const
334 {
335 return {*m_sink, severities::kFatal};
336 }
338};
339
340#ifndef __INTELLISENSE__
341static_assert(std::is_default_constructible<Journal>::value == false, "");
342static_assert(std::is_copy_constructible<Journal>::value == true, "");
343static_assert(std::is_move_constructible<Journal>::value == true, "");
344static_assert(std::is_copy_assignable<Journal>::value == true, "");
345static_assert(std::is_move_assignable<Journal>::value == true, "");
346static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
347#endif
348
349//------------------------------------------------------------------------------
350
351template <typename T>
353 : ScopedStream(stream.sink(), stream.level())
354{
355 m_ostream << t;
356}
357
358template <typename T>
360Journal::ScopedStream::operator<<(T const& t) const
361{
362 m_ostream << t;
363 return m_ostream;
364}
365
366//------------------------------------------------------------------------------
367
368template <typename T>
370Journal::Stream::operator<<(T const& t) const
371{
372 return ScopedStream(*this, t);
373}
374
375namespace detail {
376
377template <class CharT, class Traits = std::char_traits<CharT>>
378class logstream_buf : public std::basic_stringbuf<CharT, Traits>
379{
381
382 template <class T>
383 void
384 write(T const*) = delete;
385
386 void
387 write(char const* s)
388 {
389 if (strm_)
390 strm_ << s;
391 }
392
393 void
394 write(wchar_t const* s)
395 {
396 if (strm_)
397 strm_ << s;
398 }
399
400public:
401 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
402 {
403 }
404
406 {
407 sync();
408 }
409
410 int
411 sync() override
412 {
413 write(this->str().c_str());
414 this->str("");
415 return 0;
416 }
417};
418
419} // namespace detail
420
421template <class CharT, class Traits = std::char_traits<CharT>>
422class basic_logstream : public std::basic_ostream<CharT, Traits>
423{
424 typedef CharT char_type;
425 typedef Traits traits_type;
426 typedef typename traits_type::int_type int_type;
427 typedef typename traits_type::pos_type pos_type;
428 typedef typename traits_type::off_type off_type;
429
431
432public:
434 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
435 {
436 }
437};
438
441
442} // namespace beast
443
444#endif
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
std::ostringstream & ostream() const
Definition Journal.h:151
std::ostringstream m_ostream
Definition Journal.h:166
ScopedStream(ScopedStream const &other)
Definition Journal.h:133
Abstraction for the underlying message destination.
Definition Journal.h:57
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:186
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition Journal.h:215
bool active() const
Returns true if sink logs anything at this stream's level.
Definition Journal.h:230
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition Journal.h:198
Stream(Stream const &other)
Construct or copy another Stream.
Definition Journal.h:206
Stream()
Create a stream which produces no output.
Definition Journal.h:189
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition Journal.h:222
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
A generic endpoint for log messages.
Definition Journal.h:41
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition Journal.h:272
Stream fatal() const
Definition Journal.h:333
Stream error() const
Definition Journal.h:327
Stream debug() const
Definition Journal.h:309
Sink & sink() const
Returns the Sink associated with this Journal.
Definition Journal.h:278
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition Journal.h:295
Stream info() const
Definition Journal.h:315
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition Journal.h:285
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:303
Stream warn() const
Definition Journal.h:321
Sink * m_sink
Definition Journal.h:50
traits_type::off_type off_type
Definition Journal.h:428
traits_type::int_type int_type
Definition Journal.h:426
detail::logstream_buf< CharT, Traits > buf_
Definition Journal.h:430
traits_type::pos_type pos_type
Definition Journal.h:427
basic_logstream(beast::Journal::Stream const &strm)
Definition Journal.h:433
void write(T const *)=delete
void write(char const *s)
Definition Journal.h:387
beast::Journal::Stream strm_
Definition Journal.h:380
logstream_buf(beast::Journal::Stream const &strm)
Definition Journal.h:401
void write(wchar_t const *s)
Definition Journal.h:394
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:13
STL namespace.