From b7aeff95a94267db989012fecb274238a716ed12 Mon Sep 17 00:00:00 2001 From: Nicholas Dudfield Date: Fri, 27 Mar 2026 21:59:31 +0700 Subject: [PATCH] feat: add log transform to Logs for test-time message rewriting Logs::setTransform(fn) installs a function that transforms every log message before output. Useful in tests to replace raw r-addresses with human-readable account names. Usage: env.app().logs().setTransform([&](std::string const& text) { std::string out = text; // replace rG1QQv2... with Account(alice) boost::algorithm::replace_all(out, toBase58(alice.id()), "Account(alice)"); return out; }); // Pass nullptr to clear: env.app().logs().setTransform(nullptr); --- include/xrpl/basics/Log.h | 13 +++++++++++++ src/libxrpl/basics/Log.cpp | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/xrpl/basics/Log.h b/include/xrpl/basics/Log.h index 13128bfea..446875c21 100644 --- a/include/xrpl/basics/Log.h +++ b/include/xrpl/basics/Log.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -165,6 +166,7 @@ private: beast::severities::Severity thresh_; File file_; bool silent_ = false; + std::function transform_; public: Logs(beast::severities::Severity level); @@ -203,6 +205,17 @@ public: std::string const& text, bool console); + /** Set a transform applied to every log message before output. + * Useful in tests to replace raw account IDs with human-readable names. + * Pass nullptr to clear. + */ + void + setTransform(std::function fn) + { + std::lock_guard lock(mutex_); + transform_ = std::move(fn); + } + std::string rotate(); diff --git a/src/libxrpl/basics/Log.cpp b/src/libxrpl/basics/Log.cpp index 8e687d66f..3dbdbed63 100644 --- a/src/libxrpl/basics/Log.cpp +++ b/src/libxrpl/basics/Log.cpp @@ -196,9 +196,10 @@ Logs::write( std::string const& text, bool console) { - std::string s; - format(s, text, level, partition); std::lock_guard lock(mutex_); + std::string const& transformed = transform_ ? transform_(text) : text; + std::string s; + format(s, transformed, level, partition); file_.writeln(s); if (!silent_) std::cerr << s << '\n';