diff --git a/include/xrpl/beast/utility/Journal.h b/include/xrpl/beast/utility/Journal.h index 7985a5527f..288099b4a2 100644 --- a/include/xrpl/beast/utility/Journal.h +++ b/include/xrpl/beast/utility/Journal.h @@ -22,9 +22,7 @@ #include -#include - -#include +#include #include #include @@ -80,19 +78,6 @@ std::ostream& operator<<(std::ostream& os, LogParameter 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; -} // 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 -concept CanSetJsonValue = requires(T val) { - setJsonField( - std::declval(), - val, - std::declval(), - std::declval()); -}; - template void setJsonValue( - yyjson::YYJsonDocPtr& object, + boost::json::value& object, char const* name, T&& value, std::ostream* outStream) { using ValueType = std::decay_t; - auto root = yyjson_mut_doc_get_root(object.get()); + auto& root = object.as_object(); - if constexpr (CanSetJsonValue) + if constexpr (std::constructible_from) { - setJsonField(name, std::forward(value), object.get(), root); + root[name] = std::forward(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) { diff --git a/src/libxrpl/beast/utility/beast_Journal.cpp b/src/libxrpl/beast/utility/beast_Journal.cpp index 4a67ab14db..b1d0998009 100644 --- a/src/libxrpl/beast/utility/beast_Journal.cpp +++ b/src/libxrpl/beast/utility/beast_Journal.cpp @@ -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(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::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