xrpld
Loading...
Searching...
No Matches
CaptureSink.h
1#pragma once
2
3#include <xrpl/beast/utility/Journal.h>
4
5#include <mutex>
6#include <sstream>
7#include <string>
8
9namespace xrpl::test {
10
12{
15
16public:
21
22 void
23 write(beast::Severity level, std::string const& text) override
24 {
25 if (level < threshold())
26 return;
27 writeAlways(level, text);
28 }
29
30 void
31 writeAlways(beast::Severity /*level*/, std::string const& text) override
32 {
33 // Journal sinks may be written to concurrently (e.g. from a backend's background workers),
34 // so serialize access to strm_. write() funnels into writeAlways(), so the lock lives here
35 // only: locking in both would self-deadlock on this non-recursive mutex.
36 std::scoped_lock const lock(mutex_);
37 strm_ << text << '\n';
38 }
39
40 [[nodiscard]] std::string
41 messages() const
42 {
43 // Returns a snapshot of the captured output. Takes the lock so the read is safe even if a
44 // writer is still active.
45 std::scoped_lock const lock(mutex_);
46 return strm_.str();
47 }
48};
49
50} // namespace xrpl::test
Abstraction for the underlying message destination.
Definition Journal.h:59
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Sink(Sink const &sink)=default
void writeAlways(beast::Severity, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
Definition CaptureSink.h:31
CaptureSink(beast::Severity threshold=beast::Severity::Debug)
Definition CaptureSink.h:17
void write(beast::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
Definition CaptureSink.h:23
std::stringstream strm_
Definition CaptureSink.h:14
std::string messages() const
Definition CaptureSink.h:41
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:16