lokinet/llarp/service/address.hpp
Jason Rhinelander b81f7025c9
Replace logging with oxen-logger
Replaces custom logging system with spdlog-based oxen logging.  This
commit mainly replaces the backend logging with the spdlog-based system,
but doesn't (yet) convert all the existing LogWarn, etc. to use the new
format-based logging.

New logging statements will look like:

    llarp::log::warning(cat, "blah: {}", val);

where `cat` should be set up in each .cpp or cluster of .cpp files, as
described in the oxen-logging README.

As part of spdlog we get fmt, which gives us nice format strings, where
are applied generously in this commit.

Making types printable now requires two steps:
- add a ToString() method
- add this specialization:

      template <>
      constexpr inline bool llarp::IsToStringFormattable<llarp::Whatever> = true;

This will then allow the type to be printed as a "{}" value in a
fmt::format string.  This is applied to all our printable types here,
and all of the `operator<<` are removed.

This commit also:
- replaces various uses of `operator<<` to ToString()
- replaces various uses of std::stringstream with either fmt::format or
  plain std::string
- Rename some to_string and toString() methods to ToString() for
  consistency (and to work with fmt)
- Replace `stringify(...)` and `make_exception` usage with fmt::format
  (and remove stringify/make_exception from util/str.hpp).
2022-07-15 22:17:59 -03:00

110 lines
2.4 KiB
C++

#pragma once
#include <llarp/dht/key.hpp>
#include <llarp/router_id.hpp>
#include <llarp/util/aligned.hpp>
#include <functional>
#include <numeric>
#include <string>
#include <string_view>
#include <variant>
#include <set>
namespace llarp
{
namespace service
{
/// Snapp Address
struct Address : public AlignedBuffer<32>
{
/// if parsed using FromString this contains the subdomain
/// this member is not used when comparing it's extra data for dns
std::string subdomain;
/// list of whitelisted gtld to permit
static const std::set<std::string> AllowedTLDs;
/// return true if we permit using this tld
/// otherwise return false
static bool
PermitTLD(const char* tld);
std::string
ToString(const char* tld = ".loki") const;
bool
FromString(std::string_view str, const char* tld = ".loki");
Address() : AlignedBuffer<32>()
{}
explicit Address(const std::string& str) : AlignedBuffer<32>()
{
if (not FromString(str))
throw std::runtime_error("invalid address");
}
explicit Address(const Data& buf) : AlignedBuffer<32>(buf)
{}
Address(const Address& other)
: AlignedBuffer<32>(other.as_array()), subdomain(other.subdomain)
{}
explicit Address(const AlignedBuffer<32>& other) : AlignedBuffer<32>(other)
{}
bool
operator<(const Address& other) const
{
return as_array() < other.as_array();
}
bool
operator==(const Address& other) const
{
return as_array() == other.as_array();
}
bool
operator!=(const Address& other) const
{
return as_array() != other.as_array();
}
Address&
operator=(const Address& other) = default;
dht::Key_t
ToKey() const;
RouterID
ToRouter() const
{
return RouterID(as_array());
}
};
std::optional<std::variant<Address, RouterID>>
ParseAddress(std::string_view lokinet_addr);
} // namespace service
template <>
constexpr inline bool IsToStringFormattable<service::Address> = true;
} // namespace llarp
namespace std
{
template <>
struct hash<llarp::service::Address>
{
size_t
operator()(const llarp::service::Address& addr) const
{
return std::accumulate(addr.begin(), addr.end(), 0, std::bit_xor{});
}
};
} // namespace std