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 const 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 operator beast::Journal&()
91 {
92 return journal_;
93 }
94};
95
96// this sink can be used to create a custom journal
97// whose log messages will be captured to a stringstream
98// that can be later inspected.
100{
102
103public:
108
109 void
110 write(beast::severities::Severity level, std::string const& text) override
111 {
112 if (level < threshold())
113 return;
114 writeAlways(level, text);
115 }
116
117 inline void
119 {
120 strm_ << text << std::endl;
121 }
122
123 std::stringstream const&
124 messages() const
125 {
126 return strm_;
127 }
128};
129
130} // namespace test
131} // 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:147
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