-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.hpp
More file actions
131 lines (101 loc) · 3.33 KB
/
logger.hpp
File metadata and controls
131 lines (101 loc) · 3.33 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by Administrator on 2026/3/8.
//
#ifndef GB28181_LOGGER_HPP
#define GB28181_LOGGER_HPP
#include <cstdarg>
#include <string>
#include <vector>
enum class LogLevel {
DEBUG,
INFO,
WARN,
ERROR
};
class Logger {
public:
explicit Logger(const char* tag) : _tag_ptr(tag) {}
// ========== 简单单行边框日志 ==========
void d(const char* msg) const;
void i(const char* msg) const;
void w(const char* msg) const;
void e(const char* msg) const;
// ========== 带格式的边框日志 ==========
template <typename... Args>
void dFmt(const char* fmt, Args... args) {
log_formatted(LogLevel::DEBUG, fmt, args...);
}
template <typename... Args>
void iFmt(const char* fmt, Args... args) {
log_formatted(LogLevel::INFO, fmt, args...);
}
template <typename... Args>
void wFmt(const char* fmt, Args... args) {
log_formatted(LogLevel::WARN, fmt, args...);
}
template <typename... Args>
void eFmt(const char* fmt, Args... args) {
log_formatted(LogLevel::ERROR, fmt, args...);
}
// ========== 多行内容边框日志(流式API) ==========
// 使用方式: box().add("行1").add("行2").print();
class BoxBuilder {
public:
BoxBuilder(Logger& logger, LogLevel level) : _logger(logger), _level(level) {}
// 添加一行内容
BoxBuilder& add(const std::string& line);
// 添加多行内容(针对sdp或者xml这种带有换行的内容块)
BoxBuilder& addBlock(const std::string& content);
// 添加格式化行
__attribute__((format(printf, 2, 3)))
BoxBuilder& addFmt(const char* fmt, ...) {
char buffer[256];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
return add(buffer);
}
// 打印到logcat
void print() const;
private:
Logger& _logger;
LogLevel _level;
std::vector<std::string> _lines;
};
// 构建多行边框
BoxBuilder box(LogLevel level = LogLevel::INFO);
BoxBuilder dBox() {
return box(LogLevel::DEBUG);
}
BoxBuilder iBox() {
return box(LogLevel::INFO);
}
BoxBuilder wBox() {
return box(LogLevel::WARN);
}
BoxBuilder eBox() {
return box(LogLevel::ERROR);
}
private:
const char* _tag_ptr;
static constexpr auto TAG_MAX_WIDTH = 16;
static constexpr auto DEFAULT_WIDTH = 48;
static constexpr auto H_LINE = "─";
static constexpr auto V_LINE = "│";
static constexpr auto TOP_LEFT = "┌";
static constexpr auto TOP_RIGHT = "┐";
static constexpr auto BOTTOM_LEFT = "└";
static constexpr auto BOTTOM_RIGHT = "┘";
void print_border(const char* left, const char* fill, const char* right, LogLevel level) const;
void print_line(const std::string& content, LogLevel level) const;
void log_raw(LogLevel level, const char* msg) const;
template <typename... Args>
void log_formatted(const LogLevel level, const char* fmt, Args... args) {
char buffer[512];
snprintf(buffer, sizeof(buffer), fmt, args...);
print_box(level, buffer);
}
void print_box(LogLevel level, const char* content) const;
};
#endif //GB28181_LOGGER_HPP