dl
dl_log.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <iostream>
16#include <stack>
17#include <array>
18#include <mutex>
19
20#include "system/dl_platform.h"
21
22#if DL_PLATFORM == DL_PLATFORM_WIN32
23#include <stacktrace>
24#include <source_location>
25#define DL_SOURCE_LOCATION std::source_location location = std::source_location::current()
26#define DL_SOURCE_LOCATION_IMP std::source_location location
27#define DL_SOURCE_LOCATION_CALL std::source_location::current()
28#define DL_SOURCE_LOCATION_FORWARD location
29#else
30#define DL_SOURCE_LOCATION const char* file = __FILE__, int line = __LINE__
31#define DL_SOURCE_LOCATION_IMP const char* file, int line
32#define DL_SOURCE_LOCATION_CALL __FILE__, __LINE__
33#define DL_SOURCE_LOCATION_FORWARD file, line
34#endif
35
36namespace dl
37{
39 {
40 const char* _file;
41 const char* _func;
42 int _line;
44 };
45}
46
47#if DL_PLATFORM == DL_PLATFORM_WIN32
48#define DL_MAKE_SOURCE_LOCATION(loc) \
49 LogSourceLocation loc;\
50 loc._file = location.file_name();\
51 loc._func = location.function_name();\
52 loc._line = location.line();\
53 loc._column = location.column();
54#else
55#define DL_MAKE_SOURCE_LOCATION(loc) \
56 LogSourceLocation loc;\
57 loc._file = file;\
58 loc._func = __func__;\
59 loc._line = line;\
60 loc._column = 0;
61#endif
62
63#include <fmt/format.h>
64
65namespace dl
66{
67constexpr std::array<std::string_view, 9> DL_LOG_SYSTEM_LEVLE_NAME = {
68 "damage", "err", "warn", "notice", "msg", "info", "test", "pause",
69};
70
72void Output(std::string_view str);
73
74
77{
78public:
91 struct Setting
92 {
94 bool _bSourceLocation = true;
95 bool _bTimeStamp = true;
96 bool _bStackTrace = false;
97 };
99
102 {
103 _mutexSetting.lock();
104 Setting& ret = _stackSetting.top();
105 _mutexSetting.unlock();
106 return ret;
107 }
108
114 void SetLogFile(std::string_view path, std::string_view name = {});
115 std::string GetLogFile()
116 {
117 return _logFile;
118 }
119
125 {
126 _mutexLevel.lock();
127 _stackLevel.push(level);
128 }
130 {
131 _stackLevel.pop();
132 _mutexLevel.unlock();
133 }
134 bool IsLevelLimit(Level level)
135 {
136 _mutexLevel.lock();
137 bool ret = level > _stackLevel.top();
138 _mutexLevel.unlock();
139 return ret;
140 }
141
144 {
145 return level <= _levelMsgBox;
146 }
147
149 {
150 _levelMsgBox = level;
151 }
152
153
159 void Push(const Setting& setting)
160 {
161 _mutexSetting.lock();
162 _stackSetting.push(setting);
163 }
164
170 void PushLevel(Level level)
171 {
172 Setting setting = GetSetting();
173 setting._level = level;
174 Push(setting);
175 }
176
178 void Pop()
179 {
180 _stackSetting.pop();
181 _mutexSetting.unlock();
182 }
183
184 std::mutex& GetMutex() { return _mutexOutput; }
185
189 static std::string_view Level2String(Level level)
190 {
191 size_t index = (size_t)level;
192 if (index >= DL_LOG_SYSTEM_LEVLE_NAME.size())
193 return "NONE";
194 return DL_LOG_SYSTEM_LEVLE_NAME[index];
195 }
196private:
197 std::stack<Setting> _stackSetting;
198 std::stack<Level> _stackLevel;
199 Level _levelMsgBox;// 弹窗等级
200 static std::string _logFile;// 日志文件夹(为空时不生成)
201 std::recursive_mutex _mutexSetting;// 当前设置锁
202 std::recursive_mutex _mutexLevel; // level锁
203 std::mutex _mutexOutput; // 输出锁
204};
205extern LogSystem* g_log;
208{
209public:
212 {
213 g_log->PushLevelLimit(level);
214 }
216 {
217 g_log->PopLevelLimit();
218 }
219};
220
222{
223public:
224 LogStackTrack(bool open = true)
225 {
226 LogSystem::Setting s = g_log->GetSetting();
227 s._bStackTrace = open;
228 g_log->Push(s);
229 }
231 {
232 g_log->Pop();
233 }
234};
235
239inline void log_easy(std::string_view str)
240{
241 printf("%s", str.data()); // printf线程安全
242 //std::cout << str << std::endl;
243}
244
245void log_pause0(std::string_view str, DL_SOURCE_LOCATION);
246void log_test0(std::string_view str, DL_SOURCE_LOCATION);
247void log_info0(std::string_view str, DL_SOURCE_LOCATION);
248void log_msg0(std::string_view str, DL_SOURCE_LOCATION);
249void log_notice0(std::string_view str, DL_SOURCE_LOCATION);
250void log_warn0(std::string_view str, DL_SOURCE_LOCATION);
251void log_err0(std::string_view str, DL_SOURCE_LOCATION);
252void log_damage0(std::string_view str, DL_SOURCE_LOCATION);
253
255void log(std::string_view str, DL_SOURCE_LOCATION);
256
258void log_err0(const std::exception& e, DL_SOURCE_LOCATION);
259void log_err0(const std::system_error& e, DL_SOURCE_LOCATION);
260
261#define log_(...) log(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
262#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
263#define log_test(...) log_test0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
264#define log_info(...) log_info0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
265#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
266#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
267#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
268#define log_err(...) log_err0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
269#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__), DL_SOURCE_LOCATION_CALL)
270
271
272
273
274}
LogLimit(LogSystem::Level level=LogSystem::Level::DAMAGE)
默认为最高等级,用于屏蔽消息输出
LogStackTrack(bool open=true)
void Pop()
出栈设置
bool IsLevelLimit(Level level)
void PushLevelLimit(Level level)
设置日志输出等级
const Setting & GetSetting()
返回当前设置
void PushLevel(Level level)
入栈设置
std::mutex & GetMutex()
void SetLogFile(std::string_view path, std::string_view name={})
设置日志文件路径(非线程安全)
static std::string_view Level2String(Level level)
日志等级转字符串
bool IsLevelMsgBox(Level level)
检查等级是否可以弹窗(小于等于自己)
void PopLevelLimit()
Level
日志等级 越小越严重
void Push(const Setting &setting)
入栈设置
std::string GetLogFile()
void SetLevelMsgBox(Level level)
设置弹窗等级(小于等于自己才弹,越小越严重)
#define DL_SOURCE_LOCATION
平台宏
constexpr std::array< std::string_view, 9 > DL_LOG_SYSTEM_LEVLE_NAME
void log_msg0(std::string_view str, DL_SOURCE_LOCATION)
void log_easy(std::string_view str)
输出日志
void Output(std::string_view str)
打印到标准输出、vs、日志文件
void log(std::string_view str, DL_SOURCE_LOCATION)
以当前设置输出
LogSystem * g_log
void log_err0(std::string_view str, DL_SOURCE_LOCATION)
void log_info0(std::string_view str, DL_SOURCE_LOCATION)
void log_test0(std::string_view str, DL_SOURCE_LOCATION)
void log_pause0(std::string_view str, DL_SOURCE_LOCATION)
void log_warn0(std::string_view str, DL_SOURCE_LOCATION)
void log_notice0(std::string_view str, DL_SOURCE_LOCATION)
void log_damage0(std::string_view str, DL_SOURCE_LOCATION)