mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-15 12:13:24 +00:00
b81f7025c9
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).
62 lines
2.0 KiB
C++
62 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <llarp/util/buffer.hpp>
|
|
#include <llarp/util/formattable.hpp>
|
|
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
#include <cstddef>
|
|
#include <iosfwd>
|
|
|
|
namespace llarp
|
|
{
|
|
// Buffer printer lets you print a string as a nicely formatted buffer with a hex breakdown and
|
|
// visual representation of the data for logging purposes. Wraps the string data with a object
|
|
// that prints the buffer format during output; use as:
|
|
//
|
|
// fmt::print("{}", buffer_printer(my_buffer));
|
|
//
|
|
// or similarly in a log statement.
|
|
//
|
|
struct buffer_printer
|
|
{
|
|
std::basic_string_view<std::byte> buf;
|
|
|
|
// Constructed from any type of string_view<T> for a single-byte T (char, std::byte, uint8_t,
|
|
// etc.)
|
|
template <typename T, typename = std::enable_if_t<sizeof(T) == 1>>
|
|
explicit buffer_printer(std::basic_string_view<T> buf)
|
|
: buf{reinterpret_cast<const std::byte*>(buf.data()), buf.size()}
|
|
{}
|
|
|
|
// Constructed from any type of lvalue string<T> for a single-byte T (char, std::byte, uint8_t,
|
|
// etc.)
|
|
template <typename T, typename = std::enable_if_t<sizeof(T) == 1>>
|
|
explicit buffer_printer(const std::basic_string<T>& buf)
|
|
: buffer_printer(std::basic_string_view<T>{buf})
|
|
{}
|
|
|
|
// *Not* constructable from a string<T> rvalue (because we only hold a view and do not take
|
|
// ownership).
|
|
template <typename T, typename = std::enable_if_t<sizeof(T) == 1>>
|
|
explicit buffer_printer(std::basic_string<T>&& buf) = delete;
|
|
|
|
// Constructable from a (T*, size) argument pair, for byte-sized T's.
|
|
template <typename T, typename = std::enable_if_t<sizeof(T) == 1>>
|
|
explicit buffer_printer(const T* data, size_t size)
|
|
: buffer_printer(std::basic_string_view<T>{data, size})
|
|
{}
|
|
|
|
// llarp_buffer_t printer:
|
|
explicit buffer_printer(const llarp_buffer_t& buf)
|
|
: buffer_printer(std::basic_string_view<byte_t>{buf.base, buf.sz})
|
|
{}
|
|
|
|
std::string
|
|
ToString() const;
|
|
};
|
|
|
|
template <>
|
|
constexpr inline bool IsToStringFormattable<buffer_printer> = true;
|
|
} // namespace llarp
|