From c91e56cf2d88873f187f0499c2337de263c22294 Mon Sep 17 00:00:00 2001 From: Sean Darcy Date: Thu, 28 Jul 2022 12:02:29 +1000 Subject: [PATCH] adds custom formatter for OMQ structs that have to_string member --- CMakeLists.txt | 1 + oxenmq/auth.h | 18 ++++++++++++++++++ oxenmq/connections.cpp | 12 +++++++----- oxenmq/connections.h | 4 +++- oxenmq/fmt.h | 28 ++++++++++++++++++++++++++++ oxenmq/oxenmq-internal.h | 17 ----------------- 6 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 oxenmq/fmt.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 10aef79..69a3e7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,6 +212,7 @@ if(OXENMQ_INSTALL) oxenmq/bt_serialize.h oxenmq/bt_value.h oxenmq/connections.h + oxenmq/fmt.h oxenmq/hex.h oxenmq/oxenmq.h oxenmq/message.h diff --git a/oxenmq/auth.h b/oxenmq/auth.h index f0fffc7..f5e7963 100644 --- a/oxenmq/auth.h +++ b/oxenmq/auth.h @@ -51,5 +51,23 @@ struct already_hashed { /// pubkeys and a terrible hash choice for anything else). using pubkey_set = std::unordered_set; +inline constexpr std::string_view to_string(AuthLevel a) { + switch (a) { + case AuthLevel::denied: return "denied"; + case AuthLevel::none: return "none"; + case AuthLevel::basic: return "basic"; + case AuthLevel::admin: return "admin"; + default: return "(unknown)"; + } +} + +inline AuthLevel auth_from_string(std::string_view a) { + if (a == "none") return AuthLevel::none; + if (a == "basic") return AuthLevel::basic; + if (a == "admin") return AuthLevel::admin; + return AuthLevel::denied; +} + + } diff --git a/oxenmq/connections.cpp b/oxenmq/connections.cpp index 43d846e..79812a5 100644 --- a/oxenmq/connections.cpp +++ b/oxenmq/connections.cpp @@ -5,10 +5,7 @@ namespace oxenmq { std::ostream& operator<<(std::ostream& o, const ConnectionID& conn) { - if (!conn.pk.empty()) - return o << (conn.sn() ? "SN " : "non-SN authenticated remote ") << oxenc::to_hex(conn.pk); - else - return o << "unauthenticated remote [" << conn.id << "]"; + return o << conn.to_string(); } namespace { @@ -381,7 +378,12 @@ void OxenMQ::proxy_disconnect(ConnectionID conn, std::chrono::milliseconds linge OMQ_LOG(warn, "Failed to disconnect ", conn, ": no such outgoing connection"); } - +std::string ConnectionID::to_string() const { + if (!pk.empty()) + return (sn() ? std::string("SN ") : std::string("non-SN authenticated remote ")) + oxenc::to_hex(pk); + else + return std::string("unauthenticated remote [") + std::to_string(id) + "]"; +} } diff --git a/oxenmq/connections.h b/oxenmq/connections.h index 3a2a8a4..ad66530 100644 --- a/oxenmq/connections.h +++ b/oxenmq/connections.h @@ -68,6 +68,9 @@ struct ConnectionID { // Returns a copy of the ConnectionID with the route set to empty. ConnectionID unrouted() { return ConnectionID{id, pk, ""}; } + std::string to_string() const; + + private: ConnectionID(int64_t id) : id{id} {} ConnectionID(int64_t id, std::string pubkey, std::string route = "") @@ -93,4 +96,3 @@ namespace std { } }; } // namespace std - diff --git a/oxenmq/fmt.h b/oxenmq/fmt.h new file mode 100644 index 0000000..c3df7bd --- /dev/null +++ b/oxenmq/fmt.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include "connections.h" +#include "auth.h" +#include "address.h" + +template <> +struct fmt::formatter : fmt::formatter { + auto format(oxenmq::AuthLevel v, format_context& ctx) { + return formatter::format( + fmt::format("{}", to_string(v)), ctx); + } +}; +template <> +struct fmt::formatter : fmt::formatter { + auto format(oxenmq::ConnectionID conn, format_context& ctx) { + return formatter::format( + fmt::format("{}", conn.to_string()), ctx); + } +}; +template <> +struct fmt::formatter : fmt::formatter { + auto format(oxenmq::address addr, format_context& ctx) { + return formatter::format( + fmt::format("{}", addr.full_address()), ctx); + } +}; diff --git a/oxenmq/oxenmq-internal.h b/oxenmq/oxenmq-internal.h index 32f5596..3e7dde4 100644 --- a/oxenmq/oxenmq-internal.h +++ b/oxenmq/oxenmq-internal.h @@ -112,23 +112,6 @@ inline std::string_view view(const zmq::message_t& m) { return {m.data(), m.size()}; } -inline std::string to_string(AuthLevel a) { - switch (a) { - case AuthLevel::denied: return "denied"; - case AuthLevel::none: return "none"; - case AuthLevel::basic: return "basic"; - case AuthLevel::admin: return "admin"; - default: return "(unknown)"; - } -} - -inline AuthLevel auth_from_string(std::string_view a) { - if (a == "none") return AuthLevel::none; - if (a == "basic") return AuthLevel::basic; - if (a == "admin") return AuthLevel::admin; - return AuthLevel::denied; -} - // Extracts and builds the "send" part of a message for proxy_send/proxy_reply inline std::list build_send_parts(oxenc::bt_list_consumer send, std::string_view route) { std::list parts;