xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
unit_test
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
11
namespace
xrpl::test
{
12
13
// A Journal::Sink intended for use with the beast unit test framework.
14
class
SuiteJournalSink
:
public
beast::Journal::Sink
15
{
16
std::string
partition_
;
17
beast::unit_test::Suite
&
suite_
;
18
19
public
:
20
SuiteJournalSink
(
21
std::string
const
& partition,
22
beast::Severity
threshold
,
23
beast::unit_test::Suite
& suite)
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
42
inline
void
43
SuiteJournalSink::write
(
beast::Severity
level,
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
50
inline
void
51
SuiteJournalSink::writeAlways
(
beast::Severity
level,
std::string
const
& text)
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
80
class
SuiteJournal
81
{
82
SuiteJournalSink
sink_
;
83
beast::Journal
journal_
;
84
85
public
:
86
SuiteJournal
(
87
std::string
const
& partition,
88
beast::unit_test::Suite
& suite,
89
beast::Severity
threshold =
beast::Severity::Fatal
)
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.
102
class
StreamSink
:
public
beast::Journal::Sink
103
{
104
std::stringstream
strm_
;
105
106
public
:
107
StreamSink
(
beast::Severity
threshold
=
beast::Severity::Debug
) :
Sink
(
threshold
, false)
108
{
109
}
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
std::string
std::stringstream
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition
Journal.h:59
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition
beast_Journal.cpp:96
beast::Journal::Sink::Sink
Sink(Sink const &sink)=default
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
xrpl::test::StreamSink::write
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition
SuiteJournal.h:112
xrpl::test::StreamSink::messages
std::stringstream const & messages() const
Definition
SuiteJournal.h:126
xrpl::test::StreamSink::writeAlways
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition
SuiteJournal.h:120
xrpl::test::StreamSink::strm_
std::stringstream strm_
Definition
SuiteJournal.h:104
xrpl::test::StreamSink::StreamSink
StreamSink(beast::Severity threshold=beast::Severity::Debug)
Definition
SuiteJournal.h:107
xrpl::test::SuiteJournalSink
Definition
SuiteJournal.h:15
xrpl::test::SuiteJournalSink::SuiteJournalSink
SuiteJournalSink(std::string const &partition, beast::Severity threshold, beast::unit_test::Suite &suite)
Definition
SuiteJournal.h:20
xrpl::test::SuiteJournalSink::partition_
std::string partition_
Definition
SuiteJournal.h:16
xrpl::test::SuiteJournalSink::writeAlways
void writeAlways(beast::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition
SuiteJournal.h:51
xrpl::test::SuiteJournalSink::suite_
beast::unit_test::Suite & suite_
Definition
SuiteJournal.h:17
xrpl::test::SuiteJournalSink::active
bool active(beast::Severity level) const override
Returns true if text at the passed severity produces output.
Definition
SuiteJournal.h:30
xrpl::test::SuiteJournalSink::write
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition
SuiteJournal.h:43
xrpl::test::SuiteJournal::SuiteJournal
SuiteJournal(std::string const &partition, beast::unit_test::Suite &suite, beast::Severity threshold=beast::Severity::Fatal)
Definition
SuiteJournal.h:86
xrpl::test::SuiteJournal::sink_
SuiteJournalSink sink_
Definition
SuiteJournal.h:82
xrpl::test::SuiteJournal::journal_
beast::Journal journal_
Definition
SuiteJournal.h:83
std::endl
T endl(T... args)
mutex
beast::Severity
Severity
Severity level / threshold of a Journal message.
Definition
Journal.h:16
beast::Severity::Fatal
@ Fatal
Definition
Journal.h:24
beast::Severity::Debug
@ Debug
Definition
Journal.h:20
xrpl::test
Definition
STLedgerEntry.h:21
ostream
std::scoped_lock
sstream
string
Generated by
1.17.0