Compare commits

..

6 Commits

Author SHA1 Message Date
JCW
ddb103759f Fix issues
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:59:52 +01:00
JCW
1ff964a14a Fix issues
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:16:57 +01:00
JCW
8025cfad8d Fix issues
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:16:32 +01:00
JCW
8338a314e9 Optimisation
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:16:31 +01:00
JCW
bf7fcf3d39 Fix issues
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:16:29 +01:00
JCW
013cbac722 performance optimisation
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-30 20:15:00 +01:00
5 changed files with 152 additions and 87 deletions

View File

@@ -52,7 +52,6 @@ target_link_libraries(xrpl.libpb
add_library(xrpl.imports.main INTERFACE)
find_package(RapidJSON)
find_package(yyjson)
target_link_libraries(xrpl.imports.main
INTERFACE
@@ -78,7 +77,7 @@ add_module(xrpl beast)
target_link_libraries(xrpl.libxrpl.beast PUBLIC
xrpl.imports.main
xrpl.libpb
yyjson::yyjson
rapidjson
)
# Level 02

View File

@@ -30,8 +30,7 @@ class Xrpl(ConanFile):
'openssl/3.5.2',
'soci/4.0.3',
'zlib/1.3.1',
"rapidjson/1.1.0",
"yyjson/0.10.0"
"rapidjson/1.1.0"
]
test_requires = [

View File

@@ -22,9 +22,9 @@
#include <xrpl/beast/utility/instrumentation.h>
#include <boost/json.hpp>
#include <rapidjson/document.h>
#include <memory>
#include <deque>
#include <optional>
#include <source_location>
#include <sstream>
@@ -97,7 +97,7 @@ enum Severity {
kNone = kDisabled
};
std::string_view
std::string
to_string(Severity severity);
} // namespace severities
@@ -129,21 +129,18 @@ public:
ripple::log::LogParameter<T> const& param);
class Sink;
class JsonLogAttributes
{
public:
using AttributeFields = boost::json::value;
using AttributeFields = rapidjson::Value;
JsonLogAttributes();
JsonLogAttributes(JsonLogAttributes const& other);
JsonLogAttributes(JsonLogAttributes&& other);
JsonLogAttributes&
operator=(JsonLogAttributes const& other);
JsonLogAttributes&
operator=(JsonLogAttributes&& other);
void
setModuleName(std::string const& name);
@@ -162,38 +159,45 @@ public:
return contextValues_;
}
rapidjson::MemoryPoolAllocator<>&
allocator()
{
return allocator_;
}
private:
AttributeFields contextValues_;
rapidjson::MemoryPoolAllocator<> allocator_;
friend class Journal;
};
class JsonLogContext
{
std::source_location location_ = {};
boost::json::value attributes_;
rapidjson::Value attributes_;
rapidjson::MemoryPoolAllocator<> allocator_;
public:
JsonLogContext() = default;
boost::json::value&
messageParams()
rapidjson::MemoryPoolAllocator<>&
allocator()
{
return attributes_.as_object()["Params"];
return allocator_;
}
boost::json::value&
rapidjson::Value&
messageParams()
{
return attributes_["Params"];
}
rapidjson::Value&
attributes()
{
return attributes_;
}
std::source_location&
location()
{
return location_;
}
void
reset(
std::source_location location,
@@ -553,6 +557,8 @@ public:
Stream
warn(std::source_location location = std::source_location::current()) const
{
char const* a = "a";
rapidjson::Value v{a, 1};
if (m_jsonLogsEnabled)
initMessageContext(location, severities::kWarning);
return {*m_sink, severities::kWarning};
@@ -590,7 +596,8 @@ public:
{
globalLogAttributes_ = JsonLogAttributes{};
}
globalLogAttributes_->combine(globalLogAttributes.contextValues());
globalLogAttributes_->combine(
globalLogAttributes.contextValues());
}
};
@@ -701,21 +708,39 @@ using logwstream = basic_logstream<wchar_t>;
namespace ripple::log {
namespace detail {
template <typename T>
void
setJsonValue(
boost::json::value& object,
rapidjson::Value& object,
rapidjson::MemoryPoolAllocator<>& allocator,
char const* name,
T&& value,
std::ostream* outStream)
{
using ValueType = std::decay_t<T>;
auto& root = object.as_object();
if constexpr (std::constructible_from<boost::json::value, ValueType>)
rapidjson::Value jsonValue;
if constexpr (std::constructible_from<
rapidjson::Value,
ValueType,
rapidjson::MemoryPoolAllocator<>&>)
{
root[name] = std::forward<T>(value);
jsonValue = rapidjson::Value{value, allocator};
if (outStream)
{
(*outStream) << value;
}
}
else if constexpr (std::constructible_from<rapidjson::Value, ValueType>)
{
jsonValue = rapidjson::Value{value};
if (outStream)
{
(*outStream) << value;
}
}
else if constexpr (std::same_as<ValueType, std::string>)
{
jsonValue = rapidjson::Value{value.c_str(), allocator};
if (outStream)
{
(*outStream) << value;
@@ -726,13 +751,17 @@ setJsonValue(
std::ostringstream oss;
oss << value;
root[name] = oss.str();
jsonValue = rapidjson::Value{oss.str().c_str(), allocator};
if (outStream)
{
(*outStream) << oss.str();
}
}
object.RemoveMember(name);
object.AddMember(
rapidjson::StringRef(name), std::move(jsonValue), allocator);
}
} // namespace detail
@@ -744,6 +773,7 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
return os;
detail::setJsonValue(
beast::Journal::currentJsonLogContext_.messageParams(),
beast::Journal::currentJsonLogContext_.allocator(),
param.name_,
param.value_,
&os);
@@ -758,6 +788,7 @@ operator<<(std::ostream& os, LogField<T> const& param)
return os;
detail::setJsonValue(
beast::Journal::currentJsonLogContext_.messageParams(),
beast::Journal::currentJsonLogContext_.allocator(),
param.name_,
param.value_,
nullptr);
@@ -785,7 +816,11 @@ attributes(Pair&&... pairs)
beast::Journal::JsonLogAttributes result;
(detail::setJsonValue(
result.contextValues(), pairs.first, pairs.second, nullptr),
result.contextValues(),
result.allocator(),
pairs.first,
pairs.second,
nullptr),
...);
return result;

View File

@@ -19,6 +19,10 @@
#include <xrpl/beast/utility/Journal.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <ios>
#include <ostream>
#include <ranges>
@@ -94,7 +98,7 @@ Journal::getNullSink()
//------------------------------------------------------------------------------
std::string_view
std::string
severities::to_string(Severity severity)
{
switch (severity)
@@ -121,18 +125,13 @@ severities::to_string(Severity severity)
Journal::JsonLogAttributes::JsonLogAttributes()
{
contextValues_ = {};
contextValues_.emplace_object();
contextValues_.SetObject();
}
Journal::JsonLogAttributes::JsonLogAttributes(JsonLogAttributes const& other)
{
contextValues_ = other.contextValues_;
}
Journal::JsonLogAttributes::JsonLogAttributes(JsonLogAttributes&& other)
{
contextValues_ = std::move(other.contextValues_);
contextValues_.SetObject();
contextValues_.CopyFrom(other.contextValues_, allocator_);
}
Journal::JsonLogAttributes&
@@ -142,34 +141,31 @@ Journal::JsonLogAttributes::operator=(JsonLogAttributes const& other)
{
return *this;
}
contextValues_ = other.contextValues_;
return *this;
}
Journal::JsonLogAttributes&
Journal::JsonLogAttributes::operator=(JsonLogAttributes&& other)
{
if (&other == this)
{
return *this;
}
contextValues_ = std::move(other.contextValues_);
contextValues_.CopyFrom(other.contextValues_, allocator_);
return *this;
}
void
Journal::JsonLogAttributes::setModuleName(std::string const& name)
{
contextValues_.as_object()["Module"] = name;
contextValues_.AddMember(
rapidjson::StringRef("Module"),
rapidjson::Value{name.c_str(), allocator_},
allocator_);
}
void
Journal::JsonLogAttributes::combine(AttributeFields const& from)
Journal::JsonLogAttributes::combine(
AttributeFields const& from)
{
for (auto& [key, value] : from.as_object())
for (auto& member : from.GetObject())
{
contextValues_.as_object()[key] = value;
contextValues_.RemoveMember(member.name);
contextValues_.AddMember(
rapidjson::Value{member.name, allocator_},
rapidjson::Value{member.value, allocator_},
allocator_);
}
}
@@ -191,43 +187,72 @@ Journal::JsonLogContext::reset(
};
thread_local ThreadIdStringInitializer const threadId;
attributes_.emplace_null();
location_ = location;
attributes_.SetObject();
if (globalLogAttributes_.has_value())
{
attributes_ = globalLogAttributes_->contextValues();
attributes_.CopyFrom(globalLogAttributes_->contextValues(), allocator_);
if (attributes.has_value())
{
for (auto& [key, value] : attributes->contextValues().as_object())
for (auto const& [key, value] :
attributes->contextValues().GetObject())
{
attributes_.as_object()[key] = value;
attributes_.RemoveMember(key);
rapidjson::Value jsonValue;
jsonValue.CopyFrom(value, allocator_);
attributes_.AddMember(
rapidjson::Value{key, allocator_},
rapidjson::Value{value, allocator_},
allocator_);
}
}
}
else if (attributes.has_value())
{
attributes_ = attributes->contextValues();
attributes_.CopyFrom(attributes->contextValues(), allocator_);
}
if (attributes_.is_null())
{
attributes_ = {};
attributes_.emplace_object();
}
attributes_.RemoveMember("Function");
attributes_.AddMember(
rapidjson::StringRef("Function"),
rapidjson::Value{location.function_name(), allocator_},
allocator_);
attributes_.as_object()["Params"] = boost::json::object{};
attributes_.as_object()["Function"] =
currentJsonLogContext_.location().function_name();
attributes_.as_object()["File"] =
currentJsonLogContext_.location().file_name();
attributes_.as_object()["Line"] = currentJsonLogContext_.location().line();
attributes_.as_object()["ThreadId"] = threadId.value;
attributes_.as_object()["Level"] = to_string(severity);
attributes_.as_object()["Time"] =
attributes_.RemoveMember("File");
attributes_.AddMember(
rapidjson::StringRef("File"),
rapidjson::Value{location.file_name(), allocator_},
allocator_);
attributes_.RemoveMember("Line");
attributes_.AddMember(
rapidjson::StringRef("Line"),
location.line(),
allocator_);
attributes_.RemoveMember("ThreadId");
attributes_.AddMember(
rapidjson::StringRef("ThreadId"),
rapidjson::Value{threadId.value.c_str(), allocator_},
allocator_);
attributes_.RemoveMember("Params");
attributes_.AddMember(
rapidjson::StringRef("Params"),
rapidjson::Value{rapidjson::kObjectType},
allocator_);
auto severityStr = to_string(severity);
attributes_.RemoveMember("Level");
attributes_.AddMember(
rapidjson::StringRef("Level"),
rapidjson::Value{severityStr.c_str(), allocator_},
allocator_);
attributes_.RemoveMember("Time");
attributes_.AddMember(
rapidjson::StringRef("Time"),
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
.count(),
allocator_);
}
void
@@ -246,11 +271,21 @@ Journal::formatLog(std::string&& message)
return message;
}
auto& attributes = currentJsonLogContext_.attributes().as_object();
auto& attributes = currentJsonLogContext_.attributes();
attributes["Message"] = message;
attributes.RemoveMember("Message");
attributes.AddMember(
rapidjson::StringRef("Message"),
rapidjson::Value{rapidjson::StringRef(message.c_str()), currentJsonLogContext_.allocator()},
currentJsonLogContext_.allocator()
);
return boost::json::serialize(attributes);
rapidjson::StringBuffer buffer;
rapidjson::Writer writer(buffer);
attributes.Accept(writer);
return {buffer.GetString()};
}
void

View File

@@ -195,7 +195,6 @@ TEST_CASE("Global attributes inheritable")
CHECK(jsonLog.IsObject());
CHECK(jsonLog.HasMember("Field1"));
CHECK(jsonLog.HasMember("Field2"));
CHECK(jsonLog["Field1"].IsString());
// Field1 should be overwritten to Value3
CHECK(jsonLog["Field1"].GetString() == std::string{"Value3"});
@@ -413,8 +412,6 @@ TEST_CASE_FIXTURE(JsonLogStreamFixture, "TestJsonLogParams")
"Field2",
std::numeric_limits<std::uint64_t>::max());
auto test = stream().str();
rapidjson::Document logValue;
logValue.Parse(stream().str().c_str());