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