rippled
CaptureLogs.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 #include <ripple/basics/Log.h>
21 
22 namespace ripple {
23 namespace test {
24 
31 class CaptureLogs : public Logs
32 {
36 
41  {
44 
45  public:
48  std::mutex& mutex,
49  std::stringstream& strm)
50  : beast::Journal::Sink(threshold, false)
51  , strmMutex_(mutex)
52  , strm_(strm)
53  {
54  }
55 
56  void
58  override
59  {
61  strm_ << text;
62  }
63  };
64 
65 public:
66  explicit CaptureLogs(std::string* pResult)
67  : Logs(beast::severities::kInfo), pResult_(pResult)
68  {
69  }
70 
71  ~CaptureLogs() override
72  {
73  *pResult_ = strm_.str();
74  }
75 
78  std::string const& partition,
80  {
81  return std::make_unique<CaptureSink>(threshold, strmMutex_, strm_);
82  }
83 };
84 
85 } // namespace test
86 } // namespace ripple
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
std::string
STL class.
ripple::Logs
Manages partitions for logging.
Definition: Log.h:48
ripple::test::CaptureLogs::CaptureSink::write
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition: CaptureLogs.h:57
ripple::test::CaptureLogs::CaptureLogs
CaptureLogs(std::string *pResult)
Definition: CaptureLogs.h:66
std::stringstream
STL class.
std::lock_guard
STL class.
ripple::test::CaptureLogs::~CaptureLogs
~CaptureLogs() override
Definition: CaptureLogs.h:71
ripple::test::CaptureLogs::CaptureSink::strm_
std::stringstream & strm_
Definition: CaptureLogs.h:43
ripple::test::CaptureLogs::makeSink
std::unique_ptr< beast::Journal::Sink > makeSink(std::string const &partition, beast::severities::Severity threshold) override
Definition: CaptureLogs.h:77
ripple::test::CaptureLogs::strm_
std::stringstream strm_
Definition: CaptureLogs.h:34
ripple::Logs::Sink
Definition: Log.h:51
ripple::test::CaptureLogs::CaptureSink
sink for writing all log messages to a stringstream
Definition: CaptureLogs.h:40
ripple::test::CaptureLogs::pResult_
std::string * pResult_
Definition: CaptureLogs.h:35
ripple::test::CaptureLogs::strmMutex_
std::mutex strmMutex_
Definition: CaptureLogs.h:33
beast::severities::kInfo
@ kInfo
Definition: Journal.h:36
ripple::test::CaptureLogs
Log manager for CaptureSinks.
Definition: CaptureLogs.h:31
ripple::Logs::threshold
beast::severities::Severity threshold() const
Definition: Log.cpp:150
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:106
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
ripple::test::CaptureLogs::CaptureSink::CaptureSink
CaptureSink(beast::severities::Severity threshold, std::mutex &mutex, std::stringstream &strm)
Definition: CaptureLogs.h:46
ripple::test::CaptureLogs::CaptureSink::strmMutex_
std::mutex & strmMutex_
Definition: CaptureLogs.h:42
std::mutex
STL class.
std::stringstream::str
T str(T... args)
std::unique_ptr< beast::Journal::Sink >
beast
Definition: base_uint.h:641