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`.
*/
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. */
@@ -123,11 +129,8 @@ public:
/** Returns `true` if the sink logs messages at the severity of this stream. */
bool active() const;
/** Returns `true` if the stream also loggs messages to the Output window. */
bool toOutputWindow() const;
/** Also outputs Stream messages to the Output window (MSVC-specific). */
void setOutputWindow (bool toOutputWindow) const;
/** Returns `true` if the stream also logs messages to the Output window. */
bool console() const;
Sink& sink() const;
Severity severity() const;
@@ -143,7 +146,6 @@ public:
private:
Sink* m_sink;
Severity m_severity;
bool mutable m_toOutputWindow;
};
//--------------------------------------------------------------------------
@@ -156,11 +158,13 @@ public:
/** Returns a stream for this sink, with the specified severity. */
Stream stream (Severity severity) const;
/** Returns `true` if the sink logs messages at that severity. */
bool active (Severity severity) const;
Sink& sink() const;
/** Sets all streams to also log to the Output window (MSVC-specific). */
void setOutputWindow (bool toOutputWindow) const;
/** Returns `true` if the sink logs messages at that severity. */
/** @{ */
bool active (Severity severity) const;
bool console () const;
/** @} */
/** Convenience sink streams for each severity level. */
Stream const trace;

View File

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