xrpld
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::test {
7
8// A Journal::Sink intended for use with the beast unit test framework.
10{
13
14public:
16 std::string const& partition,
19 : Sink(threshold, false), partition_(partition + " "), suite_(suite)
20 {
21 }
22
23 // For unit testing, always generate logging text.
24 [[nodiscard]] bool
25 active(beast::Severity level) const override
26 {
27 return true;
28 }
29
30 void
31 write(beast::Severity level, std::string const& text) override;
32
33 void
34 writeAlways(beast::Severity level, std::string const& text) override;
35};
36
37inline void
39{
40 // Only write the string if the level at least equals the threshold.
41 if (level >= threshold())
42 writeAlways(level, text);
43}
44
45inline void
47{
48 using beast::Severity;
49
50 char const* const s = [level]() {
51 switch (level)
52 {
53 case Severity::Trace:
54 return "TRC:";
55 case Severity::Debug:
56 return "DBG:";
57 case Severity::Info:
58 return "INF:";
59 case Severity::Warning:
60 return "WRN:";
61 case Severity::Error:
62 return "ERR:";
63 case Severity::Fatal:
64 default:
65 break;
66 }
67 return "FTL:";
68 }();
69
70 static std::mutex kLogMutex;
71 std::scoped_lock const lock(kLogMutex);
72 suite_.log << s << partition_ << text << std::endl;
73}
74
76{
79
80public:
82 std::string const& partition,
85 : sink_(partition, threshold, suite), journal_(sink_)
86 {
87 }
88 operator beast::Journal&()
89 {
90 return journal_;
91 }
92};
93
94// this sink can be used to create a custom journal
95// whose log messages will be captured to a stringstream
96// that can be later inspected.
98{
100
101public:
105
106 void
107 write(beast::Severity level, std::string const& text) override
108 {
109 if (level < threshold())
110 return;
111 writeAlways(level, text);
112 }
113
114 void
115 writeAlways(beast::Severity level, std::string const& text) override
116 {
117 strm_ << text << std::endl;
118 }
119
120 std::stringstream const&
121 messages() const
122 {
123 return strm_;
124 }
125};
126
127} // namespace xrpl::test
Abstraction for the underlying message destination.
Definition Journal.h:51
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Sink(Sink const &sink)=default
A generic endpoint for log messages.
Definition Journal.h:38
A testsuite class.
Definition suite.h:50
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
std::stringstream const & messages() const
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
std::stringstream strm_
StreamSink(beast::Severity threshold=beast::Severity::Debug)
SuiteJournalSink(std::string const &partition, beast::Severity threshold, beast::unit_test::Suite &suite)
void writeAlways(beast::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::Severity level) const override
Returns true if text at the passed severity produces output.
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
SuiteJournal(std::string const &partition, beast::unit_test::Suite &suite, beast::Severity threshold=beast::Severity::Fatal)
SuiteJournalSink sink_
beast::Journal journal_
T endl(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:11