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)/* || (DL_PLATFORM == DL_PLATFORM_LINUX)*/)
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
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 static std::string _logFile;// 日志文件夹(为空时不生成)
161 std::recursive_mutex _mutexSetting;// 当前设置锁
162 std::recursive_mutex _mutexLevel; // level锁
163 std::mutex _mutexOutput; // 输出锁
164};
165extern LogSystem* g_log;
168{
169public:
172 {
173 g_log->PushLevelLimit(level);
174 }
176 {
177 g_log->PopLevelLimit();
178 }
179};
180
182{
183public:
184 LogStackTrack(bool open = true)
185 {
186 LogSystem::Setting s = g_log->GetSetting();
187 s._bStackTrace = open;
188 g_log->Push(s);
189 }
191 {
192 g_log->Pop();
193 }
194};
195
199inline void log_easy(std::string_view str)
200{
201 printf("%s", str.data()); // printf线程安全
202 //std::cout << str << std::endl;
203}
204
205#if ((DL_PLATFORM == DL_PLATFORM_WIN32)/* || (DL_PLATFORM == DL_PLATFORM_LINUX)*/)
206//void log_imp(std::string_view str, const std::source_location location = std::source_location::current());
207
208
209void log_pause0(std::string_view str, const std::source_location location = std::source_location::current());
210void log_test0(std::string_view str, const std::source_location location = std::source_location::current());
211void log_info0(std::string_view str, const std::source_location location = std::source_location::current());
212void log_msg0(std::string_view str, const std::source_location location = std::source_location::current());
213void log_notice0(std::string_view str, const std::source_location location = std::source_location::current());
214void log_warn0(std::string_view str, const std::source_location location = std::source_location::current());
215void log_err0(std::string_view str, const std::source_location location = std::source_location::current());
216void log_damage0(std::string_view str, const std::source_location location = std::source_location::current());
217
219void log(std::string_view str, const std::source_location location = std::source_location::current());
220
221void log_err0(const std::exception& e, const std::source_location location = std::source_location::current());
222void log_err0(const std::system_error& e, const std::source_location location = std::source_location::current());
223
224#define log_(...) log(fmt::format(__VA_ARGS__), std::source_location::current())
225#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__), std::source_location::current())
226#define log_test(...) log_test0(fmt::format(__VA_ARGS__), std::source_location::current())
227#define log_info(...) log_info0(fmt::format(__VA_ARGS__), std::source_location::current())
228#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__), std::source_location::current())
229#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__), std::source_location::current())
230#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__), std::source_location::current())
231#define log_err(...) log_err0(fmt::format(__VA_ARGS__), std::source_location::current())
232#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__), std::source_location::current())
233
234
235#else
236
237void log(std::string_view str);
238
239inline void log_pause0(std::string_view str) { log(str); }
240inline void log_test0(std::string_view str) { log(str); }
241inline void log_info0(std::string_view str) { log(str); }
242inline void log_msg0(std::string_view str) { log(str); }
243inline void log_notice0(std::string_view str) { log(str); }
244inline void log_warn0(std::string_view str) { log(str); }
245inline void log_err0(std::string_view str) { log(str); }
246inline void log_damage0(std::string_view str) { log(str); }
247
248void log_err0(const std::exception& e);
249void log_err0(const std::system_error& e);
250
251#define log_(...) log(fmt::format(__VA_ARGS__))
252#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__))
253#define log_test(...) log_test0(fmt::format(__VA_ARGS__))
254#define log_info(...) log_info0(fmt::format(__VA_ARGS__))
255#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__))
256#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__))
257#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__))
258#define log_err(...) log_err0(fmt::format(__VA_ARGS__))
259#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__))
260#endif
261
262
263
264
265
266
267
268}
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)
设置弹窗等级(小于等于自己才弹,越小越严重)
平台宏
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())