mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
performance optimisation
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
@@ -144,8 +144,8 @@ public:
|
||||
void
|
||||
setModuleName(std::string const& name);
|
||||
|
||||
[[nodiscard]] static JsonLogAttributes
|
||||
combine(AttributeFields const& a, AttributeFields const& b);
|
||||
void
|
||||
combine(AttributeFields const& from);
|
||||
|
||||
AttributeFields&
|
||||
contextValues()
|
||||
@@ -172,22 +172,37 @@ public:
|
||||
friend class Journal;
|
||||
};
|
||||
|
||||
struct JsonLogContext
|
||||
class JsonLogContext
|
||||
{
|
||||
std::source_location location = {};
|
||||
rapidjson::Value messageParams;
|
||||
rapidjson::MemoryPoolAllocator<> allocator;
|
||||
rapidjson::Value attributes_;
|
||||
rapidjson::MemoryPoolAllocator<> allocator_;
|
||||
|
||||
public:
|
||||
JsonLogContext() = default;
|
||||
|
||||
void
|
||||
reset(std::source_location location_) noexcept
|
||||
rapidjson::MemoryPoolAllocator<>&
|
||||
allocator()
|
||||
{
|
||||
location = location_;
|
||||
messageParams = rapidjson::Value{};
|
||||
messageParams.SetObject();
|
||||
allocator.Clear();
|
||||
return allocator_;
|
||||
}
|
||||
|
||||
rapidjson::Value&
|
||||
messageParams()
|
||||
{
|
||||
return attributes_["Params"];
|
||||
}
|
||||
|
||||
rapidjson::Value&
|
||||
attributes()
|
||||
{
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
void
|
||||
reset(
|
||||
std::source_location location,
|
||||
severities::Severity severity,
|
||||
std::optional<JsonLogAttributes> const& attributes) noexcept;
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -204,14 +219,13 @@ private:
|
||||
// Invariant: m_sink always points to a valid Sink
|
||||
Sink* m_sink = nullptr;
|
||||
|
||||
static void
|
||||
initMessageContext(std::source_location location);
|
||||
void
|
||||
initMessageContext(
|
||||
std::source_location location,
|
||||
severities::Severity severity) const;
|
||||
|
||||
static std::string
|
||||
formatLog(
|
||||
std::string const& message,
|
||||
severities::Severity severity,
|
||||
std::optional<JsonLogAttributes> const& attributes = std::nullopt);
|
||||
formatLog(std::string const& message);
|
||||
|
||||
public:
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -304,25 +318,16 @@ public:
|
||||
{
|
||||
public:
|
||||
ScopedStream(ScopedStream const& other)
|
||||
: ScopedStream(other.m_attributes, other.m_sink, other.m_level)
|
||||
: ScopedStream(other.m_sink, other.m_level)
|
||||
{
|
||||
}
|
||||
|
||||
ScopedStream(
|
||||
std::optional<JsonLogAttributes> attributes,
|
||||
Sink& sink,
|
||||
Severity level);
|
||||
ScopedStream(Sink& sink, Severity level);
|
||||
|
||||
template <typename T>
|
||||
ScopedStream(
|
||||
std::optional<JsonLogAttributes> attributes,
|
||||
Stream const& stream,
|
||||
T const& t);
|
||||
ScopedStream(Stream const& stream, T const& t);
|
||||
|
||||
ScopedStream(
|
||||
std::optional<JsonLogAttributes> attributes,
|
||||
Stream const& stream,
|
||||
std::ostream& manip(std::ostream&));
|
||||
ScopedStream(Stream const& stream, std::ostream& manip(std::ostream&));
|
||||
|
||||
ScopedStream&
|
||||
operator=(ScopedStream const&) = delete;
|
||||
@@ -343,7 +348,6 @@ public:
|
||||
operator<<(T const& t) const;
|
||||
|
||||
private:
|
||||
std::optional<JsonLogAttributes> m_attributes;
|
||||
Sink& m_sink;
|
||||
Severity const m_level;
|
||||
std::ostringstream mutable m_ostream;
|
||||
@@ -378,11 +382,7 @@ public:
|
||||
|
||||
Constructor is inlined so checking active() very inexpensive.
|
||||
*/
|
||||
Stream(
|
||||
std::optional<JsonLogAttributes> attributes,
|
||||
Sink& sink,
|
||||
Severity level)
|
||||
: m_attributes(std::move(attributes)), m_sink(sink), m_level(level)
|
||||
Stream(Sink& sink, Severity level) : m_sink(sink), m_level(level)
|
||||
{
|
||||
XRPL_ASSERT(
|
||||
m_level < severities::kDisabled,
|
||||
@@ -390,8 +390,7 @@ public:
|
||||
}
|
||||
|
||||
/** Construct or copy another Stream. */
|
||||
Stream(Stream const& other)
|
||||
: Stream(other.m_attributes, other.m_sink, other.m_level)
|
||||
Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -438,7 +437,6 @@ public:
|
||||
/** @} */
|
||||
|
||||
private:
|
||||
std::optional<JsonLogAttributes> m_attributes;
|
||||
Sink& m_sink;
|
||||
Severity m_level;
|
||||
};
|
||||
@@ -467,9 +465,8 @@ public:
|
||||
if (other.m_attributes.has_value())
|
||||
{
|
||||
if (m_attributes.has_value())
|
||||
m_attributes = JsonLogAttributes::combine(
|
||||
other.m_attributes->contextValues_,
|
||||
m_attributes->contextValues_);
|
||||
m_attributes->combine(
|
||||
other.m_attributes->contextValues_);
|
||||
else
|
||||
m_attributes = other.m_attributes;
|
||||
}
|
||||
@@ -516,9 +513,11 @@ public:
|
||||
|
||||
/** Returns a stream for this sink, with the specified severity level. */
|
||||
Stream
|
||||
stream(Severity level) const
|
||||
stream(Severity level, std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
return Stream(m_attributes, *m_sink, level);
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location, level);
|
||||
return Stream(*m_sink, level);
|
||||
}
|
||||
|
||||
/** Returns `true` if any message would be logged at this severity level.
|
||||
@@ -537,24 +536,24 @@ public:
|
||||
trace(std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kTrace};
|
||||
initMessageContext(location, severities::kTrace);
|
||||
return {*m_sink, severities::kTrace};
|
||||
}
|
||||
|
||||
Stream
|
||||
debug(std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kDebug};
|
||||
initMessageContext(location, severities::kDebug);
|
||||
return {*m_sink, severities::kDebug};
|
||||
}
|
||||
|
||||
Stream
|
||||
info(std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kInfo};
|
||||
initMessageContext(location, severities::kInfo);
|
||||
return {*m_sink, severities::kInfo};
|
||||
}
|
||||
|
||||
Stream
|
||||
@@ -563,24 +562,24 @@ public:
|
||||
char const* a = "a";
|
||||
rapidjson::Value v{a, 1};
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kWarning};
|
||||
initMessageContext(location, severities::kWarning);
|
||||
return {*m_sink, severities::kWarning};
|
||||
}
|
||||
|
||||
Stream
|
||||
error(std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kError};
|
||||
initMessageContext(location, severities::kError);
|
||||
return {*m_sink, severities::kError};
|
||||
}
|
||||
|
||||
Stream
|
||||
fatal(std::source_location location = std::source_location::current()) const
|
||||
{
|
||||
if (m_jsonLogsEnabled)
|
||||
initMessageContext(location);
|
||||
return {m_attributes, *m_sink, severities::kFatal};
|
||||
initMessageContext(location, severities::kFatal);
|
||||
return {*m_sink, severities::kFatal};
|
||||
}
|
||||
/** @} */
|
||||
|
||||
@@ -599,8 +598,7 @@ public:
|
||||
{
|
||||
globalLogAttributes_ = JsonLogAttributes{};
|
||||
}
|
||||
globalLogAttributes_ = JsonLogAttributes::combine(
|
||||
globalLogAttributes_->contextValues(),
|
||||
globalLogAttributes_->combine(
|
||||
globalLogAttributes.contextValues());
|
||||
}
|
||||
};
|
||||
@@ -617,11 +615,8 @@ static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
Journal::ScopedStream::ScopedStream(
|
||||
std::optional<JsonLogAttributes> attributes,
|
||||
Stream const& stream,
|
||||
T const& t)
|
||||
: ScopedStream(std::move(attributes), stream.sink(), stream.level())
|
||||
Journal::ScopedStream::ScopedStream(Stream const& stream, T const& t)
|
||||
: ScopedStream(stream.sink(), stream.level())
|
||||
{
|
||||
m_ostream << t;
|
||||
}
|
||||
@@ -640,7 +635,7 @@ template <typename T>
|
||||
Journal::ScopedStream
|
||||
Journal::Stream::operator<<(T const& t) const
|
||||
{
|
||||
return {m_attributes, *this, t};
|
||||
return {*this, t};
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
@@ -766,6 +761,7 @@ setJsonValue(
|
||||
}
|
||||
}
|
||||
|
||||
object.RemoveMember(name);
|
||||
object.AddMember(
|
||||
rapidjson::StringRef(name), std::move(jsonValue), allocator);
|
||||
}
|
||||
@@ -778,8 +774,8 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
|
||||
if (!beast::Journal::m_jsonLogsEnabled)
|
||||
return os;
|
||||
detail::setJsonValue(
|
||||
beast::Journal::currentJsonLogContext_.messageParams,
|
||||
beast::Journal::currentJsonLogContext_.allocator,
|
||||
beast::Journal::currentJsonLogContext_.messageParams(),
|
||||
beast::Journal::currentJsonLogContext_.allocator(),
|
||||
param.name_,
|
||||
param.value_,
|
||||
&os);
|
||||
@@ -793,8 +789,8 @@ operator<<(std::ostream& os, LogField<T> const& param)
|
||||
if (!beast::Journal::m_jsonLogsEnabled)
|
||||
return os;
|
||||
detail::setJsonValue(
|
||||
beast::Journal::currentJsonLogContext_.messageParams,
|
||||
beast::Journal::currentJsonLogContext_.allocator,
|
||||
beast::Journal::currentJsonLogContext_.messageParams(),
|
||||
beast::Journal::currentJsonLogContext_.allocator(),
|
||||
param.name_,
|
||||
param.value_,
|
||||
nullptr);
|
||||
|
||||
Reference in New Issue
Block a user