diff --git a/beast/utility/Journal.h b/beast/utility/Journal.h index e3d7df3fd2..9c85b6d412 100644 --- a/beast/utility/Journal.h +++ b/beast/utility/Journal.h @@ -20,10 +20,11 @@ #ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED #define BEAST_UTILITY_JOURNAL_H_INCLUDED +#include "../SafeBool.h" + #include -namespace beast -{ +namespace beast { /** A generic endpoint for log messages. */ class Journal @@ -57,27 +58,33 @@ public: public: virtual ~Sink () { } - /** Write text to the sink at the specified severity. */ - virtual void write (Severity severity, std::string const& text) = 0; - /** Returns `true` if text at the passed severity produces output. */ - virtual bool active (Severity severity) = 0; + virtual bool active (Severity severity) const = 0; /** Returns `true` if a message is also written to the Output Window (MSVC). */ - virtual bool console () = 0; - - /** Set the minimum severity this sink will report. */ - virtual void set_severity (Severity severity) = 0; + virtual bool console () const = 0; /** Set whether messages are also written to the Output Window (MSVC). */ - virtual void set_console (bool to_console) = 0; + virtual void console (bool output) = 0; + + /** Returns the minimum severity level this sink will report. */ + virtual Severity severity() const = 0; + + /** Set the minimum severity this sink will report. */ + virtual void severity (Severity level) = 0; + + /** Write text to the sink at the specified severity. + The caller is responsible for checking the minimum severity level + before using this function. + */ + virtual void write (Severity level, std::string const& text) = 0; }; /** Returns a Sink which does nothing. */ static Sink& getNullSink (); //-------------------------------------------------------------------------- - + class Stream; /** Scoped ostream-based container for writing messages to a Journal. */ @@ -122,7 +129,7 @@ public: //-------------------------------------------------------------------------- - class Stream + class Stream : public SafeBool { public: /** Construct a stream which produces no logging output. */ @@ -144,7 +151,10 @@ public: Severity severity() const; /** Returns `true` if sink logs anything at this stream's severity. */ + /** @{ */ bool active() const; + bool asBoolean() const; + /** @} */ /** Output stream support. */ /** @{ */ diff --git a/beast/utility/impl/Journal.cpp b/beast/utility/impl/Journal.cpp index 9829af1ac7..6d990ce7a7 100644 --- a/beast/utility/impl/Journal.cpp +++ b/beast/utility/impl/Journal.cpp @@ -19,8 +19,7 @@ #include "../Journal.h" -namespace beast -{ +namespace beast { //------------------------------------------------------------------------------ @@ -28,27 +27,32 @@ namespace beast class NullJournalSink : public Journal::Sink { public: + bool active (Journal::Severity) const + { + return false; + } + + bool console() const + { + return false; + } + + void console (bool) + { + } + + Journal::Severity severity() const + { + return Journal::kDisabled; + } + + void severity (Journal::Severity) + { + } + void write (Journal::Severity, std::string const&) { } - - bool active (Journal::Severity) - { - return false; - } - - bool console () - { - return false; - } - - void set_severity (Journal::Severity) - { - } - - void set_console (bool) - { - } }; //------------------------------------------------------------------------------ @@ -85,7 +89,7 @@ Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip ( Journal::ScopedStream::~ScopedStream () { - if (! m_ostream.str().empty()) + if (! m_ostream.str().empty() && m_sink.active (m_severity)) m_sink.write (m_severity, m_ostream.str()); } @@ -146,6 +150,11 @@ bool Journal::Stream::active () const return m_sink->active (m_severity); } +bool Journal::Stream::asBoolean () const +{ + return active(); +} + Journal::Stream& Journal::Stream::operator= (Stream const& other) { m_sink = other.m_sink; diff --git a/modules/beast_core/diagnostic/UnitTest.cpp b/modules/beast_core/diagnostic/UnitTest.cpp index edcd7a903a..77b2b0aa93 100644 --- a/modules/beast_core/diagnostic/UnitTest.cpp +++ b/modules/beast_core/diagnostic/UnitTest.cpp @@ -234,27 +234,32 @@ UnitTests::JournalSink::JournalSink (UnitTests& tests) { } -void UnitTests::JournalSink::write (Journal::Severity, std::string const& text) -{ - m_tests.logMessage (text); -} - -bool UnitTests::JournalSink::active (Journal::Severity) +bool UnitTests::JournalSink::active (Journal::Severity) const { return true; } -bool UnitTests::JournalSink::console () +bool UnitTests::JournalSink::console() const { return false; } -void UnitTests::JournalSink::set_severity (Journal::Severity) +void UnitTests::JournalSink::console (bool) { } -void UnitTests::JournalSink::set_console (bool) +Journal::Severity UnitTests::JournalSink::severity() const { + return Journal::kLowestSeverity; +} + +void UnitTests::JournalSink::severity (Journal::Severity) +{ +} + +void UnitTests::JournalSink::write (Journal::Severity, std::string const& text) +{ + m_tests.logMessage (text); } //============================================================================== diff --git a/modules/beast_core/diagnostic/UnitTest.h b/modules/beast_core/diagnostic/UnitTest.h index 59e5ce7697..8a3a7655b3 100644 --- a/modules/beast_core/diagnostic/UnitTest.h +++ b/modules/beast_core/diagnostic/UnitTest.h @@ -517,11 +517,12 @@ private: { public: explicit JournalSink (UnitTests& tests); + bool active (Journal::Severity severity) const; + bool console () const; + void console (bool); + Journal::Severity severity() const; + void severity (Journal::Severity severity); void write (Journal::Severity severity, std::string const& text); - bool active (Journal::Severity severity); - bool console (); - void set_severity (Journal::Severity severity); - void set_console (bool); private: UnitTests& m_tests;