Add console feature to Journal

This commit is contained in:
Vinnie Falco
2013-09-27 19:39:31 -07:00
parent ad0064a310
commit 3f6e7aa05a
2 changed files with 48 additions and 37 deletions

View File

@@ -61,6 +61,12 @@ public:
The default implementation always returns `true`. The default implementation always returns `true`.
*/ */
virtual bool active (Severity); virtual bool active (Severity);
/** Returns `true` if text at the severity goes to the Output window. */
virtual bool console ();
virtual void set_severity (Severity severity) = 0;
virtual void set_console (bool to_console) = 0;
}; };
/** Returns a Sink which does nothing. */ /** Returns a Sink which does nothing. */
@@ -123,11 +129,8 @@ public:
/** Returns `true` if the sink logs messages at the severity of this stream. */ /** Returns `true` if the sink logs messages at the severity of this stream. */
bool active() const; bool active() const;
/** Returns `true` if the stream also loggs messages to the Output window. */ /** Returns `true` if the stream also logs messages to the Output window. */
bool toOutputWindow() const; bool console() const;
/** Also outputs Stream messages to the Output window (MSVC-specific). */
void setOutputWindow (bool toOutputWindow) const;
Sink& sink() const; Sink& sink() const;
Severity severity() const; Severity severity() const;
@@ -143,7 +146,6 @@ public:
private: private:
Sink* m_sink; Sink* m_sink;
Severity m_severity; Severity m_severity;
bool mutable m_toOutputWindow;
}; };
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -156,11 +158,13 @@ public:
/** Returns a stream for this sink, with the specified severity. */ /** Returns a stream for this sink, with the specified severity. */
Stream stream (Severity severity) const; Stream stream (Severity severity) const;
/** Returns `true` if the sink logs messages at that severity. */ Sink& sink() const;
bool active (Severity severity) const;
/** Sets all streams to also log to the Output window (MSVC-specific). */ /** Returns `true` if the sink logs messages at that severity. */
void setOutputWindow (bool toOutputWindow) const; /** @{ */
bool active (Severity severity) const;
bool console () const;
/** @} */
/** Convenience sink streams for each severity level. */ /** Convenience sink streams for each severity level. */
Stream const trace; Stream const trace;

View File

@@ -27,6 +27,11 @@ bool Journal::Sink::active (Severity)
return true; return true;
} }
bool Journal::Sink::console ()
{
return false;
}
// A Sink that does nothing. // A Sink that does nothing.
class NullJournalSink : public Journal::Sink class NullJournalSink : public Journal::Sink
{ {
@@ -39,6 +44,19 @@ public:
{ {
return false; return false;
} }
bool console (Journal::Severity)
{
return false;
}
void set_severity (Journal::Severity)
{
}
void set_console (bool)
{
}
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -54,37 +72,34 @@ Journal::Sink& Journal::getNullSink ()
Journal::ScopedStream::ScopedStream (Stream const& stream) Journal::ScopedStream::ScopedStream (Stream const& stream)
: m_sink (stream.sink()) : m_sink (stream.sink())
, m_severity (stream.severity()) , m_severity (stream.severity())
, m_toOutputWindow (stream.toOutputWindow())
{ {
} }
Journal::ScopedStream::ScopedStream (ScopedStream const& other) Journal::ScopedStream::ScopedStream (ScopedStream const& other)
: m_sink (other.m_sink) : m_sink (other.m_sink)
, m_severity (other.m_severity) , m_severity (other.m_severity)
, m_toOutputWindow (other.m_toOutputWindow)
{ {
} }
Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&)) Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&))
: m_sink (stream.sink()) : m_sink (stream.sink())
, m_severity (stream.severity()) , m_severity (stream.severity())
, m_toOutputWindow (stream.toOutputWindow())
{ {
m_ostream << manip; m_ostream << manip;
} }
Journal::ScopedStream::~ScopedStream () Journal::ScopedStream::~ScopedStream ()
{ {
if (m_sink.active (m_severity)) if (! m_ostream.str().empty())
{ {
if (! m_ostream.str().empty()) if (m_sink.active (m_severity))
m_sink.write (m_severity, m_ostream.str()); m_sink.write (m_severity, m_ostream.str());
}
#if BEAST_MSVC #if BEAST_MSVC
if (m_toOutputWindow && beast_isRunningUnderDebugger ()) if (m_sink.console () && beast_isRunningUnderDebugger ())
Logger::outputDebugString (m_ostream.str()); Logger::outputDebugString (m_ostream.str());
#endif #endif
}
} }
std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const
@@ -102,21 +117,18 @@ std::ostringstream& Journal::ScopedStream::ostream () const
Journal::Stream::Stream () Journal::Stream::Stream ()
: m_sink (&getNullSink ()) : m_sink (&getNullSink ())
, m_severity (kFatal) , m_severity (kFatal)
, m_toOutputWindow (false)
{ {
} }
Journal::Stream::Stream (Sink& sink, Severity severity) Journal::Stream::Stream (Sink& sink, Severity severity)
: m_sink (&sink) : m_sink (&sink)
, m_severity (severity) , m_severity (severity)
, m_toOutputWindow (false)
{ {
} }
Journal::Stream::Stream (Stream const& other) Journal::Stream::Stream (Stream const& other)
: m_sink (other.m_sink) : m_sink (other.m_sink)
, m_severity (other.m_severity) , m_severity (other.m_severity)
, m_toOutputWindow (other.m_toOutputWindow)
{ {
} }
@@ -125,14 +137,9 @@ bool Journal::Stream::active () const
return m_sink->active (m_severity); return m_sink->active (m_severity);
} }
bool Journal::Stream::toOutputWindow() const bool Journal::Stream::console() const
{ {
return m_toOutputWindow; return m_sink->console ();
}
void Journal::Stream::setOutputWindow (bool toOutputWindow) const
{
m_toOutputWindow = toOutputWindow;
} }
Journal::Sink& Journal::Stream::sink () const Journal::Sink& Journal::Stream::sink () const
@@ -201,20 +208,20 @@ Journal::Stream Journal::stream (Severity severity) const
return Stream (*m_sink, severity); return Stream (*m_sink, severity);
} }
Journal::Sink& Journal::sink() const
{
return *m_sink;
}
/** Returns `true` if the sink logs messages at that severity. */ /** Returns `true` if the sink logs messages at that severity. */
bool Journal::active (Severity severity) const bool Journal::active (Severity severity) const
{ {
return m_sink->active (severity); return m_sink->active (severity);
} }
void Journal::setOutputWindow (bool toOutputWindow) const bool Journal::console () const
{ {
trace.setOutputWindow (toOutputWindow); return m_sink->console ();
debug.setOutputWindow (toOutputWindow);
info.setOutputWindow (toOutputWindow);
warning.setOutputWindow (toOutputWindow);
fatal.setOutputWindow (toOutputWindow);
trace.setOutputWindow (toOutputWindow);
} }
} }