rippled
Loading...
Searching...
No Matches
SuiteJournal.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef TEST_UNIT_TEST_SUITE_JOURNAL_H
21#define TEST_UNIT_TEST_SUITE_JOURNAL_H
22
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/beast/utility/Journal.h>
25
26namespace ripple {
27namespace test {
28
29// A Journal::Sink intended for use with the beast unit test framework.
31{
34
35public:
37 std::string const& partition,
40 : Sink(threshold, false), partition_(partition + " "), suite_(suite)
41 {
42 }
43
44 // For unit testing, always generate logging text.
45 inline bool
46 active(beast::severities::Severity level) const override
47 {
48 return true;
49 }
50
51 void
52 write(beast::severities::Severity level, std::string const& text) override;
53
54 void
56 override;
57};
58
59inline void
62 std::string const& text)
63{
64 // Only write the string if the level at least equals the threshold.
65 if (level >= threshold())
66 writeAlways(level, text);
67}
68
69inline void
72 std::string const& text)
73{
74 using namespace beast::severities;
75
76 char const* const s = [level]() {
77 switch (level)
78 {
79 case kTrace:
80 return "TRC:";
81 case kDebug:
82 return "DBG:";
83 case kInfo:
84 return "INF:";
85 case kWarning:
86 return "WRN:";
87 case kError:
88 return "ERR:";
89 default:
90 break;
91 case kFatal:
92 break;
93 }
94 return "FTL:";
95 }();
96
97 suite_.log << s << partition_ << text << std::endl;
98}
99
101{
104
105public:
107 std::string const& partition,
110 : sink_(partition, threshold, suite), journal_(sink_)
111 {
112 }
113 // Clang 10.0.0 and 10.0.1 disagree about formatting operator&
114 // TBD Re-enable formatting when we upgrade to clang 11
115 // clang-format off
116 operator beast::Journal &()
117 // clang-format on
118 {
119 return journal_;
120 }
121};
122
123// this sink can be used to create a custom journal
124// whose log messages will be captured to a stringstream
125// that can be later inspected.
127{
129
130public:
133 : Sink(threshold, false)
134 {
135 }
136
137 void
138 write(beast::severities::Severity level, std::string const& text) override
139 {
140 if (level < threshold())
141 return;
142 writeAlways(level, text);
143 }
144
145 inline void
147 override
148 {
149 strm_ << text << std::endl;
150 }
151
152 std::stringstream const&
153 messages() const
154 {
155 return strm_;
156 }
157};
158
159} // namespace test
160} // namespace ripple
161
162#endif
Abstraction for the underlying message destination.
Definition: Journal.h:76
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition: Journal.h:60
A testsuite class.
Definition: suite.h:55
log_os< char > log
Logging output stream.
Definition: suite.h:152
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: SuiteJournal.h:138
std::stringstream strm_
Definition: SuiteJournal.h:128
std::stringstream const & messages() const
Definition: SuiteJournal.h:153
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition: SuiteJournal.h:146
StreamSink(beast::severities::Severity threshold=beast::severities::kDebug)
Definition: SuiteJournal.h:131
bool active(beast::severities::Severity level) const override
Returns true if text at the passed severity produces output.
Definition: SuiteJournal.h:46
beast::unit_test::suite & suite_
Definition: SuiteJournal.h:33
SuiteJournalSink(std::string const &partition, beast::severities::Severity threshold, beast::unit_test::suite &suite)
Definition: SuiteJournal.h:36
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: SuiteJournal.h:60
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition: SuiteJournal.h:70
SuiteJournal(std::string const &partition, beast::unit_test::suite &suite, beast::severities::Severity threshold=beast::severities::kFatal)
Definition: SuiteJournal.h:106
SuiteJournalSink sink_
Definition: SuiteJournal.h:102
T endl(T... args)
A namespace for easy access to logging severity values.
Definition: Journal.h:30
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26