Journal improvements

This commit is contained in:
Vinnie Falco
2013-09-14 18:44:58 -07:00
parent 607520f2e6
commit f36fcf635c
6 changed files with 230 additions and 112 deletions

View File

@@ -17,7 +17,14 @@
*/ */
//============================================================================== //==============================================================================
Journal::Sink* Journal::getNullSink () bool Journal::Sink::active (Severity)
{
return true;
}
//------------------------------------------------------------------------------
Journal::Sink& Journal::getNullSink ()
{ {
// A Sink that does nothing. // A Sink that does nothing.
class NullSink : public Sink class NullSink : public Sink
@@ -33,6 +40,144 @@ Journal::Sink* Journal::getNullSink ()
} }
}; };
return SharedSingleton <NullSink>::get ( return *SharedSingleton <NullSink>::get (
SingletonLifetime::neverDestroyed); 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);
}

View File

@@ -23,6 +23,12 @@
/** A generic endpoint for log messages. */ /** A generic endpoint for log messages. */
class Journal class Journal
{ {
public:
class Sink;
private:
Sink* m_sink;
public: public:
/** Severity level of the message. */ /** Severity level of the message. */
enum Severity enum Severity
@@ -46,143 +52,110 @@ public:
/** Write text to the sink at the specified severity. */ /** Write text to the sink at the specified severity. */
virtual void write (Severity severity, std::string const& text) = 0; virtual void write (Severity severity, std::string const& text) = 0;
/** Returns `true` if text at the passed severity produces output. */ /** Returns `true` if text at the passed severity produces output.
virtual bool active (Severity) The default implementation always returns `true`.
{ */
return true; virtual bool active (Severity);
}
}; };
/** Returns a Sink which does nothing. */ /** Returns a Sink which does nothing. */
static Sink* getNullSink (); static Sink& getNullSink ();
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
class Stream;
/** Scoped container for building journal messages. */ /** Scoped ostream-based container for writing messages to a Journal. */
class Stream class ScopedStream
{ {
public: public:
explicit Stream (Sink* sink, Severity severity) explicit ScopedStream (Stream const& stream);
: m_sink (sink) ScopedStream (ScopedStream const& other);
, m_severity (severity)
template <typename T>
ScopedStream (Stream const& stream, T const& t)
: m_sink (stream.sink())
, m_severity (stream.severity())
{ {
m_ostream << t;
} }
Stream (Stream const& other) ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&));
: m_sink (other.m_sink)
, m_severity (other.m_severity)
{
}
Stream& operator= (Stream const& other) ~ScopedStream ();
{
m_sink = other.m_sink;
m_severity = other.m_severity;
return *this;
}
~Stream () std::ostringstream& ostream () const;
{
if (m_sink ->active (m_severity)) std::ostream& operator<< (std::ostream& manip (std::ostream&)) const;
m_sink->write (m_severity, m_stream.str ());
}
template <typename T> template <typename T>
std::ostream& operator<< (T const& t) const 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 <typename T>
ScopedStream operator<< (T const& t) const
{ {
return m_stream; return ScopedStream (*this, t);
} }
private: private:
Sink* m_sink; Sink* m_sink;
Severity m_severity; Severity m_severity;
std::ostringstream mutable m_stream;
}; };
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
Journal (Sink* sink = getNullSink ()) Journal ();
: m_sink (sink) explicit Journal (Sink& sink);
{ Journal (Journal const& other);
} ~Journal ();
bool reportActive (Severity severity) const /** Returns a stream for this sink, with the specified severity. */
{ Stream stream (Severity severity) const;
return m_sink->active (severity);
}
bool traceActive () const /** Returns `true` if the sink logs messages at that severity. */
{ bool active (Severity severity) const;
return reportActive (kTrace);
}
bool debugActive () const /** Convenience sink streams for each severity level. */
{ Stream const trace;
return reportActive (kDebug); Stream const debug;
} Stream const info;
Stream const warning;
bool infoActive () const Stream const error;
{ Stream const fatal;
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);
}
private: private:
Sink* m_sink; Journal& operator= (Journal const& other); // disallowed
}; };
#endif #endif

View File

@@ -69,7 +69,7 @@ public:
// //
void addStatic (Source* source) void addStatic (Source* source)
{ {
m_journal.info() << "Add static Source, " << source->name(); m_journal.info << "Add static Source, " << source->name();
ScopedPointer <Source> object (source); ScopedPointer <Source> object (source);
@@ -91,7 +91,7 @@ public:
// //
void add (Source* source) void add (Source* source)
{ {
m_journal.info() << "Add Source, " << source->name(); m_journal.info << "Add Source, " << source->name();
SourceDesc& desc (*m_sources.emplace_back ()); SourceDesc& desc (*m_sources.emplace_back ());
desc.source = source; desc.source = source;
@@ -159,7 +159,7 @@ public:
// This is thread safe // This is thread safe
m_chosenList = list; m_chosenList = list;
m_journal.debug() << m_journal.debug <<
"Rebuilt chosen list with " << "Rebuilt chosen list with " <<
String::fromNumber (m_chosenList->size()) << " entries"; String::fromNumber (m_chosenList->size()) << " entries";
} }
@@ -196,7 +196,7 @@ public:
/** Perform a fetch on the source. */ /** Perform a fetch on the source. */
void fetch (SourceDesc& desc, CancelCallback& callback) 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)); Source::Result result (desc.source->fetch (callback, m_journal));

View File

@@ -192,7 +192,7 @@ public:
if (error) if (error)
{ {
m_journal.fatal() << m_journal.fatal <<
"Failed to open '" << file.getFullPathName() << "'"; "Failed to open '" << file.getFullPathName() << "'";
} }

View File

@@ -48,7 +48,7 @@ public:
} }
else else
{ {
journal.error() << journal.error <<
"HTTP GET to " << m_url.full().toStdString() << "HTTP GET to " << m_url.full().toStdString() <<
" failed: '" << httpResult.error.message () << "'"; " failed: '" << httpResult.error.message () << "'";
} }

View File

@@ -160,7 +160,7 @@ void StoreSqdb::report (Error const& error, char const* fileName, int lineNumber
{ {
if (error) if (error)
{ {
m_journal.error() << m_journal.error <<
"Failure: '"<< error.getReasonText() << "' " << "Failure: '"<< error.getReasonText() << "' " <<
" at " << Debug::getSourceLocation (fileName, lineNumber); " at " << Debug::getSourceLocation (fileName, lineNumber);
} }