From eec8244a6cf120d78496947221a6c7bbe70d34eb Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Mon, 18 Jul 2022 14:56:09 -0300 Subject: [PATCH] Remote util::Printer and related cruft --- llarp/CMakeLists.txt | 1 - llarp/dns/message.cpp | 24 +- llarp/dns/message.hpp | 10 +- llarp/dns/question.cpp | 16 +- llarp/dns/question.hpp | 2 - llarp/dns/rr.cpp | 25 +- llarp/dns/rr.hpp | 2 - llarp/net/address_info.cpp | 20 +- llarp/net/address_info.hpp | 3 - llarp/net/exit_info.cpp | 14 +- llarp/net/exit_info.hpp | 3 - llarp/net/uint128.hpp | 2 - llarp/path/transit_hop.cpp | 36 +- llarp/path/transit_hop.hpp | 5 - llarp/pow.cpp | 20 +- llarp/pow.hpp | 3 - llarp/router_contact.cpp | 28 +- llarp/router_contact.hpp | 3 - llarp/service/info.cpp | 16 +- llarp/service/info.hpp | 3 - llarp/service/intro.cpp | 24 +- llarp/service/intro.hpp | 2 - llarp/service/intro_set.cpp | 61 +-- llarp/service/intro_set.hpp | 4 - llarp/util/aligned.hpp | 11 - llarp/util/formattable.hpp | 4 +- llarp/util/meta/traits.hpp | 209 -------- llarp/util/printer.cpp | 197 -------- llarp/util/printer.hpp | 583 ---------------------- test/CMakeLists.txt | 2 - test/util/meta/test_llarp_util_traits.cpp | 139 ------ test/util/test_llarp_util_printer.cpp | 94 ---- 32 files changed, 75 insertions(+), 1491 deletions(-) delete mode 100644 llarp/util/meta/traits.hpp delete mode 100644 llarp/util/printer.cpp delete mode 100644 llarp/util/printer.hpp delete mode 100644 test/util/meta/test_llarp_util_traits.cpp delete mode 100644 test/util/test_llarp_util_printer.cpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 0cd9edd9c..cf4bcb786 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -10,7 +10,6 @@ add_library(lokinet-util util/logging/buffer.cpp util/lokinet_init.c util/mem.cpp - util/printer.cpp util/str.cpp util/thread/queue_manager.cpp util/thread/threading.cpp diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index a6957116f..4519e0457 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -5,7 +5,6 @@ #include "srv_data.hpp" #include #include -#include #include #include @@ -401,19 +400,18 @@ namespace llarp } } - std::ostream& - Message::print(std::ostream& stream, int level, int spaces) const + std::string + Message::ToString() const { - Printer printer(stream, level, spaces); - - printer.printAttributeAsHex("dns message id", hdr_id); - printer.printAttributeAsHex("fields", hdr_fields); - printer.printAttribute("questions", questions); - printer.printAttribute("answers", answers); - printer.printAttribute("nameserer", authorities); - printer.printAttribute("additional", additional); - - return stream; + return fmt::format( + "[DNSMessage id={:x} fields={:x} questions={{{}}} answers={{{}}} authorities={{{}}} " + "additional={{{}}}]", + hdr_id, + hdr_fields, + fmt::format("{}", fmt::join(questions, ",")), + fmt::format("{}", fmt::join(answers, ",")), + fmt::format("{}", fmt::join(authorities, ",")), + fmt::format("{}", fmt::join(additional, ","))); } } // namespace dns diff --git a/llarp/dns/message.hpp b/llarp/dns/message.hpp index 9ba58ed1e..5d10e2901 100644 --- a/llarp/dns/message.hpp +++ b/llarp/dns/message.hpp @@ -93,16 +93,8 @@ namespace llarp [[nodiscard]] OwnedBuffer ToBuffer() const; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string - ToString() const - { - std::ostringstream o; - print(o, -1, -1); - return o.str(); - } + ToString() const; MsgID_t hdr_id; Fields_t hdr_fields; diff --git a/llarp/dns/question.cpp b/llarp/dns/question.cpp index aef0d1e01..a3ea0e886 100644 --- a/llarp/dns/question.cpp +++ b/llarp/dns/question.cpp @@ -1,7 +1,6 @@ #include "question.hpp" #include -#include #include #include "dns.hpp" @@ -120,20 +119,7 @@ namespace llarp std::string Question::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); - } - std::ostream& - Question::print(std::ostream& stream, int level, int spaces) const - { - std::ostringstream o; - Printer printer(o, level, spaces); - printer.printAttribute("qname", qname); - printer.printAttributeAsHex("qtype", qtype); - printer.printAttributeAsHex("qclass", qclass); - - return stream; + return fmt::format("[DNSQuestion qname={} qtype={:x} qclass={:x}]", qname, qtype, qclass); } } // namespace dns } // namespace llarp diff --git a/llarp/dns/question.hpp b/llarp/dns/question.hpp index 38eef2963..ae5b9cd10 100644 --- a/llarp/dns/question.hpp +++ b/llarp/dns/question.hpp @@ -27,8 +27,6 @@ namespace llarp std::string ToString() const; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; bool operator==(const Question& other) const diff --git a/llarp/dns/rr.cpp b/llarp/dns/rr.cpp index 7bb8c417b..332ff68a2 100644 --- a/llarp/dns/rr.cpp +++ b/llarp/dns/rr.cpp @@ -1,8 +1,8 @@ #include "rr.hpp" #include "dns.hpp" +#include "util/formattable.hpp" #include #include -#include namespace llarp { @@ -96,25 +96,16 @@ namespace llarp {"rdata", std::string{reinterpret_cast(rData.data()), rData.size()}}}; } - std::ostream& - ResourceRecord::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("name", rr_name); - printer.printAttribute("type", rr_type); - printer.printAttribute("class", rr_class); - printer.printAttribute("ttl", ttl); - printer.printAttribute("rdata", rData.size()); - - return stream; - } - std::string ResourceRecord::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[RR name={} type={} class={} ttl={} rdata-size={}]", + rr_name, + rr_type, + rr_class, + ttl, + rData.size()); } bool diff --git a/llarp/dns/rr.hpp b/llarp/dns/rr.hpp index 9f3e5d9f1..7f27594e9 100644 --- a/llarp/dns/rr.hpp +++ b/llarp/dns/rr.hpp @@ -33,8 +33,6 @@ namespace llarp util::StatusObject ToJSON() const override; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; std::string ToString() const; diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 5c18e276f..70788b258 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -7,7 +7,6 @@ #include "net.hpp" #include #include -#include #include @@ -171,25 +170,12 @@ namespace llarp port = addr.getPort(); } - std::ostream& - AddressInfo::print(std::ostream& stream, int level, int spaces) const - { - char tmp[128] = {0}; - inet_ntop(AF_INET6, (void*)&ip, tmp, sizeof(tmp)); - - Printer printer(stream, level, spaces); - printer.printAttribute("ip", tmp); - printer.printAttribute("port", port); - - return stream; - } - std::string AddressInfo::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + char tmp[INET6_ADDRSTRLEN] = {0}; + inet_ntop(AF_INET6, (void*)&ip, tmp, sizeof(tmp)); + return fmt::format("[{}]:{}", tmp, port); } void diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index f9982b36b..cd5698886 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -53,9 +53,6 @@ namespace llarp std::variant IP() const; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string ToString() const; }; diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 46f9cb92e..2af7c707f 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -91,9 +91,8 @@ namespace llarp return read; } - std::ostream& - ExitInfo::print( - std::ostream& stream, [[maybe_unused]] int level, [[maybe_unused]] int spaces) const + std::string + ExitInfo::ToString() const { /* // TODO: derive these from ipAdress @@ -119,14 +118,7 @@ namespace llarp #endif printer.printValue(ss.str()); */ - stream << ipAddress.ToString(); - return stream; - } - - std::string - ExitInfo::ToString() const - { - return ipAddress.ToString(); + return fmt::format("[Exit {}]", ipAddress.ToString()); } } // namespace llarp diff --git a/llarp/net/exit_info.hpp b/llarp/net/exit_info.hpp index bad9c0ba8..1db07aef3 100644 --- a/llarp/net/exit_info.hpp +++ b/llarp/net/exit_info.hpp @@ -40,9 +40,6 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf); - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string ToString() const; }; diff --git a/llarp/net/uint128.hpp b/llarp/net/uint128.hpp index 8340000b2..4bc39f53c 100644 --- a/llarp/net/uint128.hpp +++ b/llarp/net/uint128.hpp @@ -4,8 +4,6 @@ #include #include -#include "../util/meta/traits.hpp" - #include namespace llarp diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index a3b2cb19d..903326260 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -21,24 +21,15 @@ namespace llarp { namespace path { - std::ostream& - TransitHopInfo::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("tx", txID); - printer.printAttribute("rx", rxID); - printer.printAttribute("upstream", upstream); - printer.printAttribute("downstream", downstream); - - return stream; - } - std::string TransitHopInfo::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[TransitHopInfo tx={} rx={} upstream={} downstream={}]", + txID, + rxID, + upstream, + downstream); } TransitHop::TransitHop() @@ -440,22 +431,11 @@ namespace llarp return SendRoutingMessage(discarded, r); } - std::ostream& - TransitHop::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("TransitHop", info); - printer.printAttribute("started", started.count()); - printer.printAttribute("lifetime", lifetime.count()); - return stream; - } - std::string TransitHop::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[TransitHop {} started={} lifetime={}", info, started.count(), lifetime.count()); } void diff --git a/llarp/path/transit_hop.hpp b/llarp/path/transit_hop.hpp index 86080a330..eb828af9b 100644 --- a/llarp/path/transit_hop.hpp +++ b/llarp/path/transit_hop.hpp @@ -28,9 +28,6 @@ namespace llarp RouterID upstream; RouterID downstream; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string ToString() const; }; @@ -108,8 +105,6 @@ namespace llarp std::string ToString() const; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; bool Expired(llarp_time_t now) const override; diff --git a/llarp/pow.cpp b/llarp/pow.cpp index 48158f6b4..3de707111 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -53,24 +53,14 @@ namespace llarp return true; } - std::ostream& - PoW::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - - printer.printAttribute("pow timestamp", timestamp.count()); - printer.printAttribute("lifetime", extendedLifetime.count()); - printer.printAttribute("nonce", nonce); - - return stream; - } - std::string PoW::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[PoW timestamp={} lifetime={} nonce={}]", + timestamp.count(), + extendedLifetime.count(), + nonce); } } // namespace llarp diff --git a/llarp/pow.hpp b/llarp/pow.hpp index 0a01bc85c..79ff75e8b 100644 --- a/llarp/pow.hpp +++ b/llarp/pow.hpp @@ -38,9 +38,6 @@ namespace llarp return !(*this == other); } - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string ToString() const; }; diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index b1266145d..e1d319232 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -7,7 +7,6 @@ #include "util/buffer.hpp" #include "util/logging.hpp" #include "util/mem.hpp" -#include "util/printer.hpp" #include "util/time.hpp" #include @@ -573,27 +572,18 @@ namespace llarp return BDecode(&buf); } - std::ostream& - RouterContact::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("k", pubkey); - printer.printAttribute("updated", last_updated.count()); - printer.printAttribute("netid", netID); - printer.printAttribute("v", version); - printer.printAttribute("ai", addrs); - printer.printAttribute("e", enckey); - printer.printAttribute("z", signature); - - return stream; - } - std::string RouterContact::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[RC k={} updated={} netid={} v={} ai={{{}}} e={} z={}]", + pubkey, + last_updated.count(), + netID, + version, + fmt::format("{}", fmt::join(addrs, ",")), + enckey, + signature); } } // namespace llarp diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 33ed5f4d7..e328192e8 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -192,9 +192,6 @@ namespace llarp return last_updated < other.last_updated; } - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - bool Read(const fs::path& fname); diff --git a/llarp/service/info.cpp b/llarp/service/info.cpp index a969e8284..b9f5ee618 100644 --- a/llarp/service/info.cpp +++ b/llarp/service/info.cpp @@ -95,24 +95,10 @@ namespace llarp return true; } - std::ostream& - ServiceInfo::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("e", enckey); - printer.printAttribute("s", signkey); - printer.printAttribute("v", version); - printer.printAttribute("x", vanity); - - return stream; - } - std::string ServiceInfo::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format("[ServiceInfo e={} s={} v={} x={}]", enckey, signkey, version, vanity); } } // namespace service diff --git a/llarp/service/info.hpp b/llarp/service/info.hpp index bd8f5e58f..8244f6db9 100644 --- a/llarp/service/info.hpp +++ b/llarp/service/info.hpp @@ -63,9 +63,6 @@ namespace llarp return Addr() < other.Addr(); } - std::ostream& - print(std::ostream& stream, int level, int spaces) const; - std::string ToString() const; diff --git a/llarp/service/intro.cpp b/llarp/service/intro.cpp index 41d15aaaa..54de28805 100644 --- a/llarp/service/intro.cpp +++ b/llarp/service/intro.cpp @@ -65,26 +65,16 @@ namespace llarp expiresAt = 0s; } - std::ostream& - Introduction::print(std::ostream& stream, int level, int spaces) const - { - const RouterID r{router}; - Printer printer(stream, level, spaces); - printer.printAttribute("k", r.ToString()); - printer.printAttribute("l", latency.count()); - printer.printAttribute("p", pathID); - printer.printAttribute("v", version); - printer.printAttribute("x", expiresAt.count()); - - return stream; - } - std::string Introduction::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[Intro k={} l={} p={} v={} x={}]", + RouterID{router}, + latency.count(), + pathID, + version, + expiresAt.count()); } } // namespace service diff --git a/llarp/service/intro.hpp b/llarp/service/intro.hpp index 2495957b4..c1c431eb0 100644 --- a/llarp/service/intro.hpp +++ b/llarp/service/intro.hpp @@ -34,8 +34,6 @@ namespace llarp return IsExpired(now + dlt); } - std::ostream& - print(std::ostream& stream, int level, int spaces) const; std::string ToString() const; diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 431f9dbfe..5141256db 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -69,24 +69,16 @@ namespace llarp::service return signedAt < other.signedAt; } - std::ostream& - EncryptedIntroSet::print(std::ostream& out, int levels, int spaces) const - { - Printer printer(out, levels, spaces); - printer.printAttribute("d", derivedSigningKey); - printer.printAttribute("n", nounce); - printer.printAttribute("s", signedAt.count()); - printer.printAttribute("x", "[" + std::to_string(introsetPayload.size()) + " bytes]"); - printer.printAttribute("z", sig); - return out; - } - std::string EncryptedIntroSet::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[EncIntroSet d={} n={} s={} x=[{} bytes] z={}]", + derivedSigningKey, + nounce, + signedAt.count(), + introsetPayload.size(), + sig); } std::optional @@ -427,38 +419,17 @@ namespace llarp::service return maxTime; } - std::ostream& - IntroSet::print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printAttribute("addressKeys", addressKeys); - printer.printAttribute("intros", intros); - printer.printAttribute("sntrupKey", sntrupKey); - - std::string _topic = topic.ToString(); - - if (!_topic.empty()) - { - printer.printAttribute("topic", _topic); - } - else - { - printer.printAttribute("topic", topic); - } - - printer.printAttribute("signedAt", timestampSignedAt.count()); - - printer.printAttribute("version", version); - printer.printAttribute("sig", signature); - - return stream; - } - std::string IntroSet::ToString() const { - std::ostringstream o; - print(o, -1, -1); - return o.str(); + return fmt::format( + "[IntroSet addressKeys={} intros={{{}}} sntrupKey={} topic={} signedAt={} v={} sig={}]", + addressKeys, + fmt::format("{}", fmt::join(intros, ",")), + sntrupKey, + topic, + timestampSignedAt.count(), + version, + signature); } } // namespace llarp::service diff --git a/llarp/service/intro_set.hpp b/llarp/service/intro_set.hpp index 71a19288e..fdeec0126 100644 --- a/llarp/service/intro_set.hpp +++ b/llarp/service/intro_set.hpp @@ -57,8 +57,6 @@ namespace llarp return timestampSignedAt < other.timestampSignedAt; } - std::ostream& - print(std::ostream& stream, int level, int spaces) const; std::string ToString() const; @@ -170,8 +168,6 @@ namespace llarp bool Verify(llarp_time_t now) const; - std::ostream& - print(std::ostream& stream, int level, int spaces) const; std::string ToString() const; diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index d41fff48f..92dcf38a4 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -2,9 +2,7 @@ #include "bencode.h" #include -#include #include -#include "printer.hpp" #include @@ -283,15 +281,6 @@ namespace llarp return true; } - std::ostream& - print(std::ostream& stream, int level, int spaces) const - { - Printer printer(stream, level, spaces); - printer.printValue(ToHex()); - - return stream; - } - private: Data m_data; }; diff --git a/llarp/util/formattable.hpp b/llarp/util/formattable.hpp index edc4285e3..a2b1e8a37 100644 --- a/llarp/util/formattable.hpp +++ b/llarp/util/formattable.hpp @@ -52,7 +52,7 @@ namespace fmt { template auto - format(const fs::path& p, FormatContext& ctx) + format(const fs::path& p, FormatContext& ctx) const { return formatter::format(p.string(), ctx); } @@ -69,7 +69,7 @@ namespace fmt { template auto - format(const T& val, FormatContext& ctx) + format(const T& val, FormatContext& ctx) const { if constexpr (llarp::is_scoped_enum_v) return formatter::format(ToString(val), ctx); diff --git a/llarp/util/meta/traits.hpp b/llarp/util/meta/traits.hpp deleted file mode 100644 index 240ec277a..000000000 --- a/llarp/util/meta/traits.hpp +++ /dev/null @@ -1,209 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace llarp -{ - namespace traits - { - /// Represents the empty type - struct Bottom - {}; - - /// Int tag - template - struct Tag - { - char arr[N + 1]; - }; - - /// Type trait representing whether a type is pointer-like - template - struct is_pointy : public std::false_type - {}; - - // We take the following things: - // - has element_type typedef - // - has dereference operator - // - has arrow operator - template - struct is_pointy())>, void>> - : public std::true_type - {}; - - /// Type trait representing whether a type is an STL-style container - template - struct is_container : public std::false_type - {}; - - // We take that the container has begin, end and size methods to be a - // container. - // clang-format off - template < typename T > - struct is_container< - T, - std::conditional_t< - false, - std::void_t< typename T::value_type, - typename T::size_type, - typename T::iterator, - typename T::const_iterator, - decltype(std::declval().size()), - decltype(std::declval().begin()), - decltype(std::declval().end()), - decltype(std::declval().cbegin()), - decltype(std::declval().cend()) >, - void > > : public std::true_type - { - }; - // clang-format on - - namespace Switch - { - template - struct Switch - { - using Type = Bottom; - }; - - template - struct Switch<0u, T, Types...> - { - using Type = T; - }; - - template - struct Switch - { - using Type = typename Switch::Type; - }; - - } // namespace Switch - - namespace select - { - /// This provides a way to do a compile-type dispatch based on type traits - - /// meta function which always returns false - template - class False : public std::false_type - {}; - - /// a case in the selection - template