adds custom formatter for OMQ structs that have to_string member

This commit is contained in:
Sean Darcy 2022-07-28 12:02:29 +10:00
parent 61b7505304
commit c91e56cf2d
6 changed files with 57 additions and 23 deletions

View File

@ -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

View File

@ -51,5 +51,23 @@ struct already_hashed {
/// pubkeys and a terrible hash choice for anything else).
using pubkey_set = std::unordered_set<std::string, already_hashed>;
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;
}
}

View File

@ -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) + "]";
}
}

View File

@ -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

28
oxenmq/fmt.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <fmt/format.h>
#include "connections.h"
#include "auth.h"
#include "address.h"
template <>
struct fmt::formatter<oxenmq::AuthLevel> : fmt::formatter<std::string> {
auto format(oxenmq::AuthLevel v, format_context& ctx) {
return formatter<std::string>::format(
fmt::format("{}", to_string(v)), ctx);
}
};
template <>
struct fmt::formatter<oxenmq::ConnectionID> : fmt::formatter<std::string> {
auto format(oxenmq::ConnectionID conn, format_context& ctx) {
return formatter<std::string>::format(
fmt::format("{}", conn.to_string()), ctx);
}
};
template <>
struct fmt::formatter<oxenmq::address> : fmt::formatter<std::string> {
auto format(oxenmq::address addr, format_context& ctx) {
return formatter<std::string>::format(
fmt::format("{}", addr.full_address()), ctx);
}
};

View File

@ -112,23 +112,6 @@ inline std::string_view view(const zmq::message_t& m) {
return {m.data<char>(), 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<zmq::message_t> build_send_parts(oxenc::bt_list_consumer send, std::string_view route) {
std::list<zmq::message_t> parts;