From f36fcf635c780d3c90f8eecb6b98b495c5a9e3a4 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 14 Sep 2013 18:44:58 -0700 Subject: [PATCH] Journal improvements --- .../modules/beast_core/diagnostic/Journal.cpp | 149 ++++++++++++++- .../modules/beast_core/diagnostic/Journal.h | 179 ++++++++---------- src/ripple/validators/impl/Logic.h | 8 +- src/ripple/validators/impl/Manager.cpp | 2 +- src/ripple/validators/impl/SourceURL.cpp | 2 +- src/ripple/validators/impl/StoreSqdb.cpp | 2 +- 6 files changed, 230 insertions(+), 112 deletions(-) diff --git a/src/beast/modules/beast_core/diagnostic/Journal.cpp b/src/beast/modules/beast_core/diagnostic/Journal.cpp index 3271e7cd2e..10e0553769 100644 --- a/src/beast/modules/beast_core/diagnostic/Journal.cpp +++ b/src/beast/modules/beast_core/diagnostic/Journal.cpp @@ -17,7 +17,14 @@ */ //============================================================================== -Journal::Sink* Journal::getNullSink () +bool Journal::Sink::active (Severity) +{ + return true; +} + +//------------------------------------------------------------------------------ + +Journal::Sink& Journal::getNullSink () { // A Sink that does nothing. class NullSink : public Sink @@ -33,6 +40,144 @@ Journal::Sink* Journal::getNullSink () } }; - return SharedSingleton ::get ( + return *SharedSingleton ::get ( SingletonLifetime::neverDestroyed); } + +//------------------------------------------------------------------------------ + +Journal::ScopedStream::ScopedStream (Stream const& stream) + : m_sink (stream.sink()) + , m_severity (stream.severity()) +{ +} + +Journal::ScopedStream::ScopedStream (ScopedStream const& other) + : m_sink (other.m_sink) + , m_severity (other.m_severity) +{ +} + +Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&)) + : m_sink (stream.sink()) + , m_severity (stream.severity()) +{ + m_ostream << manip; +} + +Journal::ScopedStream::~ScopedStream () +{ + if (m_sink.active (m_severity)) + { + if (! m_ostream.str().empty()) + m_sink.write (m_severity, m_ostream.str()); + } +} + +std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const +{ + return m_ostream << manip; +} + +std::ostringstream& Journal::ScopedStream::ostream () const +{ + return m_ostream; +} + +//------------------------------------------------------------------------------ + +Journal::Stream::Stream () + : m_sink (&getNullSink ()) + , m_severity (kFatal) +{ +} + +Journal::Stream::Stream (Sink& sink, Severity severity) + : m_sink (&sink) + , m_severity (severity) +{ +} + +Journal::Stream::Stream (Stream const& other) + : m_sink (other.m_sink) + , m_severity (other.m_severity) +{ +} + +bool Journal::Stream::active () const +{ + return m_sink->active (m_severity); +} + +Journal::Sink& Journal::Stream::sink () const +{ + return *m_sink; +} + +Journal::Severity Journal::Stream::severity () const +{ + return m_severity; +} + +Journal::Stream& Journal::Stream::operator= (Stream const& other) +{ + m_sink = other.m_sink; + m_severity = other.m_severity; + return *this; +} + +Journal::ScopedStream Journal::Stream::operator<< (std::ostream& manip (std::ostream&)) const +{ + return ScopedStream (*this, manip); +} + +//------------------------------------------------------------------------------ + +Journal::Journal () + : m_sink (&getNullSink()) + , trace (stream (kTrace)) + , debug (stream (kDebug)) + , info (stream (kInfo)) + , warning (stream (kWarning)) + , error (stream (kError)) + , fatal (stream (kFatal)) +{ +} + +Journal::Journal (Sink& sink) + : m_sink (&sink) + , trace (stream (kTrace)) + , debug (stream (kDebug)) + , info (stream (kInfo)) + , warning (stream (kWarning)) + , error (stream (kError)) + , fatal (stream (kFatal)) +{ +} + +Journal::Journal (Journal const& other) + : m_sink (other.m_sink) + , trace (stream (kTrace)) + , debug (stream (kDebug)) + , info (stream (kInfo)) + , warning (stream (kWarning)) + , error (stream (kError)) + , fatal (stream (kFatal)) +{ +} + +Journal::~Journal () +{ +} + +Journal::Stream Journal::stream (Severity severity) const +{ + return Stream (*m_sink, severity); +} + +/** Returns `true` if the sink logs messages at that severity. */ +bool Journal::active (Severity severity) const +{ + return m_sink->active (severity); +} + diff --git a/src/beast/modules/beast_core/diagnostic/Journal.h b/src/beast/modules/beast_core/diagnostic/Journal.h index 7eb89278f4..eabe4a542d 100644 --- a/src/beast/modules/beast_core/diagnostic/Journal.h +++ b/src/beast/modules/beast_core/diagnostic/Journal.h @@ -23,6 +23,12 @@ /** A generic endpoint for log messages. */ class Journal { +public: + class Sink; + +private: + Sink* m_sink; + public: /** Severity level of the message. */ enum Severity @@ -46,143 +52,110 @@ public: /** 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) - { - return true; - } + /** Returns `true` if text at the passed severity produces output. + The default implementation always returns `true`. + */ + virtual bool active (Severity); }; /** Returns a Sink which does nothing. */ - static Sink* getNullSink (); + static Sink& getNullSink (); //-------------------------------------------------------------------------- + + class Stream; - /** Scoped container for building journal messages. */ - class Stream + /** Scoped ostream-based container for writing messages to a Journal. */ + class ScopedStream { public: - explicit Stream (Sink* sink, Severity severity) - : m_sink (sink) - , m_severity (severity) + explicit ScopedStream (Stream const& stream); + ScopedStream (ScopedStream const& other); + + template + ScopedStream (Stream const& stream, T const& t) + : m_sink (stream.sink()) + , m_severity (stream.severity()) { + m_ostream << t; } - Stream (Stream const& other) - : m_sink (other.m_sink) - , m_severity (other.m_severity) - { - } + ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&)); - Stream& operator= (Stream const& other) - { - m_sink = other.m_sink; - m_severity = other.m_severity; - return *this; - } + ~ScopedStream (); - ~Stream () - { - if (m_sink ->active (m_severity)) - m_sink->write (m_severity, m_stream.str ()); - } + std::ostringstream& ostream () const; + + std::ostream& operator<< (std::ostream& manip (std::ostream&)) const; template std::ostream& operator<< (T const& t) const { - return m_stream << t; + return m_ostream << t; } - std::ostringstream& stream () const + private: + ScopedStream& operator= (ScopedStream const&); // disallowed + + Sink& m_sink; + Severity const m_severity; + std::ostringstream mutable m_ostream; + }; + + //-------------------------------------------------------------------------- + + class Stream + { + public: + /** Construct a stream which produces no logging output. */ + Stream (); + + Stream (Sink& sink, Severity severity); + Stream (Stream const& other); + Stream& operator= (Stream const& other); + + /** Returns `true` if the sink logs messages at the severity of this stream. */ + bool active() const; + + Sink& sink() const; + Severity severity() const; + + ScopedStream operator<< (std::ostream& manip (std::ostream&)) const; + + template + ScopedStream operator<< (T const& t) const { - return m_stream; + return ScopedStream (*this, t); } private: Sink* m_sink; Severity m_severity; - std::ostringstream mutable m_stream; }; //-------------------------------------------------------------------------- - Journal (Sink* sink = getNullSink ()) - : m_sink (sink) - { - } + Journal (); + explicit Journal (Sink& sink); + Journal (Journal const& other); + ~Journal (); - bool reportActive (Severity severity) const - { - return m_sink->active (severity); - } + /** Returns a stream for this sink, with the specified severity. */ + Stream stream (Severity severity) const; - bool traceActive () const - { - return reportActive (kTrace); - } + /** Returns `true` if the sink logs messages at that severity. */ + bool active (Severity severity) const; - bool debugActive () const - { - return reportActive (kDebug); - } - - bool infoActive () const - { - return reportActive (kInfo); - } - - bool warningActive () const - { - return reportActive (kWarning); - } - - bool errorActive () const - { - return reportActive (kError); - } - - bool fatalActive () const - { - return reportActive (kFatal); - } - - Stream report (Severity severity) const - { - return Stream (m_sink, severity); - } - - Stream trace () const - { - return report (kTrace); - } - - Stream debug () const - { - return report (kDebug); - } - - Stream info () const - { - return report (kInfo); - } - - Stream warning () const - { - return report (kWarning); - } - - Stream error () const - { - return report (kError); - } - - Stream fatal () const - { - return Stream (m_sink, kFatal); - } + /** Convenience sink streams for each severity level. */ + Stream const trace; + Stream const debug; + Stream const info; + Stream const warning; + Stream const error; + Stream const fatal; private: - Sink* m_sink; + Journal& operator= (Journal const& other); // disallowed }; #endif diff --git a/src/ripple/validators/impl/Logic.h b/src/ripple/validators/impl/Logic.h index 525620b1f1..0f2d1d9e1c 100644 --- a/src/ripple/validators/impl/Logic.h +++ b/src/ripple/validators/impl/Logic.h @@ -69,7 +69,7 @@ public: // void addStatic (Source* source) { - m_journal.info() << "Add static Source, " << source->name(); + m_journal.info << "Add static Source, " << source->name(); ScopedPointer object (source); @@ -91,7 +91,7 @@ public: // void add (Source* source) { - m_journal.info() << "Add Source, " << source->name(); + m_journal.info << "Add Source, " << source->name(); SourceDesc& desc (*m_sources.emplace_back ()); desc.source = source; @@ -159,7 +159,7 @@ public: // This is thread safe m_chosenList = list; - m_journal.debug() << + m_journal.debug << "Rebuilt chosen list with " << String::fromNumber (m_chosenList->size()) << " entries"; } @@ -196,7 +196,7 @@ public: /** Perform a fetch on the source. */ void fetch (SourceDesc& desc, CancelCallback& callback) { - m_journal.info() << "fetch ('" << desc.source->name() << "')"; + m_journal.info << "fetch ('" << desc.source->name() << "')"; Source::Result result (desc.source->fetch (callback, m_journal)); diff --git a/src/ripple/validators/impl/Manager.cpp b/src/ripple/validators/impl/Manager.cpp index e6d50f2f62..b3cf8bf796 100644 --- a/src/ripple/validators/impl/Manager.cpp +++ b/src/ripple/validators/impl/Manager.cpp @@ -192,7 +192,7 @@ public: if (error) { - m_journal.fatal() << + m_journal.fatal << "Failed to open '" << file.getFullPathName() << "'"; } diff --git a/src/ripple/validators/impl/SourceURL.cpp b/src/ripple/validators/impl/SourceURL.cpp index 088f086bb9..07fd548d75 100644 --- a/src/ripple/validators/impl/SourceURL.cpp +++ b/src/ripple/validators/impl/SourceURL.cpp @@ -48,7 +48,7 @@ public: } else { - journal.error() << + journal.error << "HTTP GET to " << m_url.full().toStdString() << " failed: '" << httpResult.error.message () << "'"; } diff --git a/src/ripple/validators/impl/StoreSqdb.cpp b/src/ripple/validators/impl/StoreSqdb.cpp index c0263590c7..3615a1b023 100644 --- a/src/ripple/validators/impl/StoreSqdb.cpp +++ b/src/ripple/validators/impl/StoreSqdb.cpp @@ -160,7 +160,7 @@ void StoreSqdb::report (Error const& error, char const* fileName, int lineNumber { if (error) { - m_journal.error() << + m_journal.error << "Failure: '"<< error.getReasonText() << "' " << " at " << Debug::getSourceLocation (fileName, lineNumber); }