xrpld
Loading...
Searching...
No Matches
Journal.cpp
1#include <xrpl/beast/utility/Journal.h>
2
3#include <gtest/gtest.h>
4
5#include <cstddef>
6#include <string>
7
8namespace beast {
9namespace {
10
11// Counts the messages that actually reach the sink, so the tests can assert on
12// which severities the journal let through.
13class CountingSink : public Journal::Sink
14{
15 std::size_t count_{0};
16
17public:
18 CountingSink() : Sink(Severity::Warning, false)
19 {
20 }
21
22 [[nodiscard]] std::size_t
23 count() const
24 {
25 return count_;
26 }
27
28 void
29 write(Severity level, std::string const&) override
30 {
31 if (level >= threshold())
32 ++count_;
33 }
34
35 void
36 writeAlways(Severity, std::string const&) override
37 {
38 ++count_;
39 }
40};
41
42} // namespace
43
44TEST(Journal, info_threshold)
45{
46 CountingSink sink;
47 sink.threshold(Severity::Info);
48 Journal const j(sink);
49
50 j.trace() << " ";
51 EXPECT_EQ(sink.count(), 0uz);
52 j.debug() << " ";
53 EXPECT_EQ(sink.count(), 0uz);
54 j.info() << " ";
55 EXPECT_EQ(sink.count(), 1uz);
56 j.warn() << " ";
57 EXPECT_EQ(sink.count(), 2uz);
58 j.error() << " ";
59 EXPECT_EQ(sink.count(), 3uz);
60 j.fatal() << " ";
61 EXPECT_EQ(sink.count(), 4uz);
62}
63
64TEST(Journal, debug_threshold)
65{
66 CountingSink sink;
67 sink.threshold(Severity::Debug);
68 Journal const j(sink);
69
70 j.trace() << " ";
71 EXPECT_EQ(sink.count(), 0uz);
72 j.debug() << " ";
73 EXPECT_EQ(sink.count(), 1uz);
74 j.info() << " ";
75 EXPECT_EQ(sink.count(), 2uz);
76 j.warn() << " ";
77 EXPECT_EQ(sink.count(), 3uz);
78 j.error() << " ";
79 EXPECT_EQ(sink.count(), 4uz);
80 j.fatal() << " ";
81 EXPECT_EQ(sink.count(), 5uz);
82}
83
84// A Journal holds a reference to its sink rather than a copy of the threshold,
85// so lowering the threshold has to take effect on a Journal that already exists.
86// The two tests above each build the Journal after setting the threshold, so
87// neither of them would notice if that stopped working.
88TEST(Journal, threshold_change_applies_to_an_existing_journal)
89{
90 CountingSink sink;
91 sink.threshold(Severity::Info);
92 Journal const j(sink);
93
94 j.debug() << " ";
95 EXPECT_EQ(sink.count(), 0uz);
96
97 sink.threshold(Severity::Debug);
98
99 j.debug() << " ";
100 EXPECT_EQ(sink.count(), 1uz);
101}
102
103} // namespace beast
Abstraction for the underlying message destination.
Definition Journal.h:59
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
Stream info() const
Definition Journal.h:350
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
Stream warn() const
Definition Journal.h:356
T count(T... args)
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:16
TEST(BasicSecondsClock, now)
void write(nudb::detail::ostream &os, std::size_t t)
Definition Varint.h:120