Compare commits

...

1 Commits

Author SHA1 Message Date
JCW
fc1fc894cd Switch to boost.json
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
2025-08-29 16:11:27 +01:00
2 changed files with 38 additions and 318 deletions

View File

@@ -22,9 +22,7 @@
#include <xrpl/beast/utility/instrumentation.h>
#include <boost/asio/execution/allocator.hpp>
#include <yyjson.h>
#include <boost/json.hpp>
#include <memory>
#include <optional>
@@ -80,19 +78,6 @@ std::ostream&
operator<<(std::ostream& os, LogParameter<T> const& param);
} // namespace ripple::log
namespace yyjson {
struct YYJsonDeleter
{
void
operator()(yyjson_mut_doc* doc)
{
yyjson_mut_doc_free(doc);
}
};
using YYJsonDocPtr = std::unique_ptr<yyjson_mut_doc, YYJsonDeleter>;
} // namespace yyjson
namespace beast {
/** A namespace for easy access to logging severity values. */
@@ -147,7 +132,7 @@ public:
class JsonLogAttributes
{
public:
using AttributeFields = yyjson::YYJsonDocPtr;
using AttributeFields = boost::json::value;
JsonLogAttributes();
JsonLogAttributes(JsonLogAttributes const& other);
@@ -186,7 +171,7 @@ public:
struct JsonLogContext
{
std::source_location location = {};
yyjson::YYJsonDocPtr messageParams;
boost::json::value messageParams;
JsonLogContext() = default;
@@ -194,9 +179,8 @@ public:
reset(std::source_location location_) noexcept
{
location = location_;
messageParams.reset(yyjson_mut_doc_new(nullptr));
yyjson_mut_doc_set_root(
messageParams.get(), yyjson_mut_obj(messageParams.get()));
messageParams = {};
messageParams.emplace_object();
}
};
@@ -724,224 +708,20 @@ namespace ripple::log {
namespace detail {
inline void
setJsonField(
std::string const& name,
std::int8_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_sint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::uint8_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_uint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::int16_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_sint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::uint16_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_uint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::int32_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_sint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::uint32_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_uint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::int64_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_sint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::uint64_t value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_uint(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::string const& value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_strncpy(doc, value.c_str(), value.length()));
}
inline void
setJsonField(
std::string const& name,
char const* value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_strcpy(doc, value));
}
inline void
setJsonField(
std::string const& name,
bool value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_bool(doc, value));
}
inline void
setJsonField(
std::string const& name,
float value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_real(doc, value));
}
inline void
setJsonField(
std::string const& name,
double value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_real(doc, value));
}
inline void
setJsonField(
std::string const& name,
std::nullptr_t,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_null(doc));
}
inline void
setJsonField(
std::string const& name,
yyjson_mut_val* value,
yyjson_mut_doc* doc,
yyjson_mut_val* obj)
{
yyjson_mut_obj_put(
obj,
yyjson_mut_strncpy(doc, name.c_str(), name.length()),
yyjson_mut_val_mut_copy(doc, value));
}
template <typename T>
concept CanSetJsonValue = requires(T val) {
setJsonField(
std::declval<std::string const&>(),
val,
std::declval<yyjson_mut_doc*>(),
std::declval<yyjson_mut_val*>());
};
template <typename T>
void
setJsonValue(
yyjson::YYJsonDocPtr& object,
boost::json::value& object,
char const* name,
T&& value,
std::ostream* outStream)
{
using ValueType = std::decay_t<T>;
auto root = yyjson_mut_doc_get_root(object.get());
auto& root = object.as_object();
if constexpr (CanSetJsonValue<ValueType>)
if constexpr (std::constructible_from<boost::json::value, ValueType>)
{
setJsonField(name, std::forward<T>(value), object.get(), root);
root[name] = std::forward<T>(value);
if (outStream)
{
(*outStream) << value;
@@ -952,7 +732,7 @@ setJsonValue(
std::ostringstream oss;
oss << value;
setJsonField(name, oss.str(), object.get(), root);
root[name] = oss.str();
if (outStream)
{

View File

@@ -121,15 +121,13 @@ severities::to_string(Severity severity)
Journal::JsonLogAttributes::JsonLogAttributes()
{
contextValues_.reset(yyjson_mut_doc_new(nullptr));
yyjson_mut_doc_set_root(
contextValues_.get(), yyjson_mut_obj(contextValues_.get()));
contextValues_ = {};
contextValues_.emplace_object();
}
Journal::JsonLogAttributes::JsonLogAttributes(JsonLogAttributes const& other)
{
contextValues_.reset(
yyjson_mut_doc_mut_copy(other.contextValues_.get(), nullptr));
contextValues_ = other.contextValues_;
}
Journal::JsonLogAttributes::JsonLogAttributes(JsonLogAttributes&& other)
@@ -144,8 +142,7 @@ Journal::JsonLogAttributes::operator=(JsonLogAttributes const& other)
{
return *this;
}
contextValues_.reset(
yyjson_mut_doc_mut_copy(other.contextValues_.get(), nullptr));
contextValues_ = other.contextValues_;
return *this;
}
@@ -164,9 +161,7 @@ Journal::JsonLogAttributes::operator=(JsonLogAttributes&& other)
void
Journal::JsonLogAttributes::setModuleName(std::string const& name)
{
auto root = yyjson_mut_doc_get_root(contextValues_.get());
ripple::log::detail::setJsonField(
"Module", name, contextValues_.get(), root);
contextValues_.as_object()["Module"] = name;
}
Journal::JsonLogAttributes
@@ -176,23 +171,11 @@ Journal::JsonLogAttributes::combine(
{
JsonLogAttributes result;
result.contextValues_.reset(yyjson_mut_doc_mut_copy(a.get(), nullptr));
result.contextValues_ = a;
auto bRoot = yyjson_mut_doc_get_root(b.get());
auto root = yyjson_mut_doc_get_root(result.contextValues_.get());
for (auto iter = yyjson_mut_obj_iter_with(bRoot);
yyjson_mut_obj_iter_has_next(&iter);
yyjson_mut_obj_iter_next(&iter))
for (auto& [key, value] : b.as_object())
{
auto key = iter.cur;
auto val = yyjson_mut_obj_iter_get_val(key);
auto keyCopied =
yyjson_mut_val_mut_copy(result.contextValues_.get(), key);
auto valueCopied =
yyjson_mut_val_mut_copy(result.contextValues_.get(), val);
yyjson_mut_obj_put(root, keyCopied, valueCopied);
result.contextValues_.as_object()[key] = value;
}
return result;
@@ -215,89 +198,46 @@ Journal::formatLog(
return message;
}
yyjson::YYJsonDocPtr doc{yyjson_mut_doc_new(nullptr)};
auto logContext = yyjson_mut_obj(doc.get());
yyjson_mut_doc_set_root(doc.get(), logContext);
boost::json::value doc;
if (globalLogAttributes_)
{
auto root = yyjson_mut_doc_get_root(
globalLogAttributes_->contextValues().get());
for (auto iter = yyjson_mut_obj_iter_with(root);
yyjson_mut_obj_iter_has_next(&iter);
yyjson_mut_obj_iter_next(&iter))
{
auto key = iter.cur;
auto val = yyjson_mut_obj_iter_get_val(key);
auto keyCopied = yyjson_mut_val_mut_copy(doc.get(), key);
auto valueCopied = yyjson_mut_val_mut_copy(doc.get(), val);
yyjson_mut_obj_put(logContext, keyCopied, valueCopied);
}
doc = globalLogAttributes_->contextValues_;
}
else
{
doc.emplace_object();
}
if (attributes.has_value())
{
auto root = yyjson_mut_doc_get_root(attributes->contextValues().get());
for (auto iter = yyjson_mut_obj_iter_with(root);
yyjson_mut_obj_iter_has_next(&iter);
yyjson_mut_obj_iter_next(&iter))
for (auto& [key, value] : attributes->contextValues_.as_object())
{
auto key = iter.cur;
auto val = yyjson_mut_obj_iter_get_val(key);
auto keyCopied = yyjson_mut_val_mut_copy(doc.get(), key);
auto valueCopied = yyjson_mut_val_mut_copy(doc.get(), val);
yyjson_mut_obj_put(logContext, keyCopied, valueCopied);
doc.as_object()[key] = value;
}
}
ripple::log::detail::setJsonField(
"Function",
currentJsonLogContext_.location.function_name(),
doc.get(),
logContext);
ripple::log::detail::setJsonField(
"File",
currentJsonLogContext_.location.file_name(),
doc.get(),
logContext);
ripple::log::detail::setJsonField(
"Line",
static_cast<std::uint64_t>(currentJsonLogContext_.location.line()),
doc.get(),
logContext);
auto& logContext = doc.as_object();
logContext["Function"] = currentJsonLogContext_.location.function_name();
logContext["File"] = currentJsonLogContext_.location.file_name();
logContext["Line"] = currentJsonLogContext_.location.line();
std::stringstream threadIdStream;
threadIdStream << std::this_thread::get_id();
auto threadIdStr = threadIdStream.str();
auto messageParamsRoot =
yyjson_mut_doc_get_root(currentJsonLogContext_.messageParams.get());
ripple::log::detail::setJsonField(
"ThreadId", threadIdStr, doc.get(), logContext);
ripple::log::detail::setJsonField(
"Params", messageParamsRoot, doc.get(), logContext);
yyjson_mut_doc_set_root(
currentJsonLogContext_.messageParams.get(), nullptr);
logContext["ThreadId"] = threadIdStr;
logContext["Params"] = currentJsonLogContext_.messageParams;
auto severityStr = to_string(severity);
ripple::log::detail::setJsonField(
"Level", severityStr, doc.get(), logContext);
ripple::log::detail::setJsonField(
"Message", message, doc.get(), logContext);
ripple::log::detail::setJsonField(
"Time",
logContext["Level"] = severityStr;
logContext["Message"] = message;
logContext["Time"] =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count(),
doc.get(),
logContext);
.count();
std::string result = yyjson_mut_write(doc.get(), 0, nullptr);
return result;
return boost::json::serialize(doc);
}
void