xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
message.hpp
浏览该文件的文档.
1#pragma once
2#include <algorithm>
3#include <cstddef>
4#include <functional>
5#include <iostream>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9#include <vector>
10
11namespace xclogger {
12class ImplCatgory;
13struct Message {
14 std::string role;
15 std::string label;
16 std::string file;
17 std::string function;
18 size_t time;
19 size_t process_id;
20 size_t thread_id;
21 int line;
22 int level = 0;
23 std::vector<std::string> messages;
24 static void __encode_string(std::vector<char>& data,
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());
29 }
30 static std::vector<char> encode(const Message& msg) {
31 std::vector<char> data;
32 for (auto& it : {msg.role, msg.label, msg.file, msg.function})
33 __encode_string(data, it);
34 for (auto it : {msg.time, msg.process_id, msg.thread_id})
35 data.insert(data.end(), (char*)&it, (char*)&it + sizeof(size_t));
36 for (auto it : {msg.line, msg.level})
37 data.insert(data.end(), (char*)&it, (char*)&it + sizeof(int));
38 std::for_each(
39 msg.messages.begin(), msg.messages.end(),
40 [&data](const std::string& str) { __encode_string(data, str); });
41 return data;
42 }
43 static Message decode(const char* data, size_t size) {
44 Message msg;
45 size_t offset = 0;
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);
54 offset += str_size;
55 };
56 for (auto& it : {&msg.role, &msg.label, &msg.file, &msg.function})
57 decode_string(*it);
58 for (auto it : {&msg.time, &msg.process_id, &msg.thread_id}) {
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);
63 }
64 for (auto it : {&msg.line, &msg.level}) {
65 if (offset + sizeof(int) > size)
66 throw std::runtime_error("decode int failed");
67 *it = *(int*)(data + offset);
68 offset += sizeof(int);
69 }
70 while (offset < size) {
71 std::string str;
72 decode_string(str);
73 msg.messages.emplace_back(std::move(str));
74 }
75 return msg;
76 }
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()));
80 }
81};
82template <class T = ImplCatgory>
83inline std::ostream& operator<<(std::ostream& o, const Message& msg) {
84 o << "[" << msg.file << ":" << msg.line << "]"
85 << "[" << msg.function << "]"
86 << "[" << msg.role << "]"
87 << "[" << msg.label << "]"
88 << "[" << msg.process_id << "]"
89 << "[" << msg.thread_id << "]"
90 << "[" << msg.time << "us]"
91 << "[level=" << msg.level << "] ";
92 for (const auto& m : msg.messages) o << m << " ";
93 std::cout << std::endl;
94 return o;
95}
96} // namespace xclogger
std::ostream & operator<<(std::ostream &o, const Message &msg)
Definition message.hpp:83
static std::vector< char > encode(const Message &msg)
Definition message.hpp:30
static size_t hash(const std::vector< char > &data)
Definition message.hpp:77
std::string file
Definition message.hpp:16
std::string role
Definition message.hpp:14
std::vector< std::string > messages
Definition message.hpp:23
std::string label
Definition message.hpp:15
static void __encode_string(std::vector< char > &data, const std::string &str)
Definition message.hpp:24
static Message decode(const char *data, size_t size)
Definition message.hpp:43
std::string function
Definition message.hpp:17