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
55inline void
58 std::string const& text)
59{
60 using namespace beast::severities;
61
62 char const* const s = [level]() {
63 switch (level)
64 {
65 case kTrace:
66 return "TRC:";
67 case kDebug:
68 return "DBG:";
69 case kInfo:
70 return "INF:";
71 case kWarning:
72 return "WRN:";
73 case kError:
74 return "ERR:";
75 default:
76 break;
77 case kFatal:
78 break;
79 }
80 return "FTL:";
81 }();
82
83 // Only write the string if the level at least equals the threshold.
84 if (level >= threshold())
85 suite_.log << s << partition_ << text << std::endl;
86}
87
89{
92
93public:
95 std::string const& partition,
98 : sink_(partition, threshold, suite), journal_(sink_)
99 {
100 }
101 // Clang 10.0.0 and 10.0.1 disagree about formatting operator&
102 // TBD Re-enable formatting when we upgrade to clang 11
103 // clang-format off
104 operator beast::Journal &()
105 // clang-format on
106 {
107 return journal_;
108 }
109};
110
111// this sink can be used to create a custom journal
112// whose log messages will be captured to a stringstream
113// that can be later inspected.
115{
117
118public:
121 : Sink(threshold, false)
122 {
123 }
124
125 void
126 write(beast::severities::Severity level, std::string const& text) override
127 {
128 if (level < threshold())
129 return;
130
131 strm_ << text << std::endl;
132 }
133 std::stringstream const&
134 messages() const
135 {
136 return strm_;
137 }
138};
139
140} // namespace test
141} // namespace ripple
142
143#endif
Abstraction for the underlying message destination.
Definition: Journal.h:75
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
Definition: Journal.h:59
A testsuite class.
Definition: suite.h:53
log_os< char > log
Logging output stream.
Definition: suite.h:150
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: SuiteJournal.h:126
std::stringstream strm_
Definition: SuiteJournal.h:116
std::stringstream const & messages() const
Definition: SuiteJournal.h:134
StreamSink(beast::severities::Severity threshold=beast::severities::kDebug)
Definition: SuiteJournal.h:119
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:56
SuiteJournal(std::string const &partition, beast::unit_test::suite &suite, beast::severities::Severity threshold=beast::severities::kFatal)
Definition: SuiteJournal.h:94
SuiteJournalSink sink_
Definition: SuiteJournal.h:90
T endl(T... args)
A namespace for easy access to logging severity values.
Definition: Journal.h:29
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26