From d1fdea9bc804c2923648b70a34b42245baf5ef84 Mon Sep 17 00:00:00 2001 From: seelabs Date: Thu, 11 Feb 2021 19:09:54 -0500 Subject: [PATCH] [REMOVE] Add more structure to logs --- src/ripple/basics/Log.h | 93 ++++++++++++++++++++++++++++++ src/ripple/beast/utility/Journal.h | 2 - src/ripple/json/json_writer.h | 7 +++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/ripple/basics/Log.h b/src/ripple/basics/Log.h index 929225c043..a940289d07 100644 --- a/src/ripple/basics/Log.h +++ b/src/ripple/basics/Log.h @@ -27,8 +27,15 @@ #include #include #include +#include +#include #include +namespace Json { +class Value; +class Compact; +} // namespace Json + namespace ripple { // DEPRECATED use beast::severities::Severity instead @@ -256,6 +263,92 @@ private: x #endif +// terse "std::tie" +template +std::tuple +jv(T1 const& t1, T2 const& t2) +{ + return std::tie(t1, t2); +} + +template +void +jlogv_fields(Stream&& stream, char const* sep) +{ +} + +template +void +jlogv_fields( + Stream&& stream, + char const* sep, + std::tuple const& nameValue, + Ts&&... nameValues) +{ + bool const withQuotes = [&] { + if constexpr (std::is_arithmetic_v) + { + return false; + } + if constexpr (std::is_same_v, Json::Value>) + { + auto const& v = std::get<1>(nameValue); + return !v.isObject() && !v.isNumeric() && !v.isBool(); + } + if constexpr (std::is_same_v, Json::Compact>) + { + return false; + } + return true; + }(); + + stream << sep << '"' << std::get<0>(nameValue) << "\": "; + if constexpr (std::is_same_v, Json::Value>) + { + stream << string_for_log(std::get<1>(nameValue)); + } + else + { + if (withQuotes) + { + // print the value with quotes + stream << '"' << std::get<1>(nameValue) << '"'; + } + else + { + stream << std::get<1>(nameValue); + } + } + + jlogv_fields( + std::forward(stream), ", ", std::forward(nameValues)...); +} + +template +void +jlogv( + Stream&& stream, + std::size_t lineNo, + std::string_view const& msg, + Ts&&... nameValues) +{ + beast::Journal::ScopedStream s{std::forward(stream), msg}; + s << " {"; + jlogv_fields(s, "", std::forward(nameValues)..., jv("jlogId", lineNo)); + s << '}'; +} + +// Wraps a Journal::Stream to skip evaluation of +// expensive argument lists if the stream is not active. +#ifndef JLOGV +#define JLOGV(x, msg, ...) \ + if (!x) \ + { \ + } \ + else \ + jlogv(x, __LINE__, msg, __VA_ARGS__) +#endif + //------------------------------------------------------------------------------ // Debug logging: diff --git a/src/ripple/beast/utility/Journal.h b/src/ripple/beast/utility/Journal.h index 333a743a65..4b69986d86 100644 --- a/src/ripple/beast/utility/Journal.h +++ b/src/ripple/beast/utility/Journal.h @@ -134,7 +134,6 @@ public: class Stream; -private: /* Scoped ostream-based container for writing messages to a Journal. */ class ScopedStream { @@ -189,7 +188,6 @@ private: #endif //-------------------------------------------------------------------------- -public: /** Provide a light-weight way to check active() before string formatting */ class Stream { diff --git a/src/ripple/json/json_writer.h b/src/ripple/json/json_writer.h index d1ddcd4dec..5ccbb26bdd 100644 --- a/src/ripple/json/json_writer.h +++ b/src/ripple/json/json_writer.h @@ -342,6 +342,13 @@ public: } }; +[[nodiscard]] inline std::string +string_for_log(Json::Value const& v) +{ + FastWriter w; + return w.write(v); +} + } // namespace Json #endif // JSON_WRITER_H_INCLUDED