rippled
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 <ripple/beast/unit_test.h>
24 #include <ripple/beast/utility/Journal.h>
25 
26 namespace ripple {
27 namespace test {
28 
29 // A Journal::Sink intended for use with the beast unit test framework.
31 {
33  beast::unit_test::suite& suite_;
34 
35 public:
37  std::string const& partition,
39  beast::unit_test::suite& suite)
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 
55 inline 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 
93 public:
95  std::string const& partition,
96  beast::unit_test::suite& suite,
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 
118 public:
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
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
beast::severities::kTrace
@ kTrace
Definition: Journal.h:34
ripple::test::StreamSink::StreamSink
StreamSink(beast::severities::Severity threshold=beast::severities::kDebug)
Definition: SuiteJournal.h:119
std::string
STL class.
std::stringstream
STL class.
beast::severities
A namespace for easy access to logging severity values.
Definition: Journal.h:29
ripple::test::StreamSink::strm_
std::stringstream strm_
Definition: SuiteJournal.h:116
ripple::test::SuiteJournal::sink_
SuiteJournalSink sink_
Definition: SuiteJournal.h:90
ripple::test::StreamSink::write
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: SuiteJournal.h:126
beast::Journal::Sink::Sink
Sink()=delete
ripple::test::SuiteJournalSink::partition_
std::string partition_
Definition: SuiteJournal.h:32
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
beast::severities::kInfo
@ kInfo
Definition: Journal.h:36
ripple::test::SuiteJournal
Definition: SuiteJournal.h:88
ripple::test::StreamSink
Definition: SuiteJournal.h:114
beast::severities::kError
@ kError
Definition: Journal.h:38
ripple::test::StreamSink::messages
std::stringstream const & messages() const
Definition: SuiteJournal.h:134
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::endl
T endl(T... args)
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:106
ripple::test::SuiteJournalSink::SuiteJournalSink
SuiteJournalSink(std::string const &partition, beast::severities::Severity threshold, beast::unit_test::suite &suite)
Definition: SuiteJournal.h:36
ripple::test::SuiteJournal::SuiteJournal
SuiteJournal(std::string const &partition, beast::unit_test::suite &suite, beast::severities::Severity threshold=beast::severities::kFatal)
Definition: SuiteJournal.h:94
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
ripple::test::SuiteJournal::journal_
beast::Journal journal_
Definition: SuiteJournal.h:91
beast::severities::kWarning
@ kWarning
Definition: Journal.h:37
beast::severities::kDebug
@ kDebug
Definition: Journal.h:35
ripple::test::SuiteJournalSink
Definition: SuiteJournal.h:30
ripple::test::SuiteJournalSink::active
bool active(beast::severities::Severity level) const override
Returns true if text at the passed severity produces output.
Definition: SuiteJournal.h:46
beast::severities::kFatal
@ kFatal
Definition: Journal.h:39
ripple::test::SuiteJournalSink::suite_
beast::unit_test::suite & suite_
Definition: SuiteJournal.h:33
ripple::test::SuiteJournalSink::write
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: SuiteJournal.h:56