From 5e060a9e7bbbd0bc1d5d085f8642bd786886cf78 Mon Sep 17 00:00:00 2001 From: JCW Date: Mon, 1 Sep 2025 11:59:50 +0100 Subject: [PATCH] Fix Signed-off-by: JCW --- include/xrpl/beast/utility/Journal.h | 128 +++++++++++++------- src/libxrpl/beast/utility/beast_Journal.cpp | 62 +++------- 2 files changed, 103 insertions(+), 87 deletions(-) diff --git a/include/xrpl/beast/utility/Journal.h b/include/xrpl/beast/utility/Journal.h index 4e4bd3ae3f..c3e0012aef 100644 --- a/include/xrpl/beast/utility/Journal.h +++ b/include/xrpl/beast/utility/Journal.h @@ -22,7 +22,13 @@ #include +#include +#include +#include + #include +#include +#include #include #include @@ -174,23 +180,27 @@ public: class JsonLogContext { - rapidjson::Value messageParams_; - rapidjson::MemoryPoolAllocator<> allocator_; + rapidjson::StringBuffer buffer_; + rapidjson::Writer messageParamsWriter_; std::optional journalAttributesJson_; public: - JsonLogContext() = default; - - rapidjson::MemoryPoolAllocator<>& - allocator() + JsonLogContext() + : messageParamsWriter_(buffer_) { - return allocator_; + } - rapidjson::Value& + rapidjson::Writer& + writer() + { + return messageParamsWriter_; + } + + char const* messageParams() { - return messageParams_; + return buffer_.GetString(); } std::optional& @@ -461,9 +471,28 @@ public: /** Journal has no default constructor. */ Journal() = delete; + template Journal( Journal const& other, - std::optional attributes = std::nullopt); + TAttributesFactory&& attributesFactory = nullptr) + : m_attributes(other.m_attributes) + , m_sink(other.m_sink) + , m_attributesJson(other.m_attributesJson) + { +/* + if constexpr (!std::is_same_v, std::nullptr_t>) + { + if (attributes.has_value()) + { + if (m_attributes) + m_attributes->combine(attributes->contextValues_); + else + m_attributes = std::move(attributes); + } + rebuildAttributeJson(); + } +*/ + } /** Create a journal that writes to the specified sink. */ explicit Journal( @@ -699,39 +728,59 @@ using logwstream = basic_logstream; namespace ripple::log { namespace detail { -template +template void setJsonValue( - rapidjson::Value& object, - rapidjson::MemoryPoolAllocator<>& allocator, + rapidjson::Writer& writer, char const* name, T&& value, std::ostream* outStream) { using ValueType = std::decay_t; - rapidjson::Value jsonValue; - if constexpr (std::constructible_from< - rapidjson::Value, - ValueType, - rapidjson::MemoryPoolAllocator<>&>) + writer.Key(name); + if constexpr (std::is_integral_v) { - jsonValue = rapidjson::Value{value, allocator}; + if constexpr (std::is_signed_v) + { + writer.Int64(value); + } + else + { + writer.Uint64(value); + } if (outStream) { (*outStream) << value; } } - else if constexpr (std::constructible_from) + else if constexpr (std::is_floating_point_v) { - jsonValue = rapidjson::Value{value}; + writer.Double(value); + if (outStream) { (*outStream) << value; } } - else if constexpr (std::same_as) + else if constexpr (std::is_same_v) { - jsonValue = rapidjson::Value{value.c_str(), allocator}; + writer.Bool(value); + if (outStream) + { + (*outStream) << value; + } + } + else if constexpr (std::is_same_v || std::is_same_v) + { + writer.String(value); + if (outStream) + { + (*outStream) << value; + } + } + else if constexpr (std::is_same_v) + { + writer.String(value.c_str(), value.length()); if (outStream) { (*outStream) << value; @@ -742,17 +791,13 @@ setJsonValue( std::ostringstream oss; oss << value; - jsonValue = rapidjson::Value{oss.str().c_str(), allocator}; + writer.String(oss.str().c_str(), oss.str().length()); if (outStream) { (*outStream) << oss.str(); } } - - object.RemoveMember(name); - object.AddMember( - rapidjson::StringRef(name), std::move(jsonValue), allocator); } } // namespace detail @@ -763,8 +808,7 @@ operator<<(std::ostream& os, LogParameter const& param) if (!beast::Journal::m_jsonLogsEnabled) return os; detail::setJsonValue( - beast::Journal::currentJsonLogContext_.messageParams(), - beast::Journal::currentJsonLogContext_.allocator(), + beast::Journal::currentJsonLogContext_.writer(), param.name_, param.value_, &os); @@ -778,8 +822,7 @@ operator<<(std::ostream& os, LogField const& param) if (!beast::Journal::m_jsonLogsEnabled) return os; detail::setJsonValue( - beast::Journal::currentJsonLogContext_.messageParams(), - beast::Journal::currentJsonLogContext_.allocator(), + beast::Journal::currentJsonLogContext_.writer(), param.name_, param.value_, nullptr); @@ -801,20 +844,17 @@ field(char const* name, T&& value) } template -[[nodiscard]] beast::Journal::JsonLogAttributes +[[nodiscard]] auto attributes(Pair&&... pairs) { - beast::Journal::JsonLogAttributes result; - - (detail::setJsonValue( - result.contextValues(), - result.allocator(), - pairs.first, - pairs.second, - nullptr), - ...); - - return result; + return [&](rapidjson::Writer>& writer) { + (detail::setJsonValue( + writer, + pairs.first, + pairs.second, + nullptr), + ...); + }; } template diff --git a/src/libxrpl/beast/utility/beast_Journal.cpp b/src/libxrpl/beast/utility/beast_Journal.cpp index b523477cf7..b900cd5e40 100644 --- a/src/libxrpl/beast/utility/beast_Journal.cpp +++ b/src/libxrpl/beast/utility/beast_Journal.cpp @@ -171,21 +171,6 @@ Journal::JsonLogAttributes::combine( } } -Journal::Journal( - Journal const& other, - std::optional attributes) - : m_attributes(other.m_attributes), m_sink(other.m_sink) -{ - if (attributes.has_value()) - { - if (m_attributes) - m_attributes->combine(attributes->contextValues_); - else - m_attributes = std::move(attributes); - } - rebuildAttributeJson(); -} - void Journal::addGlobalAttributes(JsonLogAttributes globalLogAttributes) { @@ -224,40 +209,30 @@ Journal::JsonLogContext::reset( journalAttributesJson_ = journalAttributesJson; - messageParams_.SetObject(); + buffer_.Clear(); - messageParams_.AddMember( - rapidjson::StringRef("Function"), - rapidjson::Value{location.function_name(), allocator_}, - allocator_); + writer().StartObject(); - messageParams_.AddMember( - rapidjson::StringRef("File"), - rapidjson::Value{location.file_name(), allocator_}, - allocator_); + writer().Key("Function"); + writer().String(location.function_name()); - messageParams_.AddMember( - rapidjson::StringRef("Line"), - location.line(), - allocator_); + writer().Key("File"); + writer().String(location.file_name()); - messageParams_.AddMember( - rapidjson::StringRef("ThreadId"), - rapidjson::Value{threadId.value.c_str(), allocator_}, - allocator_); + writer().Key("Line"); + writer().Int64(location.line()); + + writer().Key("ThreadId"); + writer().String(threadId.value.c_str()); auto severityStr = to_string(severity); - messageParams_.AddMember( - rapidjson::StringRef("Level"), - rapidjson::Value{severityStr.c_str(), allocator_}, - allocator_); + writer().Key("Level"); + writer().String(severityStr.c_str()); - messageParams_.AddMember( - rapidjson::StringRef("Time"), - std::chrono::duration_cast( + writer().Key("Time"); + writer().Uint64(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) - .count(), - allocator_); + .count()); } void @@ -276,7 +251,8 @@ Journal::formatLog(std::string&& message) return message; } - auto& messageParams = currentJsonLogContext_.messageParams(); + currentJsonLogContext_.writer().EndObject(); + auto messageParams = currentJsonLogContext_.messageParams(); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); @@ -301,7 +277,7 @@ Journal::formatLog(std::string&& message) } writer.Key("MessageParams"); - messageParams.Accept(writer); + writer.RawValue(messageParams, std::strlen(messageParams), rapidjson::kObjectType); writer.Key("Message"); writer.String(message.c_str(), message.length());