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#endif
26
27#include <fmt/format.h>
28
29namespace dl
30{
31constexpr std::array<std::string_view, 9> DL_LOG_SYSTEM_LEVLE_NAME = {
32 "damage", "err", "warn", "notice", "msg", "info", "test", "pause",
33};
34
35
37{
38public:
51 struct Setting
52 {
54 bool _bSourceLocation = true;
55 bool _bTimeStamp = true;
56 bool _bStackTrace = false;
57 };
59
62 {
63 _mutexSetting.lock();
64 Setting& ret = _stackSetting.top();
65 _mutexSetting.unlock();
66 return ret;
67 }
68
74 void SetLogFile(std::string_view path, std::string_view name = {});
75 std::string GetLogFile()
76 {
77 return _logFile;
78 }
79
85 {
86 _mutexLevel.lock();
87 _stackLevel.push(level);
88 }
90 {
91 _stackLevel.pop();
92 _mutexLevel.unlock();
93 }
94 bool IsLevelLimit(Level level)
95 {
96 _mutexLevel.lock();
97 bool ret = level > _stackLevel.top();
98 _mutexLevel.unlock();
99 return ret;
100 }
101
104 {
105 return level <= _levelMsgBox;
106 }
107
109 {
110 _levelMsgBox = level;
111 }
112
113
119 void Push(const Setting& setting)
120 {
121 _mutexSetting.lock();
122 _stackSetting.push(setting);
123 }
124
130 void PushLevel(Level level)
131 {
132 Setting setting = GetSetting();
133 setting._level = level;
134 Push(setting);
135 }
136
138 void Pop()
139 {
140 _stackSetting.pop();
141 _mutexSetting.unlock();
142 }
143
144 std::mutex& GetMutex() { return _mutexOutput; }
145
149 static std::string_view Level2String(Level level)
150 {
151 size_t index = (size_t)level;
152 if (index >= DL_LOG_SYSTEM_LEVLE_NAME.size())
153 return "NONE";
154 return DL_LOG_SYSTEM_LEVLE_NAME[index];
155 }
156private:
157 std::stack<Setting> _stackSetting;
158 std::stack<Level> _stackLevel;
159 Level _levelMsgBox;// 弹窗等级
160 std::string _logFile;// 日志文件夹(为空时不生成)
161 std::recursive_mutex _mutexSetting;// 当前设置锁
162 std::recursive_mutex _mutexLevel; // level锁
163 std::mutex _mutexOutput; // 输出锁
164};
165extern LogSystem* g_log;
166
168{
169public:
172 {
173 g_log->PushLevelLimit(level);
174 }
176 {
177 g_log->PopLevelLimit();
178 }
179};
180
184inline void log_easy(std::string_view str)
185{
186 printf(str.data()); // printf线程安全
187 //std::cout << str << std::endl;
188}
189
190#if (DL_PLATFORM == DL_PLATFORM_WIN32)
191//void log_imp(std::string_view str, const std::source_location location = std::source_location::current());
192
193
194void log_pause0(std::string_view str, const std::source_location location = std::source_location::current());
195void log_test0(std::string_view str, const std::source_location location = std::source_location::current());
196void log_info0(std::string_view str, const std::source_location location = std::source_location::current());
197void log_msg0(std::string_view str, const std::source_location location = std::source_location::current());
198void log_notice0(std::string_view str, const std::source_location location = std::source_location::current());
199void log_warn0(std::string_view str, const std::source_location location = std::source_location::current());
200void log_err0(std::string_view str, const std::source_location location = std::source_location::current());
201void log_damage0(std::string_view str, const std::source_location location = std::source_location::current());
202
204void log(std::string_view str, const std::source_location location = std::source_location::current());
205
206void log_err0(const std::exception& e, const std::source_location location = std::source_location::current());
207
208#define log_(...) log(fmt::format(__VA_ARGS__), std::source_location::current())
209#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__), std::source_location::current())
210#define log_test(...) log_test0(fmt::format(__VA_ARGS__), std::source_location::current())
211#define log_info(...) log_info0(fmt::format(__VA_ARGS__), std::source_location::current())
212#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__), std::source_location::current())
213#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__), std::source_location::current())
214#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__), std::source_location::current())
215#define log_err(...) log_err0(fmt::format(__VA_ARGS__), std::source_location::current())
216#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__), std::source_location::current())
217
218#endif
219
220#if (DL_PLATFORM == DL_PLATFORM_LINUX) || (DL_PLATFORM == DL_PLATFORM_ANDROID)
221
222void log(std::string_view str);
223
224inline void log_pause0(std::string_view str) { log(str); }
225inline void log_test0(std::string_view str) { log(str); }
226inline void log_info0(std::string_view str) { log(str); }
227inline void log_msg0(std::string_view str) { log(str); }
228inline void log_notice0(std::string_view str) { log(str); }
229inline void log_warn0(std::string_view str) { log(str); }
230inline void log_err0(std::string_view str) { log(str); }
231inline void log_damage0(std::string_view str) { log(str); }
232
233
234void log_err0(const std::exception& e);
235
236#define log_(...) log(fmt::format(__VA_ARGS__))
237#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__))
238#define log_test(...) log_test0(fmt::format(__VA_ARGS__))
239#define log_info(...) log_info0(fmt::format(__VA_ARGS__))
240#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__))
241#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__))
242#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__))
243#define log_err(...) log_err0(fmt::format(__VA_ARGS__))
244#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__))
245#endif
246
247
248
249
250
251
252
253}
LogLimit(LogSystem::Level level=LogSystem::Level::DAMAGE)
默认为最高等级,用于屏蔽消息输出
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)
设置弹窗等级(小于等于自己才弹,越小越严重)
平台宏
void log_msg0(std::string_view str, const std::source_location location=std::source_location::current())
void log_pause0(std::string_view str, const std::source_location location=std::source_location::current())
void log_notice0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr std::array< std::string_view, 9 > DL_LOG_SYSTEM_LEVLE_NAME
void log_warn0(std::string_view str, const std::source_location location=std::source_location::current())
void log_easy(std::string_view str)
输出日志
void log_test0(std::string_view str, const std::source_location location=std::source_location::current())
LogSystem * g_log
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出
void log_info0(std::string_view str, const std::source_location location=std::source_location::current())
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())
void log_damage0(std::string_view str, const std::source_location location=std::source_location::current())