rippled
Loading...
Searching...
No Matches
Journal.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED
21#define BEAST_UTILITY_JOURNAL_H_INCLUDED
22
23#include <xrpl/beast/utility/instrumentation.h>
24#include <sstream>
25
26namespace beast {
27
29namespace severities {
32 kAll = 0,
33
40
43};
44} // namespace severities
45
59{
60public:
61 class Sink;
62
63private:
64 // Severity level / threshold of a Journal message.
66
67 // Invariant: m_sink always points to a valid Sink
69
70public:
71 //--------------------------------------------------------------------------
72
74 class Sink
75 {
76 protected:
77 Sink() = delete;
78 explicit Sink(Sink const& sink) = default;
79 Sink(Severity thresh, bool console);
80 Sink&
81 operator=(Sink const& lhs) = delete;
82
83 public:
84 virtual ~Sink() = 0;
85
87 virtual bool
88 active(Severity level) const;
89
92 virtual bool
93 console() const;
94
97 virtual void
98 console(bool output);
99
101 virtual Severity
102 threshold() const;
103
105 virtual void
106 threshold(Severity thresh);
107
112 virtual void
113 write(Severity level, std::string const& text) = 0;
114
115 private:
118 };
119
120#ifndef __INTELLISENSE__
121 static_assert(std::is_default_constructible<Sink>::value == false, "");
122 static_assert(std::is_copy_constructible<Sink>::value == false, "");
123 static_assert(std::is_move_constructible<Sink>::value == false, "");
124 static_assert(std::is_copy_assignable<Sink>::value == false, "");
125 static_assert(std::is_move_assignable<Sink>::value == false, "");
126 static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
127#endif
128
130 static Sink&
131 getNullSink();
132
133 //--------------------------------------------------------------------------
134
135 class Stream;
136
137 /* Scoped ostream-based container for writing messages to a Journal. */
139 {
140 public:
142 : ScopedStream(other.m_sink, other.m_level)
143 {
144 }
145
147
148 template <typename T>
149 ScopedStream(Stream const& stream, T const& t);
150
152
154 operator=(ScopedStream const&) = delete;
155
157
159 ostream() const
160 {
161 return m_ostream;
162 }
163
165 operator<<(std::ostream& manip(std::ostream&)) const;
166
167 template <typename T>
169 operator<<(T const& t) const;
170
171 private:
175 };
176
177#ifndef __INTELLISENSE__
178 static_assert(
180 "");
181 static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
182 static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
183 static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
184 static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
185 static_assert(
187 "");
188#endif
189
190 //--------------------------------------------------------------------------
191public:
193 class Stream
194 {
195 public:
197 explicit Stream()
198 : m_sink(getNullSink()), m_level(severities::kDisabled)
199 {
200 }
201
207 {
208 XRPL_ASSERT(
210 "beast::Journal::Stream::Stream : maximum level");
211 }
212
214 Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
215 {
216 }
217
218 Stream&
219 operator=(Stream const& other) = delete;
220
222 Sink&
223 sink() const
224 {
225 return m_sink;
226 }
227
230 level() const
231 {
232 return m_level;
233 }
234
237 bool
238 active() const
239 {
240 return m_sink.active(m_level);
241 }
242
243 explicit
244 operator bool() const
245 {
246 return active();
247 }
253 operator<<(std::ostream& manip(std::ostream&)) const;
254
255 template <typename T>
257 operator<<(T const& t) const;
260 private:
263 };
264
265#ifndef __INTELLISENSE__
266 static_assert(std::is_default_constructible<Stream>::value == true, "");
267 static_assert(std::is_copy_constructible<Stream>::value == true, "");
268 static_assert(std::is_move_constructible<Stream>::value == true, "");
269 static_assert(std::is_copy_assignable<Stream>::value == false, "");
270 static_assert(std::is_move_assignable<Stream>::value == false, "");
271 static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
272#endif
273
274 //--------------------------------------------------------------------------
275
277 Journal() = delete;
278
280 explicit Journal(Sink& sink) : m_sink(&sink)
281 {
282 }
283
285 Sink&
286 sink() const
287 {
288 return *m_sink;
289 }
290
292 Stream
293 stream(Severity level) const
294 {
295 return Stream(*m_sink, level);
296 }
297
302 bool
303 active(Severity level) const
304 {
305 return m_sink->active(level);
306 }
307
310 Stream
311 trace() const
312 {
313 return {*m_sink, severities::kTrace};
314 }
315
316 Stream
317 debug() const
318 {
319 return {*m_sink, severities::kDebug};
320 }
321
322 Stream
323 info() const
324 {
325 return {*m_sink, severities::kInfo};
326 }
327
328 Stream
329 warn() const
330 {
331 return {*m_sink, severities::kWarning};
332 }
333
334 Stream
335 error() const
336 {
337 return {*m_sink, severities::kError};
338 }
339
340 Stream
341 fatal() const
342 {
343 return {*m_sink, severities::kFatal};
344 }
346};
347
348#ifndef __INTELLISENSE__
349static_assert(std::is_default_constructible<Journal>::value == false, "");
350static_assert(std::is_copy_constructible<Journal>::value == true, "");
351static_assert(std::is_move_constructible<Journal>::value == true, "");
352static_assert(std::is_copy_assignable<Journal>::value == true, "");
353static_assert(std::is_move_assignable<Journal>::value == true, "");
354static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
355#endif
356
357//------------------------------------------------------------------------------
358
359template <typename T>
361 : ScopedStream(stream.sink(), stream.level())
362{
363 m_ostream << t;
364}
365
366template <typename T>
368Journal::ScopedStream::operator<<(T const& t) const
369{
370 m_ostream << t;
371 return m_ostream;
372}
373
374//------------------------------------------------------------------------------
375
376template <typename T>
378Journal::Stream::operator<<(T const& t) const
379{
380 return ScopedStream(*this, t);
381}
382
383namespace detail {
384
385template <class CharT, class Traits = std::char_traits<CharT>>
386class logstream_buf : public std::basic_stringbuf<CharT, Traits>
387{
389
390 template <class T>
391 void
392 write(T const*) = delete;
393
394 void
395 write(char const* s)
396 {
397 if (strm_)
398 strm_ << s;
399 }
400
401 void
402 write(wchar_t const* s)
403 {
404 if (strm_)
405 strm_ << s;
406 }
407
408public:
409 explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
410 {
411 }
412
414 {
415 sync();
416 }
417
418 int
419 sync() override
420 {
421 write(this->str().c_str());
422 this->str("");
423 return 0;
424 }
425};
426
427} // namespace detail
428
429template <class CharT, class Traits = std::char_traits<CharT>>
430class basic_logstream : public std::basic_ostream<CharT, Traits>
431{
432 typedef CharT char_type;
433 typedef Traits traits_type;
434 typedef typename traits_type::int_type int_type;
435 typedef typename traits_type::pos_type pos_type;
436 typedef typename traits_type::off_type off_type;
437
439
440public:
442 : std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
443 {
444 }
445};
446
449
450} // namespace beast
451
452#endif
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
ScopedStream & operator=(ScopedStream const &)=delete
Severity const m_level
Definition: Journal.h:173
std::ostringstream & ostream() const
Definition: Journal.h:159
std::ostringstream m_ostream
Definition: Journal.h:174
ScopedStream(ScopedStream const &other)
Definition: Journal.h:141
Abstraction for the underlying message destination.
Definition: Journal.h:75
Severity thresh_
Definition: Journal.h:116
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.
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:194
Sink & sink() const
Returns the Sink that this Stream writes to.
Definition: Journal.h:223
bool active() const
Returns true if sink logs anything at this stream's level.
Definition: Journal.h:238
Stream & operator=(Stream const &other)=delete
Stream(Sink &sink, Severity level)
Create a stream that writes at the given level.
Definition: Journal.h:206
Stream(Stream const &other)
Construct or copy another Stream.
Definition: Journal.h:214
Stream()
Create a stream which produces no output.
Definition: Journal.h:197
Severity level() const
Returns the Severity level of messages this Stream reports.
Definition: Journal.h:230
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
A generic endpoint for log messages.
Definition: Journal.h:59
Journal(Sink &sink)
Create a journal that writes to the specified sink.
Definition: Journal.h:280
Stream fatal() const
Definition: Journal.h:341
Stream error() const
Definition: Journal.h:335
Stream debug() const
Definition: Journal.h:317
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:286
bool active(Severity level) const
Returns true if any message would be logged at this severity level.
Definition: Journal.h:303
Stream info() const
Definition: Journal.h:323
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:293
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:311
Stream warn() const
Definition: Journal.h:329
Sink * m_sink
Definition: Journal.h:68
traits_type::off_type off_type
Definition: Journal.h:436
traits_type::int_type int_type
Definition: Journal.h:434
detail::logstream_buf< CharT, Traits > buf_
Definition: Journal.h:438
traits_type::pos_type pos_type
Definition: Journal.h:435
basic_logstream(beast::Journal::Stream const &strm)
Definition: Journal.h:441
void write(T const *)=delete
void write(char const *s)
Definition: Journal.h:395
beast::Journal::Stream strm_
Definition: Journal.h:388
logstream_buf(beast::Journal::Stream const &strm)
Definition: Journal.h:409
void write(wchar_t const *s)
Definition: Journal.h:402
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
STL namespace.