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:
32  : Sink (severities::kDisabled, false)
33  { }
34 
35  ~NullJournalSink() override = default;
36 
37  bool active (severities::Severity) const override
38  {
39  return false;
40  }
41 
42  bool console() const override
43  {
44  return false;
45  }
46 
47  void console (bool) override
48  {
49  }
50 
52  {
53  return severities::kDisabled;
54  }
55 
57  {
58  }
59 
60  void write (severities::Severity, std::string const&) override
61  {
62  }
63 };
64 
65 //------------------------------------------------------------------------------
66 
68 {
69  static NullJournalSink sink;
70  return sink;
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 Journal::Sink::Sink (Severity thresh, bool console)
76  : thresh_ (thresh)
77  , m_console (console)
78 {
79 }
80 
81 Journal::Sink::~Sink() = default;
82 
83 bool Journal::Sink::active (Severity level) const
84 {
85  return level >= thresh_;
86 }
87 
89 {
90  return m_console;
91 }
92 
93 void Journal::Sink::console (bool output)
94 {
95  m_console = output;
96 }
97 
99 {
100  return thresh_;
101 }
102 
104 {
105  thresh_ = thresh;
106 }
107 
108 //------------------------------------------------------------------------------
109 
111  : m_sink (sink)
112  , m_level (level)
113 {
114  // Modifiers applied from all ctors
115  m_ostream
116  << std::boolalpha
117  << std::showbase;
118 }
119 
121  Stream const& stream, std::ostream& manip (std::ostream&))
122  : ScopedStream (stream.sink(), stream.level())
123 {
124  m_ostream << manip;
125 }
126 
128 {
129  std::string const& s (m_ostream.str());
130  if (! s.empty ())
131  {
132  if (s == "\n")
133  m_sink.write (m_level, "");
134  else
135  m_sink.write (m_level, s);
136  }
137 }
138 
140 {
141  return m_ostream << manip;
142 }
143 
144 //------------------------------------------------------------------------------
145 
147  std::ostream& manip (std::ostream&)) const
148 {
149  return ScopedStream (*this, manip);
150 }
151 
152 } // beast
153 
beast::NullJournalSink::console
void console(bool) override
Set whether messages are also written to the Output Window (MSVC).
Definition: beast_Journal.cpp:47
beast::Journal::Sink
Abstraction for the underlying message destination.
Definition: Journal.h:76
beast::NullJournalSink::threshold
void threshold(severities::Severity) override
Set the minimum severity this sink will report.
Definition: beast_Journal.cpp:56
beast::Journal::ScopedStream::~ScopedStream
~ScopedStream()
Definition: beast_Journal.cpp:127
std::string
STL class.
beast::severities::kDisabled
@ kDisabled
Definition: Journal.h:43
beast::Journal::ScopedStream::m_ostream
std::ostringstream m_ostream
Definition: Journal.h:164
beast::Journal::ScopedStream::ScopedStream
ScopedStream(ScopedStream const &other)
Definition: Journal.h:134
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:60
std::showbase
T showbase(T... args)
beast::Journal::getNullSink
static Sink & getNullSink()
Returns a Sink which does nothing.
Definition: beast_Journal.cpp:67
beast::Journal::Stream::operator<<
ScopedStream operator<<(std::ostream &manip(std::ostream &)) const
Output stream support.
Definition: beast_Journal.cpp:146
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:139
beast::Journal::sink
Sink & sink() const
Returns the Sink associated with this Journal.
Definition: Journal.h:265
beast::Journal::Sink::active
virtual bool active(Severity level) const
Returns true if text at the passed severity produces output.
Definition: beast_Journal.cpp:83
beast::Journal::stream
Stream stream(Severity level) const
Returns a stream for this sink, with the specified severity level.
Definition: Journal.h:271
beast::Journal::Sink::Sink
Sink()=delete
beast::Journal::m_sink
Sink * m_sink
Definition: Journal.h:70
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:88
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:179
beast::Journal::ScopedStream
Definition: Journal.h:131
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:42
beast::Journal::Sink::threshold
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
Definition: beast_Journal.cpp:98
std::boolalpha
T boolalpha(T... args)
beast::severities::Severity
Severity
Severity level / threshold of a Journal message.
Definition: Journal.h:32
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:51
beast
Definition: base_uint.h:582