25 const std::string& str) {
26 size_t size = str.size();
27 data.insert(data.end(), (
char*)&size, (
char*)&size +
sizeof(
size_t));
28 data.insert(data.end(), str.begin(), str.end());
31 std::vector<char> data;
35 data.insert(data.end(), (
char*)&it, (
char*)&it +
sizeof(size_t));
37 data.insert(data.end(), (
char*)&it, (
char*)&it +
sizeof(int));
40 [&data](
const std::string& str) { __encode_string(data, str); });
46 auto decode_string = [&](std::string& str) {
47 if (offset +
sizeof(
size_t) > size)
48 throw std::runtime_error(
"decode string failed");
49 size_t str_size = *(
size_t*)(data + offset);
50 offset +=
sizeof(size_t);
51 if (offset + str_size > size)
52 throw std::runtime_error(
"decode string failed");
53 str = std::string(data + offset, str_size);
59 if (offset +
sizeof(
size_t) > size)
60 throw std::runtime_error(
"decode size_t failed");
61 *it = *(
size_t*)(data + offset);
62 offset +=
sizeof(size_t);
65 if (offset +
sizeof(
int) > size)
66 throw std::runtime_error(
"decode int failed");
67 *it = *(
int*)(data + offset);
68 offset +=
sizeof(int);
70 while (offset < size) {
73 msg.
messages.emplace_back(std::move(str));
77 static size_t hash(
const std::vector<char>& data) {
78 return std::hash<std::string_view>{}(std::string_view(
79 reinterpret_cast<const char*
>(data.data()), data.size()));
82template <
class T = ImplCatgory>
84 o <<
"[" << msg.
file <<
":" << msg.
line <<
"]"
86 <<
"[" << msg.
role <<
"]"
87 <<
"[" << msg.
label <<
"]"
90 <<
"[" << msg.
time <<
"us]"
91 <<
"[level=" << msg.
level <<
"] ";
92 for (
const auto& m : msg.
messages) o << m <<
" ";
93 std::cout << std::endl;
std::ostream & operator<<(std::ostream &o, const Message &msg)
static std::vector< char > encode(const Message &msg)
static size_t hash(const std::vector< char > &data)
std::vector< std::string > messages
static void __encode_string(std::vector< char > &data, const std::string &str)
static Message decode(const char *data, size_t size)