#pragma once #include #include #include #include #include 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: // // out << buffer_printer(my_buffer); // struct buffer_printer { std::basic_string_view buf; // Constructed from any type of string_view for a single-byte T (char, std::byte, uint8_t, // etc.) template > explicit buffer_printer(std::basic_string_view buf) : buf{reinterpret_cast(buf.data()), buf.size()} {} // Constructed from any type of lvalue string for a single-byte T (char, std::byte, uint8_t, // etc.) template > explicit buffer_printer(const std::basic_string& buf) : buffer_printer(std::basic_string_view{buf}) {} // *Not* constructable from a string rvalue (because we only hold a view and do not take // ownership). template > explicit buffer_printer(std::basic_string&& buf) = delete; // Constructable from a (T*, size) argument pair, for byte-sized T's. template > explicit buffer_printer(const T* data, size_t size) : buffer_printer(std::basic_string_view{data, size}) {} // llarp_buffer_t printer: explicit buffer_printer(const llarp_buffer_t& buf) : buffer_printer(std::basic_string_view{buf.base, buf.sz}) {} }; std::ostream& operator<<(std::ostream& o, const buffer_printer& bp); } // namespace llarp