rippled
beast_Journal.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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/beast/utility/Journal.h>
21 #include <cassert>
22 
23 namespace beast {
24 
25 //------------------------------------------------------------------------------
26 
27 // A Sink that does nothing.
29 {
30 public:
31  NullJournalSink() : Sink(severities::kDisabled, false)
32  {
33  }
34 
35  ~NullJournalSink() override = default;
36 
37  bool active(severities::Severity) const override
38  {
39  return false;
40  }
41 
42  bool
43  console() const override
44  {
45  return false;
46  }
47 
48  void
49  console(bool) override
50  {
51  }
52 
54  threshold() const override
55  {
56  return severities::kDisabled;
57  }
58 
60  {
61  }
62 
63  void
65  {
66  }
67 };
68 
69 //------------------------------------------------------------------------------
70 
71 Journal::Sink&
73 {
74  static NullJournalSink sink;
75  return sink;
76 }
77 
78 //------------------------------------------------------------------------------
79 
80 Journal::Sink::Sink(Severity thresh, bool console)
81  : thresh_(thresh), m_console(console)
82 {
83 }
84 
85 Journal::Sink::~Sink() = default;
86 
87 bool
89 {
90  return level >= thresh_;
91 }
92 
93 bool
95 {
96  return m_console;
97 }
98 
99 void
101 {
102  m_console = output;
103 }
104 
107 {
108  return thresh_;
109 }
110 
111 void
113 {
114  thresh_ = thresh;
115 }
116 
117 //------------------------------------------------------------------------------
118 
120  : m_sink(sink), m_level(level)
121 {
122  // Modifiers applied from all ctors
124 }
125 
127  Stream const& stream,
128  std::ostream& manip(std::ostream&))
129  : ScopedStream(stream.sink(), stream.level())
130 {
131  m_ostream << manip;
132 }
133 
135 {
136  std::string const& s(m_ostream.str());
137  if (!s.empty())
138  {
139  if (s == "\n")
140  m_sink.write(m_level, "");
141  else
142  m_sink.write(m_level, s);
143  }
144 }
145 
148 {
149  return m_ostream << manip;
150 }
151 
152 //------------------------------------------------------------------------------
153 
156 {
157  return ScopedStream(*this, manip);
158 }
159 
160 } // namespace beast
beast::NullJournalSink::console
void console(bool) override
Set whether messages are also written to the Output Window (MSVC).
Definition: beast_Journal.cpp:49
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:74
beast::NullJournalSink::threshold
void threshold(severities::Severity) override
Set the minimum severity this sink will report.
Definition: beast_Journal.cpp:59
beast::Journal::ScopedStream::~ScopedStream
~ScopedStream()
Definition: beast_Journal.cpp:134
std::string
STL class.
beast::severities::kDisabled
@ kDisabled
Definition: Journal.h:41
beast::Journal::ScopedStream::m_ostream
std::ostringstream m_ostream
Definition: Journal.h:175
beast::Journal::ScopedStream::ScopedStream
ScopedStream(ScopedStream const &other)
Definition: Journal.h:142
beast::NullJournalSink
Definition: beast_Journal.cpp:28
beast::NullJournalSink::NullJournalSink
NullJournalSink()
Definition: beast_Journal.cpp:31
beast::NullJournalSink::write
void write(severities::Severity, std::string const &) override
Write text to the sink at the specified severity.
Definition: beast_Journal.cpp:64
std::showbase
T showbase(T... args)
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:72
beast::Journal::Stream::operator<<
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
Definition: beast_Journal.cpp:155
beast::Journal::Sink::~Sink
virtual ~Sink()=0
std::ostream
STL class.
beast::Journal::ScopedStream::operator<<
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
Definition: beast_Journal.cpp:147
beast::Journal::sink
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:284
beast::Journal::Sink::active
virtual bool active(Severity level) const
Returns true if text at the passed severity produces output.
Definition: beast_Journal.cpp:88
beast::Journal::stream
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:291
beast::Journal::Sink::Sink
Sink()=delete
beast::Journal::m_sink
Sink * m_sink
Definition: Journal.h:68
beast::Journal::Sink::console
virtual bool console() const
Returns true if a message is also written to the Output Window (MSVC).
Definition: beast_Journal.cpp:94
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:194
beast::Journal::ScopedStream
Definition: Journal.h:139
beast::NullJournalSink::active
bool active(severities::Severity) const override
Returns true if text at the passed severity produces output.
Definition: beast_Journal.cpp:37
beast::NullJournalSink::console
bool console() const override
Returns true if a message is also written to the Output Window (MSVC).
Definition: beast_Journal.cpp:43
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:106
std::boolalpha
T boolalpha(T... args)
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:31
cassert
std::string::empty
T empty(T... args)
beast::Journal::Sink::write
virtual void write(Severity level, std::string const &text)=0
Write text to the sink at the specified severity.
beast::NullJournalSink::~NullJournalSink
~NullJournalSink() override=default
beast::NullJournalSink::threshold
severities::Severity threshold() const override
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:54
beast
Definition: base_uint.h:641