-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.cpp
More file actions
144 lines (122 loc) · 3.74 KB
/
logger.cpp
File metadata and controls
144 lines (122 loc) · 3.74 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
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by Administrator on 2026/3/8.
//
#include "logger.hpp"
#include <iostream>
#include <sstream>
// ANSI 颜色码定义
#define COLOR_RESET "\033[0m"
#define COLOR_DEBUG "\033[32m" // 绿色
#define COLOR_INFO "\033[36m" // 青色
#define COLOR_WARN "\033[33m" // 黄色
#define COLOR_ERROR "\033[31m" // 红色
// 获取 LogLevel 对应的颜色码
static const char* levelColor(const LogLevel level) {
switch (level) {
case LogLevel::DEBUG:
return COLOR_DEBUG;
case LogLevel::INFO:
return COLOR_INFO;
case LogLevel::WARN:
return COLOR_WARN;
case LogLevel::ERROR:
return COLOR_ERROR;
default:
return COLOR_RESET;
}
}
void Logger::print_border(const char* left, const char* fill, const char* right, const LogLevel level) const {
std::string border;
const char* color = levelColor(level);
border += color;
border += left;
for (int i = 0; i < DEFAULT_WIDTH; ++i) {
border += fill;
}
border += right;
border += COLOR_RESET;
log_raw(level, border.c_str());
}
void Logger::log_raw(const LogLevel level, const char* msg) const {
std::string levelStr;
const char* color = levelColor(level);
switch (level) {
case LogLevel::DEBUG:
levelStr = "[DEBUG]";
break;
case LogLevel::INFO:
levelStr = "[INFO ]";
break;
case LogLevel::WARN:
levelStr = "[WARN ]";
break;
case LogLevel::ERROR:
levelStr = "[ERROR]";
break;
}
// 格式化 _tag_ptr 的宽度
const std::string tag_str(_tag_ptr);
if (tag_str.length() > TAG_MAX_WIDTH) {
// 超过长度时截断并添加 "..."
tag_str.substr(0, TAG_MAX_WIDTH - 3) + "...";
}
const auto tag = tag_str + std::string(TAG_MAX_WIDTH - tag_str.length(), ' ');
std::cout << color << levelStr << COLOR_RESET << " [" << tag << "] " << msg << std::endl;
}
void Logger::print_line(const std::string& content, const LogLevel level) const {
std::string line;
const char* color = levelColor(level);
line += color;
line += V_LINE;
line += " ";
line += content;
line += COLOR_RESET;
log_raw(level, line.c_str());
}
void Logger::print_box(const LogLevel level, const char* content) const {
print_border(TOP_LEFT, H_LINE, TOP_RIGHT, level);
print_line(content, level);
print_border(BOTTOM_LEFT, H_LINE, BOTTOM_RIGHT, level);
}
// 简单单行边框
void Logger::d(const char* msg) const {
print_box(LogLevel::DEBUG, msg);
}
void Logger::i(const char* msg) const {
print_box(LogLevel::INFO, msg);
}
void Logger::w(const char* msg) const {
print_box(LogLevel::WARN, msg);
}
void Logger::e(const char* msg) const {
print_box(LogLevel::ERROR, msg);
}
Logger::BoxBuilder& Logger::BoxBuilder::add(const std::string& line) {
_lines.push_back(line);
return *this;
}
Logger::BoxBuilder& Logger::BoxBuilder::addBlock(const std::string& content) {
std::istringstream stream(content);
std::string line;
while (std::getline(stream, line)) {
_lines.push_back(line);
}
return *this;
}
void Logger::BoxBuilder::print() const {
if (_lines.empty())
return;
// 上边框
_logger.print_border(TOP_LEFT, H_LINE, TOP_RIGHT, _level);
// 内容行
const char* color = levelColor(_level);
for (const auto& line : _lines) {
std::string output = std::string(color) + V_LINE + " " + line + COLOR_RESET;
_logger.log_raw(_level, output.c_str());
}
// 下边框
_logger.print_border(BOTTOM_LEFT, H_LINE, BOTTOM_RIGHT, _level);
}
Logger::BoxBuilder Logger::box(LogLevel level) {
return {*this, level};
}