From: Pieter Lexis Date: Thu, 25 Sep 2025 15:40:16 +0000 (+0200) Subject: feat: implement ostream << operator for AnyValue X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=06292f03fcb9ba9aff0ade5173d5fab16eda008d;p=thirdparty%2Fpdns.git feat: implement ostream << operator for AnyValue --- diff --git a/pdns/protozero-trace.cc b/pdns/protozero-trace.cc index 5b1fa712f4..894fd97695 100644 --- a/pdns/protozero-trace.cc +++ b/pdns/protozero-trace.cc @@ -21,6 +21,9 @@ */ #include "protozero-trace.hh" +#include "base64.hh" +#include +#include namespace pdns::trace { @@ -96,6 +99,48 @@ AnyValue AnyValue::decode(protozero::pbf_reader& reader) return {}; } +std::string AnyValue::toLogString() const +{ + if (std::holds_alternative(*this)) { + return "\"" + std::get(*this) + "\""; + } + if (std::holds_alternative(*this)) { + if (std::get(*this)) { + return "true"; + } + return "false"; + } + if (std::holds_alternative(*this)) { + return std::to_string(std::get(*this)); + } + if (std::holds_alternative(*this)) { + return std::to_string(std::get(*this)); + } + if (std::holds_alternative(*this)) { + std::string tmp = "["; + for (auto const& val : std::get(*this).values) { + tmp += val.toLogString() + ", "; + } + tmp.resize(tmp.size() - 2); // Strip ", " + return tmp += "]"; + } + if (std::holds_alternative(*this)) { + std::string tmp = "{"; + for (auto const& val : std::get(*this).values) { + tmp += val.key + ": " + val.value.toLogString() + ", "; + } + if (tmp.size() > 2) { + tmp.resize(tmp.size() - 2); // Strip ", " + } + return tmp += "}"; + } + if (std::holds_alternative>(*this)) { + auto val = std::get>(*this); + return Base64Encode(std::string(val.begin(), val.end())); + } + return "UNSUPPORTED TYPE"; +} + void EntityRef::encode(protozero::pbf_writer& writer) const { pdns::trace::encode(writer, 1, schema_url); diff --git a/pdns/protozero-trace.hh b/pdns/protozero-trace.hh index 6141916921..bf3617dfcb 100644 --- a/pdns/protozero-trace.hh +++ b/pdns/protozero-trace.hh @@ -176,6 +176,11 @@ struct AnyValue : public std::variant