-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.h
76 lines (63 loc) · 2.93 KB
/
logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <span>
struct ByteStr {
std::string_view str;
};
template <> struct fmt::formatter<ByteStr> {
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(ByteStr byte_str, FormatContext &ctx) const {
auto out = ctx.out();
const std::size_t num_bytes = byte_str.str.size();
if (num_bytes > 0) {
const uint8_t first_byte = byte_str.str[0];
if (first_byte == '*') {
out = format_to(out, "??");
} else {
out = format_to(out, "{:02x}", first_byte);
}
std::span<uint8_t> remainder_byte_span{(uint8_t *)byte_str.str.data() + 1,
num_bytes - 1};
for (uint8_t c : remainder_byte_span) {
if (c == '*') {
out = format_to(out, " ??");
} else {
out = format_to(out, " {:02x}", c);
}
}
}
return out;
}
};
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define COMMON_FORMATTER(name, format, ...) \
try { \
fflush(stdout); \
fmt::print("[" name "] " format "\n", __VA_ARGS__); \
fflush(stdout); \
} catch (fmt::format_error & e) { \
puts("Formatting exception:" format); \
puts(__FILE__ " at " TOSTRING(__LINE__)); \
puts(e.what()); \
} catch (...) { \
}
#define PANIC(format, ...) \
do { \
COMMON_FORMATTER("panic", format, __VA_ARGS__); \
std::exit(-1); \
} while (false)
#define ERR(format, ...) \
do { \
COMMON_FORMATTER("error", format, __VA_ARGS__); \
} while (false)
#define DEBUG(format, ...) \
do { \
COMMON_FORMATTER("debug", format, __VA_ARGS__); \
} while (false)
#define INFO(format, ...) \
do { \
COMMON_FORMATTER("info", format, __VA_ARGS__); \
} while (false)