Files
boost_beast/beast/utility/impl/Journal.cpp
T

156 lines
3.6 KiB
C++
Raw Normal View History

2013-09-12 13:50:15 -07:00
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/utility/Journal.h>
2016-01-20 12:32:02 -08:00
#include <cassert>
2013-11-15 12:23:14 -08:00
namespace beast {
2013-09-28 16:39:18 -07:00
//------------------------------------------------------------------------------
2013-09-27 19:39:31 -07:00
// A Sink that does nothing.
class NullJournalSink : public Journal::Sink
{
public:
2016-02-17 18:34:42 -08:00
NullJournalSink ()
: Sink (severities::kDisabled, false)
2016-02-17 18:34:42 -08:00
{ }
~NullJournalSink() override = default;
bool active (severities::Severity) const override
2013-11-15 12:23:14 -08:00
{
return false;
}
2016-02-17 18:34:42 -08:00
bool console() const override
2013-11-15 12:23:14 -08:00
{
return false;
}
2016-02-17 18:34:42 -08:00
void console (bool) override
2013-11-15 12:23:14 -08:00
{
}
severities::Severity threshold() const override
2013-11-15 12:23:14 -08:00
{
return severities::kDisabled;
2013-11-15 12:23:14 -08:00
}
void threshold (severities::Severity) override
2013-11-15 12:23:14 -08:00
{
}
void write (severities::Severity, std::string const&) override
{
}
};
2013-09-14 18:44:58 -07:00
//------------------------------------------------------------------------------
Journal::Sink& Journal::getNullSink ()
2013-09-12 13:50:15 -07:00
{
2015-10-06 12:38:26 -07:00
static NullJournalSink sink;
return sink;
2013-09-12 13:50:15 -07:00
}
2013-09-14 18:44:58 -07:00
//------------------------------------------------------------------------------
2016-02-17 18:34:42 -08:00
Journal::Sink::Sink (Severity thresh, bool console)
: thresh_ (thresh)
, m_console (console)
2013-11-30 11:00:21 -08:00
{
}
Journal::Sink::~Sink ()
{
}
bool Journal::Sink::active (Severity level) const
{
2016-02-17 18:34:42 -08:00
return level >= thresh_;
2013-11-30 11:00:21 -08:00
}
bool Journal::Sink::console () const
{
return m_console;
}
void Journal::Sink::console (bool output)
{
m_console = output;
}
severities::Severity Journal::Sink::threshold () const
2013-11-30 11:00:21 -08:00
{
2016-02-17 18:34:42 -08:00
return thresh_;
2013-11-30 11:00:21 -08:00
}
2016-02-17 18:34:42 -08:00
void Journal::Sink::threshold (Severity thresh)
2013-11-30 11:00:21 -08:00
{
2016-02-17 18:34:42 -08:00
thresh_ = thresh;
2013-11-30 11:00:21 -08:00
}
//------------------------------------------------------------------------------
Journal::ScopedStream::ScopedStream (Sink& sink, Severity level)
: m_sink (sink)
, m_level (level)
2013-09-14 18:44:58 -07:00
{
// Modifiers applied from all ctors
m_ostream
<< std::boolalpha
<< std::showbase;
2013-09-14 18:44:58 -07:00
}
Journal::ScopedStream::ScopedStream (
Stream const& stream, std::ostream& manip (std::ostream&))
: ScopedStream (stream.sink(), stream.level())
2013-09-14 18:44:58 -07:00
{
2016-02-17 18:34:42 -08:00
m_ostream << manip;
2013-09-14 18:44:58 -07:00
}
Journal::ScopedStream::~ScopedStream ()
{
2016-02-17 18:34:42 -08:00
std::string const& s (m_ostream.str());
if (! s.empty ())
{
2016-02-17 18:34:42 -08:00
if (s == "\n")
m_sink.write (m_level, "");
else
m_sink.write (m_level, s);
}
2013-09-14 18:44:58 -07:00
}
std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const
{
return m_ostream << manip;
}
//------------------------------------------------------------------------------
Journal::ScopedStream Journal::Stream::operator<< (
std::ostream& manip (std::ostream&)) const
2013-09-14 18:44:58 -07:00
{
return ScopedStream (*this, manip);
}
} // beast
2013-09-14 18:44:58 -07:00