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 0f9091ec43
4 changed files with 40 additions and 6 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

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

29
oxenmq/fmt.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <fmt/format.h>
#include "connections.h"
#include "auth.h"
#include "address.h"
#include "oxenmq-internal.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);
}
};