rippled
Loading...
Searching...
No Matches
SuiteJournal.h
1#pragma once
2
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/beast/utility/Journal.h>
5
6namespace xrpl {
7namespace test {
8
9// A Journal::Sink intended for use with the beast unit test framework.
11{
14
15public:
17 std::string const& partition,
20 : Sink(threshold, false), partition_(partition + " "), suite_(suite)
21 {
22 }
23
24 // For unit testing, always generate logging text.
25 inline bool
26 active(beast::severities::Severity level) const override
27 {
28 return true;
29 }
30
31 void
32 write(beast::severities::Severity level, std::string const& text) override;
33
34 void
35 writeAlways(beast::severities::Severity level, std::string const& text) override;
36};
37
38inline void
40{
41 // Only write the string if the level at least equals the threshold.
42 if (level >= threshold())
43 writeAlways(level, text);
44}
45
46inline void
48{
49 using namespace beast::severities;
50
51 char const* const s = [level]() {
52 switch (level)
53 {
54 case kTrace:
55 return "TRC:";
56 case kDebug:
57 return "DBG:";
58 case kInfo:
59 return "INF:";
60 case kWarning:
61 return "WRN:";
62 case kError:
63 return "ERR:";
64 default:
65 break;
66 case kFatal:
67 break;
68 }
69 return "FTL:";
70 }();
71
72 static std::mutex log_mutex;
73 std::lock_guard lock(log_mutex);
74 suite_.log << s << partition_ << text << std::endl;
75}
76
78{
81
82public:
84 std::string const& partition,
87 : sink_(partition, threshold, suite), journal_(sink_)
88 {
89 }
90 // Clang 10.0.0 and 10.0.1 disagree about formatting operator&
91 // TBD Re-enable formatting when we upgrade to clang 11
92 // clang-format off
93 operator beast::Journal &()
94 // clang-format on
95 {
96 return journal_;
97 }
98};
99
100// this sink can be used to create a custom journal
101// whose log messages will be captured to a stringstream
102// that can be later inspected.
104{
106
107public:
111
112 void
113 write(beast::severities::Severity level, std::string const& text) override
114 {
115 if (level < threshold())
116 return;
117 writeAlways(level, text);
118 }
119
120 inline void
122 {
123 strm_ << text << std::endl;
124 }
125
126 std::stringstream const&
127 messages() const
128 {
129 return strm_;
130 }
131};
132
133} // namespace test
134} // namespace xrpl
Abstraction for the underlying message destination.
Definition Journal.h:56
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition Journal.h:40
A testsuite class.
Definition suite.h:51
log_os< char > log
Logging output stream.
Definition suite.h:144
StreamSink(beast::severities::Severity threshold=beast::severities::kDebug)
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
std::stringstream const & messages() const
std::stringstream strm_
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
SuiteJournalSink(std::string const &partition, beast::severities::Severity threshold, beast::unit_test::suite &suite)
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
beast::unit_test::suite & suite_
bool active(beast::severities::Severity level) const override
Returns true if text at the passed severity produces output.
SuiteJournal(std::string const &partition, beast::unit_test::suite &suite, beast::severities::Severity threshold=beast::severities::kFatal)
SuiteJournalSink sink_
beast::Journal journal_
T endl(T... args)
A namespace for easy access to logging severity values.
Definition Journal.h:10
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:12
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5