mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Journal improvements
This commit is contained in:
@@ -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 <NullSink>::get (
|
||||
return *SharedSingleton <NullSink>::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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 ();
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Scoped container for building journal messages. */
|
||||
class Stream
|
||||
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 <typename T>
|
||||
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 <typename T>
|
||||
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
|
||||
{
|
||||
return m_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 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
|
||||
|
||||
@@ -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 <Source> 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));
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ public:
|
||||
|
||||
if (error)
|
||||
{
|
||||
m_journal.fatal() <<
|
||||
m_journal.fatal <<
|
||||
"Failed to open '" << file.getFullPathName() << "'";
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
}
|
||||
else
|
||||
{
|
||||
journal.error() <<
|
||||
journal.error <<
|
||||
"HTTP GET to " << m_url.full().toStdString() <<
|
||||
" failed: '" << httpResult.error.message () << "'";
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user