diff --git a/beast/utility/Journal.h b/beast/utility/Journal.h index 3b4171f36..6e6cca587 100644 --- a/beast/utility/Journal.h +++ b/beast/utility/Journal.h @@ -24,7 +24,18 @@ namespace beast { -/** A generic endpoint for log messages. */ +/** A generic endpoint for log messages. + + The Journal has a few simple goals: + + * To be light-weight and copied by value. + * To allow logging statements to be left in source code. + * The logging is controlled at run-time based on a logging threshold. + + It is advisable to check Journal::active(level) prior to formatting log + text. Doing so sidesteps expensive text formatting when the results + will not be sent to the log. +*/ class Journal { public: @@ -47,10 +58,7 @@ public: class Sink; private: - Journal& operator= (Journal const& other); // disallowed - Sink* m_sink; - Severity m_level; public: //-------------------------------------------------------------------------- @@ -58,9 +66,12 @@ public: /** Abstraction for the underlying message destination. */ class Sink { - public: - Sink (); + protected: + Sink () = delete; + explicit Sink(Sink const& sink) = default; + Sink (Severity thresh, bool console); + public: virtual ~Sink () = 0; /** Returns `true` if text at the passed severity produces output. */ @@ -73,19 +84,19 @@ public: virtual void console (bool output); /** Returns the minimum severity level this sink will report. */ - virtual Severity severity() const; + virtual Severity threshold() const; /** Set the minimum severity this sink will report. */ - virtual void severity (Severity level); + virtual void threshold (Severity thresh); /** Write text to the sink at the specified severity. - The caller is responsible for checking the minimum severity level - before using this function. + A conforming implementation will not write the text if the passed + level is below the current threshold(). */ virtual void write (Severity level, std::string const& text) = 0; private: - Severity m_level; + Severity thresh_; bool m_console; }; @@ -107,10 +118,8 @@ public: ScopedStream (Stream const& stream, T const& t) : m_sink (stream.sink()) , m_level (stream.severity()) - , m_active (stream.active ()) { - if (active ()) - m_ostream << t; + m_ostream << t; } ScopedStream (Stream const& stream, @@ -118,9 +127,6 @@ public: ~ScopedStream (); - bool active () const - { return m_active; } - std::ostringstream& ostream () const; std::ostream& operator<< ( @@ -129,8 +135,7 @@ public: template std::ostream& operator<< (T const& t) const { - if (active ()) - m_ostream << t; + m_ostream << t; return m_ostream; } @@ -140,10 +145,8 @@ public: ScopedStream& operator= (ScopedStream const&); // disallowed Sink& m_sink; - Severity const m_level; - bool const m_active; + Severity const m_level; // cached from Stream for call to m_sink.write std::ostringstream mutable m_ostream; - bool m_toOutputWindow; }; //-------------------------------------------------------------------------- @@ -155,10 +158,7 @@ public: Stream (); /** Create stream that writes at the given level. */ - /** @{ */ - Stream (Sink& sink, Severity level, bool active = true); - Stream (Stream const& stream, bool active); - /** @} */ + Stream (Sink& sink, Severity level); /** Construct or copy another Stream. */ /** @{ */ @@ -174,7 +174,10 @@ public: /** Returns `true` if sink logs anything at this stream's severity. */ /** @{ */ - bool active() const; + bool active() const + { + return m_sink->active (m_level); + } explicit operator bool() const @@ -197,7 +200,6 @@ public: private: Sink* m_sink; Severity m_level; - bool m_disabled; }; //-------------------------------------------------------------------------- @@ -206,17 +208,13 @@ public: Journal (); /** Create a journal that writes to the specified sink. */ - /** @{ */ - explicit Journal (Sink& sink, Severity level = kAll); + explicit Journal (Sink& sink); - /** Create a journal from another journal. - When specifying a new minimum severity level, the effective minimum - level will be the higher of the other journal and the specified value. - */ - /** @{ */ + /** Create a journal from another journal. */ Journal (Journal const& other); - Journal (Journal const& other, Severity level); - /** @} */ + + // Disallowed. + Journal& operator= (Journal const& other) = delete; /** Destroy the journal. */ ~Journal (); @@ -228,17 +226,11 @@ public: Stream stream (Severity level) const; /** Returns `true` if any message would be logged at this severity level. - For a message to be logged, the severity must be at or above both - the journal's severity level and the sink's severity level. + For a message to be logged, the severity must be at or above the + sink's severity threshold. */ bool active (Severity level) const; - /** Returns this Journal's minimum severity level. - If the underlying sink has a higher threshold, there will still - be no output at that level. - */ - Severity severity () const; - /** Convenience sink streams for each severity level. */ Stream const trace; Stream const debug; diff --git a/beast/utility/WrappedSink.h b/beast/utility/WrappedSink.h index 672812b9c..010670211 100644 --- a/beast/utility/WrappedSink.h +++ b/beast/utility/WrappedSink.h @@ -25,6 +25,11 @@ namespace beast { /** Wraps a Journal::Sink to prefix its output with a string. */ + +// A WrappedSink both is a Sink and has a Sink: +// o It inherits from Sink so it has the correct interface. +// o It has a sink (reference) so it preserves the passed write() behavior. +// The data inherited from the base class is ignored. class WrappedSink : public beast::Journal::Sink { private: @@ -34,15 +39,15 @@ private: public: explicit WrappedSink (beast::Journal::Sink& sink, std::string const& prefix = "") - : sink_(sink) + : Sink (sink) + , sink_(sink) , prefix_(prefix) { } explicit WrappedSink (beast::Journal const& journal, std::string const& prefix = "") - : sink_(journal.sink()) - , prefix_(prefix) + : WrappedSink (journal.sink(), prefix) { } @@ -69,14 +74,14 @@ public: } beast::Journal::Severity - severity() const override + threshold() const override { - return sink_.severity(); + return sink_.threshold(); } - void severity (beast::Journal::Severity level) override + void threshold (beast::Journal::Severity thresh) override { - sink_.severity (level); + sink_.threshold (thresh); } void write (beast::Journal::Severity level, std::string const& text) override diff --git a/beast/utility/impl/Journal.cpp b/beast/utility/impl/Journal.cpp index 92064aa1a..5b9283979 100644 --- a/beast/utility/impl/Journal.cpp +++ b/beast/utility/impl/Journal.cpp @@ -28,30 +28,36 @@ namespace beast { class NullJournalSink : public Journal::Sink { public: - bool active (Journal::Severity) const + NullJournalSink () + : Sink (Journal::kDisabled, false) + { } + + ~NullJournalSink() override = default; + + bool active (Journal::Severity) const override { return false; } - bool console() const + bool console() const override { return false; } - void console (bool) + void console (bool) override { } - Journal::Severity severity() const + Journal::Severity threshold() const override { return Journal::kDisabled; } - void severity (Journal::Severity) + void threshold (Journal::Severity) override { } - void write (Journal::Severity, std::string const&) + void write (Journal::Severity, std::string const&) override { } }; @@ -66,9 +72,9 @@ Journal::Sink& Journal::getNullSink () //------------------------------------------------------------------------------ -Journal::Sink::Sink () - : m_level (kWarning) - , m_console (false) +Journal::Sink::Sink (Severity thresh, bool console) + : thresh_ (thresh) + , m_console (console) { } @@ -78,7 +84,7 @@ Journal::Sink::~Sink () bool Journal::Sink::active (Severity level) const { - return level >= m_level; + return level >= thresh_; } bool Journal::Sink::console () const @@ -91,14 +97,14 @@ void Journal::Sink::console (bool output) m_console = output; } -Journal::Severity Journal::Sink::severity () const +Journal::Severity Journal::Sink::threshold () const { - return m_level; + return thresh_; } -void Journal::Sink::severity (Severity level) +void Journal::Sink::threshold (Severity thresh) { - m_level = level; + thresh_ = thresh; } //------------------------------------------------------------------------------ @@ -106,7 +112,6 @@ void Journal::Sink::severity (Severity level) Journal::ScopedStream::ScopedStream (Stream const& stream) : m_sink (stream.sink ()) , m_level (stream.severity ()) - , m_active (stream.active ()) { init (); } @@ -114,7 +119,6 @@ Journal::ScopedStream::ScopedStream (Stream const& stream) Journal::ScopedStream::ScopedStream (ScopedStream const& other) : m_sink (other.m_sink) , m_level (other.m_level) - , m_active (other.m_active) { init (); } @@ -123,25 +127,20 @@ Journal::ScopedStream::ScopedStream ( Stream const& stream, std::ostream& manip (std::ostream&)) : m_sink (stream.sink ()) , m_level (stream.severity ()) - , m_active (stream.active ()) { init (); - if (active ()) - m_ostream << manip; + m_ostream << manip; } Journal::ScopedStream::~ScopedStream () { - if (active ()) + std::string const& s (m_ostream.str()); + if (! s.empty ()) { - std::string const& s (m_ostream.str()); - if (! s.empty ()) - { - if (s == "\n") - m_sink.write (m_level, ""); - else - m_sink.write (m_level, s); - } + if (s == "\n") + m_sink.write (m_level, ""); + else + m_sink.write (m_level, s); } } @@ -150,10 +149,7 @@ void Journal::ScopedStream::init () // Modifiers applied from all ctors m_ostream << std::boolalpha - << std::showbase - //<< std::hex - ; - + << std::showbase; } std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const @@ -171,29 +167,19 @@ std::ostringstream& Journal::ScopedStream::ostream () const Journal::Stream::Stream () : m_sink (&getNullSink ()) , m_level (kDisabled) - , m_disabled (true) { } -Journal::Stream::Stream (Sink& sink, Severity level, bool active) +Journal::Stream::Stream (Sink& sink, Severity level) : m_sink (&sink) , m_level (level) - , m_disabled (! active) { assert (level != kDisabled); } -Journal::Stream::Stream (Stream const& stream, bool active) - : m_sink (&stream.sink ()) - , m_level (stream.severity ()) - , m_disabled (! active) -{ -} - Journal::Stream::Stream (Stream const& other) : m_sink (other.m_sink) , m_level (other.m_level) - , m_disabled (other.m_disabled) { } @@ -207,11 +193,6 @@ Journal::Severity Journal::Stream::severity () const return m_level; } -bool Journal::Stream::active () const -{ - return ! m_disabled && m_sink->active (m_level); -} - Journal::Stream& Journal::Stream::operator= (Stream const& other) { m_sink = other.m_sink; @@ -229,7 +210,6 @@ Journal::ScopedStream Journal::Stream::operator<< ( Journal::Journal () : m_sink (&getNullSink()) - , m_level (kDisabled) , trace (stream (kTrace)) , debug (stream (kDebug)) , info (stream (kInfo)) @@ -239,9 +219,8 @@ Journal::Journal () { } -Journal::Journal (Sink& sink, Severity level) +Journal::Journal (Sink& sink) : m_sink (&sink) - , m_level (level) , trace (stream (kTrace)) , debug (stream (kDebug)) , info (stream (kInfo)) @@ -252,26 +231,7 @@ Journal::Journal (Sink& sink, Severity level) } Journal::Journal (Journal const& other) - : m_sink (other.m_sink) - , m_level (other.m_level) - , trace (stream (kTrace)) - , debug (stream (kDebug)) - , info (stream (kInfo)) - , warning (stream (kWarning)) - , error (stream (kError)) - , fatal (stream (kFatal)) -{ -} - -Journal::Journal (Journal const& other, Severity level) - : m_sink (other.m_sink) - , m_level (std::max (other.m_level, level)) - , trace (stream (kTrace)) - , debug (stream (kDebug)) - , info (stream (kInfo)) - , warning (stream (kWarning)) - , error (stream (kError)) - , fatal (stream (kFatal)) + : Journal (*other.m_sink) { } @@ -286,21 +246,12 @@ Journal::Sink& Journal::sink() const Journal::Stream Journal::stream (Severity level) const { - return Stream (*m_sink, level, level >= m_level); + return Stream (*m_sink, level); } bool Journal::active (Severity level) const { - if (level == kDisabled) - return false; - if (level < m_level) - return false; return m_sink->active (level); } -Journal::Severity Journal::severity () const -{ - return m_level; -} - } diff --git a/beast/utility/tests/Journal.test.cpp b/beast/utility/tests/Journal.test.cpp index 78d510c7f..786771d33 100644 --- a/beast/utility/tests/Journal.test.cpp +++ b/beast/utility/tests/Journal.test.cpp @@ -32,7 +32,8 @@ public: public: TestSink() - : m_count(0) + : Sink (Journal::kWarning, false) + , m_count(0) { } @@ -49,9 +50,10 @@ public: } void - write (Journal::Severity, std::string const&) + write (Journal::Severity level, std::string const&) override { - ++m_count; + if (level >= threshold()) + ++m_count; } }; @@ -59,7 +61,7 @@ public: { TestSink sink; - sink.severity(Journal::kInfo); + sink.threshold(Journal::kInfo); Journal j(sink); @@ -78,7 +80,7 @@ public: sink.reset(); - sink.severity(Journal::kDebug); + sink.threshold(Journal::kDebug); j.trace << " "; expect(sink.count() == 0);