xrpld
Loading...
Searching...
No Matches
SuiteJournal.h
1#pragma once
2
3#include <xrpl/beast/unit_test/suite.h>
4#include <xrpl/beast/utility/Journal.h>
5
6#include <mutex>
7#include <ostream>
8#include <sstream>
9#include <string>
10
11namespace xrpl::test {
12
13// A Journal::Sink intended for use with the beast unit test framework.
15{
18
19public:
21 std::string const& partition,
24 : Sink(threshold, false), partition_(partition + " "), suite_(suite)
25 {
26 }
27
28 // For unit testing, always generate logging text.
29 [[nodiscard]] bool
30 active(beast::Severity level) const override
31 {
32 return true;
33 }
34
35 void
36 write(beast::Severity level, std::string const& text) override;
37
38 void
39 writeAlways(beast::Severity level, std::string const& text) override;
40};
41
42inline void
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
52{
53 using beast::Severity;
54
55 char const* const s = [level]() {
56 switch (level)
57 {
58 case Severity::Trace:
59 return "TRC:";
60 case Severity::Debug:
61 return "DBG:";
62 case Severity::Info:
63 return "INF:";
64 case Severity::Warning:
65 return "WRN:";
66 case Severity::Error:
67 return "ERR:";
68 case Severity::Fatal:
69 default:
70 break;
71 }
72 return "FTL:";
73 }();
74
75 static std::mutex kLogMutex;
76 std::scoped_lock const lock(kLogMutex);
77 suite_.log << s << partition_ << text << std::endl;
78}
79
81{
84
85public:
87 std::string const& partition,
90 : sink_(partition, threshold, suite), journal_(sink_)
91 {
92 }
93 operator beast::Journal&()
94 {
95 return journal_;
96 }
97};
98
99// this sink can be used to create a custom journal
100// whose log messages will be captured to a stringstream
101// that can be later inspected.
103{
105
106public:
110
111 void
112 write(beast::Severity level, std::string const& text) override
113 {
114 if (level < threshold())
115 return;
116 writeAlways(level, text);
117 }
118
119 void
120 writeAlways(beast::Severity level, std::string const& text) override
121 {
122 strm_ << text << std::endl;
123 }
124
125 std::stringstream const&
126 messages() const
127 {
128 return strm_;
129 }
130};
131
132} // namespace xrpl::test
Abstraction for the underlying message destination.
Definition Journal.h:59
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:44
A testsuite class.
Definition suite.h:52
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:16