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 static std::mutex log_mutex;
98 std::lock_guard lock(log_mutex);
99 suite_.log << s << partition_ << text << std::endl;
100}
101
103{
106
107public:
109 std::string const& partition,
112 : sink_(partition, threshold, suite), journal_(sink_)
113 {
114 }
115 // Clang 10.0.0 and 10.0.1 disagree about formatting operator&
116 // TBD Re-enable formatting when we upgrade to clang 11
117 // clang-format off
118 operator beast::Journal &()
119 // clang-format on
120 {
121 return journal_;
122 }
123};
124
125// this sink can be used to create a custom journal
126// whose log messages will be captured to a stringstream
127// that can be later inspected.
129{
131
132public:
138
139 void
140 write(beast::severities::Severity level, std::string const& text) override
141 {
142 if (level < threshold())
143 return;
144 writeAlways(level, text);
145 }
146
147 inline void
149 override
150 {
151 strm_ << text << std::endl;
152 }
153
154 std::stringstream const&
155 messages() const
156 {
157 return strm_;
158 }
159};
160
161} // namespace test
162} // namespace ripple
163
164#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.
std::stringstream strm_
std::stringstream const & messages() const
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
StreamSink(beast::severities::Severity threshold=beast::severities::kDebug)
bool active(beast::severities::Severity level) const override
Returns true if text at the passed severity produces output.
beast::unit_test::suite & suite_
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.
SuiteJournal(std::string const &partition, beast::unit_test::suite &suite, beast::severities::Severity threshold=beast::severities::kFatal)
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:25