mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
222 lines
5.0 KiB
C++
222 lines
5.0 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
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 <xrpl/beast/utility/Journal.h>
|
|
|
|
#include <ios>
|
|
#include <ostream>
|
|
#include <string>
|
|
|
|
namespace beast {
|
|
|
|
Journal::StructuredJournalImpl* Journal::m_structuredJournalImpl = nullptr;
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// A Sink that does nothing.
|
|
class NullJournalSink : public Journal::Sink
|
|
{
|
|
public:
|
|
NullJournalSink() : Sink(severities::kDisabled, false)
|
|
{
|
|
}
|
|
|
|
~NullJournalSink() override = default;
|
|
|
|
bool
|
|
active(severities::Severity) const override
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
console() const override
|
|
{
|
|
return false;
|
|
}
|
|
|
|
void
|
|
console(bool) override
|
|
{
|
|
}
|
|
|
|
severities::Severity
|
|
threshold() const override
|
|
{
|
|
return severities::kDisabled;
|
|
}
|
|
|
|
void
|
|
threshold(severities::Severity) override
|
|
{
|
|
}
|
|
|
|
void
|
|
write(severities::Severity, std::string const&) override
|
|
{
|
|
}
|
|
|
|
void
|
|
writeAlways(severities::Severity, std::string const&) override
|
|
{
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Journal::Sink&
|
|
Journal::getNullSink()
|
|
{
|
|
static NullJournalSink sink;
|
|
return sink;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string
|
|
severities::to_string(Severity severity)
|
|
{
|
|
switch (severity)
|
|
{
|
|
case kDisabled:
|
|
return "disabled";
|
|
case kTrace:
|
|
return "trace";
|
|
case kDebug:
|
|
return "debug";
|
|
case kInfo:
|
|
return "info";
|
|
case kWarning:
|
|
return "warning";
|
|
case kError:
|
|
return "error";
|
|
case kFatal:
|
|
return "fatal";
|
|
default:
|
|
UNREACHABLE("Unexpected severity value!");
|
|
}
|
|
return "";
|
|
|
|
}
|
|
Journal::Sink::Sink(Severity thresh, bool console)
|
|
: thresh_(thresh), m_console(console)
|
|
{
|
|
}
|
|
|
|
Journal::Sink::~Sink() = default;
|
|
|
|
bool
|
|
Journal::Sink::active(Severity level) const
|
|
{
|
|
return level >= thresh_;
|
|
}
|
|
|
|
bool
|
|
Journal::Sink::console() const
|
|
{
|
|
return m_console;
|
|
}
|
|
|
|
void
|
|
Journal::Sink::console(bool output)
|
|
{
|
|
m_console = output;
|
|
}
|
|
|
|
severities::Severity
|
|
Journal::Sink::threshold() const
|
|
{
|
|
return thresh_;
|
|
}
|
|
|
|
void
|
|
Journal::Sink::threshold(Severity thresh)
|
|
{
|
|
thresh_ = thresh;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Journal::ScopedStream::ScopedStream(
|
|
std::unique_ptr<StructuredLogAttributes> attributes,
|
|
Sink& sink,
|
|
Severity level)
|
|
: m_attributes(std::move(attributes)), m_sink(sink), m_level(level)
|
|
{
|
|
// Modifiers applied from all ctors
|
|
m_ostream << std::boolalpha << std::showbase;
|
|
}
|
|
|
|
Journal::ScopedStream::ScopedStream(
|
|
std::unique_ptr<StructuredLogAttributes> attributes,
|
|
Stream const& stream,
|
|
std::ostream& manip(std::ostream&))
|
|
: ScopedStream(std::move(attributes), stream.sink(), stream.level())
|
|
{
|
|
m_ostream << manip;
|
|
}
|
|
|
|
Journal::ScopedStream::~ScopedStream()
|
|
{
|
|
std::string const& s(m_ostream.str());
|
|
if (!s.empty())
|
|
{
|
|
if (s == "\n")
|
|
{
|
|
if (m_structuredJournalImpl)
|
|
{
|
|
m_structuredJournalImpl->flush(
|
|
&m_sink, m_level, "", m_attributes.get());
|
|
}
|
|
else
|
|
{
|
|
m_sink.write(m_level, "");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (m_structuredJournalImpl)
|
|
{
|
|
m_structuredJournalImpl->flush(
|
|
&m_sink, m_level, s, m_attributes.get());
|
|
}
|
|
else
|
|
{
|
|
m_sink.write(m_level, s);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
return {m_attributes ? m_attributes->clone() : nullptr, *this, manip};
|
|
}
|
|
|
|
} // namespace beast
|