From ce84cc8b44c5bb8fb23cf339a76f630b06cbb272 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Thu, 15 Jan 2026 20:50:55 -0500 Subject: [PATCH] improve trace hf code (#6190) * adjust trace statements * add helper function * use lambda instead * use same paradigm in TestHostFunctions * oops --- src/test/app/TestHostFunctions.h | 109 ++++++------------ src/xrpld/app/wasm/HostFuncImpl.h | 19 +++ .../app/wasm/detail/HostFuncImplTrace.cpp | 92 +++------------ 3 files changed, 67 insertions(+), 153 deletions(-) diff --git a/src/test/app/TestHostFunctions.h b/src/test/app/TestHostFunctions.h index cbc75f1ed9..17a210441d 100644 --- a/src/test/app/TestHostFunctions.h +++ b/src/test/app/TestHostFunctions.h @@ -380,35 +380,46 @@ public: return 4; } - Expected - trace(std::string_view const& msg, Slice const& data, bool asHex) override + template + void + log(std::string_view const& msg, F&& dataFn) { - auto const ret = msg.size() + data.size() * (asHex ? 2 : 1); #ifdef DEBUG_OUTPUT auto& j = std::cerr; #else if (!getJournal().active(beast::severities::kTrace)) - return ret; + return; auto j = getJournal().trace(); #endif - if (!asHex) - { - j << "WASM TRACE: " << msg << " " - << std::string_view( - reinterpret_cast(data.data()), data.size()); - } - else - { - std::string hex; - hex.reserve(data.size() * 2); - boost::algorithm::hex( - data.begin(), data.end(), std::back_inserter(hex)); - j << "WASM DEV TRACE: " << msg << " " << hex; - } + j << "WasmTrace: " << msg << " " << dataFn(); #ifdef DEBUG_OUTPUT j << std::endl; #endif + } + + Expected + trace(std::string_view const& msg, Slice const& data, bool asHex) override + { + auto const ret = msg.size() + data.size() * (asHex ? 2 : 1); + + if (!asHex) + { + log(msg, [&data] { + return std::string_view( + reinterpret_cast(data.data()), data.size()); + }); + } + else + { + log(msg, [&data] { + std::string hex; + hex.reserve(data.size() * 2); + boost::algorithm::hex( + data.begin(), data.end(), std::back_inserter(hex)); + return hex; + }); + } return ret; } @@ -417,18 +428,7 @@ public: traceNum(std::string_view const& msg, int64_t data) override { auto const ret = msg.size() + sizeof(data); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - j << "WASM TRACE NUM: " << msg << " " << data; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif + log(msg, [data] { return data; }); return ret; } @@ -436,24 +436,7 @@ public: traceAccount(std::string_view const& msg, AccountID const& account) override { auto const ret = msg.size() + account.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - if (!account) - return Unexpected(HostFunctionError::INVALID_ACCOUNT); - - auto const accountStr = toBase58(account); - - j << "WASM TRACE ACCOUNT: " << msg << " " << accountStr; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&account] { return toBase58(account); }); return ret; } @@ -461,20 +444,7 @@ public: traceFloat(std::string_view const& msg, Slice const& data) override { auto const ret = msg.size() + data.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - auto const s = wasm_float::floatToString(data); - j << "WASM TRACE FLOAT: " << msg << " " << s; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&data] { return wasm_float::floatToString(data); }); return ret; } @@ -482,20 +452,7 @@ public: traceAmount(std::string_view const& msg, STAmount const& amount) override { auto const ret = msg.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - auto const amountStr = amount.getFullText(); - j << "WASM TRACE AMOUNT: " << msg << " " << amountStr; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&amount] { return amount.getFullText(); }); return ret; } diff --git a/src/xrpld/app/wasm/HostFuncImpl.h b/src/xrpld/app/wasm/HostFuncImpl.h index 0183a74a12..26c55328db 100644 --- a/src/xrpld/app/wasm/HostFuncImpl.h +++ b/src/xrpld/app/wasm/HostFuncImpl.h @@ -41,6 +41,25 @@ class WasmHostFunctionsImpl : public HostFunctions return cacheIdx; } + template + void + log(std::string_view const& msg, F&& dataFn) + { +#ifdef DEBUG_OUTPUT + auto& j = std::cerr; +#else + if (!getJournal().active(beast::severities::kTrace)) + return; + auto j = getJournal().trace(); +#endif + j << "WasmTrace[" << to_short_string(leKey.key) << "]: " << msg << " " + << dataFn(); + +#ifdef DEBUG_OUTPUT + j << std::endl; +#endif + } + public: WasmHostFunctionsImpl(ApplyContext& ct, Keylet const& leKey) : HostFunctions(ct.journal), ctx(ct), leKey(leKey) diff --git a/src/xrpld/app/wasm/detail/HostFuncImplTrace.cpp b/src/xrpld/app/wasm/detail/HostFuncImplTrace.cpp index d597a54f54..ddf4cde0e5 100644 --- a/src/xrpld/app/wasm/detail/HostFuncImplTrace.cpp +++ b/src/xrpld/app/wasm/detail/HostFuncImplTrace.cpp @@ -16,33 +16,25 @@ WasmHostFunctionsImpl::trace( bool asHex) { auto const ret = msg.size() + data.size() * (asHex ? 2 : 1); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif if (!asHex) { - j << "HF TRACE (" << leKey.key << "): " << msg << " " - << std::string_view( - reinterpret_cast(data.data()), data.size()); + log(msg, [&data] { + return std::string_view( + reinterpret_cast(data.data()), data.size()); + }); } else { - std::string hex; - hex.reserve(data.size() * 2); - boost::algorithm::hex( - data.begin(), data.end(), std::back_inserter(hex)); - j << "HF DEV TRACE (" << leKey.key << "): " << msg << " " << hex; + log(msg, [&data] { + std::string hex; + hex.reserve(data.size() * 2); + boost::algorithm::hex( + data.begin(), data.end(), std::back_inserter(hex)); + return hex; + }); } -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - return ret; } @@ -50,20 +42,7 @@ Expected WasmHostFunctionsImpl::traceNum(std::string_view const& msg, int64_t data) { auto const ret = msg.size() + sizeof(data); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - - j << "HF TRACE NUM(" << leKey.key << "): " << msg << " " << data; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [data] { return data; }); return ret; } @@ -73,22 +52,7 @@ WasmHostFunctionsImpl::traceAccount( AccountID const& account) { auto const ret = msg.size() + account.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - - auto const accountStr = toBase58(account); - - j << "HF TRACE ACCOUNT(" << leKey.key << "): " << msg << " " << accountStr; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&account] { return toBase58(account); }); return ret; } @@ -98,20 +62,7 @@ WasmHostFunctionsImpl::traceFloat( Slice const& data) { auto const ret = msg.size() + data.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - auto const s = wasm_float::floatToString(data); - j << "HF TRACE FLOAT(" << leKey.key << "): " << msg << " " << s; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&data] { return wasm_float::floatToString(data); }); return ret; } @@ -121,20 +72,7 @@ WasmHostFunctionsImpl::traceAmount( STAmount const& amount) { auto const ret = msg.size(); -#ifdef DEBUG_OUTPUT - auto& j = std::cerr; -#else - if (!getJournal().active(beast::severities::kTrace)) - return ret; - auto j = getJournal().trace(); -#endif - auto const amountStr = amount.getFullText(); - j << "HF TRACE AMOUNT(" << leKey.key << "): " << msg << " " << amountStr; - -#ifdef DEBUG_OUTPUT - j << std::endl; -#endif - + log(msg, [&amount] { return amount.getFullText(); }); return ret; }