Journal API improvements

This commit is contained in:
Vinnie Falco
2013-11-15 12:23:14 -08:00
parent 71db1dfa06
commit b38dd98e71
4 changed files with 72 additions and 47 deletions

View File

@@ -20,10 +20,11 @@
#ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED #ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED
#define BEAST_UTILITY_JOURNAL_H_INCLUDED #define BEAST_UTILITY_JOURNAL_H_INCLUDED
#include "../SafeBool.h"
#include <sstream> #include <sstream>
namespace beast namespace beast {
{
/** A generic endpoint for log messages. */ /** A generic endpoint for log messages. */
class Journal class Journal
@@ -57,20 +58,26 @@ public:
public: public:
virtual ~Sink () { } virtual ~Sink () { }
/** 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. */ /** Returns `true` if text at the passed severity produces output. */
virtual bool active (Severity severity) = 0; virtual bool active (Severity severity) const = 0;
/** Returns `true` if a message is also written to the Output Window (MSVC). */ /** Returns `true` if a message is also written to the Output Window (MSVC). */
virtual bool console () = 0; virtual bool console () const = 0;
/** Set the minimum severity this sink will report. */
virtual void set_severity (Severity severity) = 0;
/** Set whether messages are also written to the Output Window (MSVC). */ /** Set whether messages are also written to the Output Window (MSVC). */
virtual void set_console (bool to_console) = 0; virtual void console (bool output) = 0;
/** Returns the minimum severity level this sink will report. */
virtual Severity severity() const = 0;
/** Set the minimum severity this sink will report. */
virtual void severity (Severity level) = 0;
/** Write text to the sink at the specified severity.
The caller is responsible for checking the minimum severity level
before using this function.
*/
virtual void write (Severity level, std::string const& text) = 0;
}; };
/** Returns a Sink which does nothing. */ /** Returns a Sink which does nothing. */
@@ -122,7 +129,7 @@ public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
class Stream class Stream : public SafeBool <Stream>
{ {
public: public:
/** Construct a stream which produces no logging output. */ /** Construct a stream which produces no logging output. */
@@ -144,7 +151,10 @@ public:
Severity severity() const; Severity severity() const;
/** Returns `true` if sink logs anything at this stream's severity. */ /** Returns `true` if sink logs anything at this stream's severity. */
/** @{ */
bool active() const; bool active() const;
bool asBoolean() const;
/** @} */
/** Output stream support. */ /** Output stream support. */
/** @{ */ /** @{ */

View File

@@ -19,8 +19,7 @@
#include "../Journal.h" #include "../Journal.h"
namespace beast namespace beast {
{
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -28,27 +27,32 @@ namespace beast
class NullJournalSink : public Journal::Sink class NullJournalSink : public Journal::Sink
{ {
public: public:
bool active (Journal::Severity) const
{
return false;
}
bool console() const
{
return false;
}
void console (bool)
{
}
Journal::Severity severity() const
{
return Journal::kDisabled;
}
void severity (Journal::Severity)
{
}
void write (Journal::Severity, std::string const&) void write (Journal::Severity, std::string const&)
{ {
} }
bool active (Journal::Severity)
{
return false;
}
bool console ()
{
return false;
}
void set_severity (Journal::Severity)
{
}
void set_console (bool)
{
}
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -85,7 +89,7 @@ Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip (
Journal::ScopedStream::~ScopedStream () Journal::ScopedStream::~ScopedStream ()
{ {
if (! m_ostream.str().empty()) if (! m_ostream.str().empty() && m_sink.active (m_severity))
m_sink.write (m_severity, m_ostream.str()); m_sink.write (m_severity, m_ostream.str());
} }
@@ -146,6 +150,11 @@ bool Journal::Stream::active () const
return m_sink->active (m_severity); return m_sink->active (m_severity);
} }
bool Journal::Stream::asBoolean () const
{
return active();
}
Journal::Stream& Journal::Stream::operator= (Stream const& other) Journal::Stream& Journal::Stream::operator= (Stream const& other)
{ {
m_sink = other.m_sink; m_sink = other.m_sink;

View File

@@ -234,27 +234,32 @@ UnitTests::JournalSink::JournalSink (UnitTests& tests)
{ {
} }
void UnitTests::JournalSink::write (Journal::Severity, std::string const& text) bool UnitTests::JournalSink::active (Journal::Severity) const
{
m_tests.logMessage (text);
}
bool UnitTests::JournalSink::active (Journal::Severity)
{ {
return true; return true;
} }
bool UnitTests::JournalSink::console () bool UnitTests::JournalSink::console() const
{ {
return false; return false;
} }
void UnitTests::JournalSink::set_severity (Journal::Severity) void UnitTests::JournalSink::console (bool)
{ {
} }
void UnitTests::JournalSink::set_console (bool) Journal::Severity UnitTests::JournalSink::severity() const
{ {
return Journal::kLowestSeverity;
}
void UnitTests::JournalSink::severity (Journal::Severity)
{
}
void UnitTests::JournalSink::write (Journal::Severity, std::string const& text)
{
m_tests.logMessage (text);
} }
//============================================================================== //==============================================================================

View File

@@ -517,11 +517,12 @@ private:
{ {
public: public:
explicit JournalSink (UnitTests& tests); explicit JournalSink (UnitTests& tests);
bool active (Journal::Severity severity) const;
bool console () const;
void console (bool);
Journal::Severity severity() const;
void severity (Journal::Severity severity);
void write (Journal::Severity severity, std::string const& text); void write (Journal::Severity severity, std::string const& text);
bool active (Journal::Severity severity);
bool console ();
void set_severity (Journal::Severity severity);
void set_console (bool);
private: private:
UnitTests& m_tests; UnitTests& m_tests;