dl
dl_log.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <iostream>
16#include <stack>
17#include <array>
18
19#include "system/dl_platform.h"
20
21#if (DL_PLATFORM == DL_PLATFORM_WIN32)
22#include <stacktrace>
23#include <source_location>
24#endif
25
26#include <fmt/format.h>
27
28namespace dl
29{
30constexpr std::array<std::string_view, 9> DL_LOG_SYSTEM_LEVLE_NAME = {
31 "damage", "err", "warn", "notice", "msg", "info", "test", "pause",
32};
33
34
36{
37public:
39 enum class Level
40 {
41 DAMAGE,
42 ERR,
43 WARN,
44 NOTICE,
45 MSG,
46 INFO,
47 TEST,
48 PAUSE,
49 };
50 struct Setting
51 {
53 bool _bSourceLocation = true;
54 bool _bTimeStamp = true;
55 bool _bStackTrace = false;
56 };
58
61 {
62 return _stackSetting.top();
63 }
64
66 void SetLevelLimit(Level level) { _levelLimit = level; }
67 Level GetLevelLimit() { return _levelLimit; }
68
70 bool IsLevelLimit(Level level)
71 {
72 return level > _levelLimit;
73 }
74
76 bool IsLevelMsgBox(Level level)
77 {
78 return level <= _levelMsgBox;
79 }
80
82 void Push(const Setting& setting)
83 {
84 _stackSetting.push(setting);
85 }
86
88 void PushLevel(Level level)
89 {
90 Setting setting = GetSetting();
91 setting._level = level;
92 _stackSetting.push(setting);
93 }
94
96 void Pop()
97 {
98 _stackSetting.pop();
99 }
100
104 static std::string_view Level2String(Level level)
105 {
106 size_t index = (size_t)level;
107 if (index >= DL_LOG_SYSTEM_LEVLE_NAME.size())
108 return "NONE";
109 return DL_LOG_SYSTEM_LEVLE_NAME[index];
110 }
111private:
112 std::stack<Setting> _stackSetting;
113 Level _levelLimit; // 限制等级
114 Level _levelMsgBox;// 弹窗等级
115};
116extern LogSystem* g_log;
117
119{
120public:
123 {
124 _levelPre = g_log->GetLevelLimit();
125 g_log->SetLevelLimit(level);
126 }
128 {
129 g_log->SetLevelLimit(_levelPre);
130 }
131private:
132 LogSystem::Level _levelPre;
133};
134
138inline void log_easy(std::string_view str)
139{
140 std::cout << str << std::endl;
141}
142
143#if (DL_PLATFORM == DL_PLATFORM_WIN32)
144//void log_imp(std::string_view str, const std::source_location location = std::source_location::current());
145
146
147void log_pause0(std::string_view str, const std::source_location location = std::source_location::current());
148void log_test0(std::string_view str, const std::source_location location = std::source_location::current());
149void log_info0(std::string_view str, const std::source_location location = std::source_location::current());
150void log_msg0(std::string_view str, const std::source_location location = std::source_location::current());
151void log_notice0(std::string_view str, const std::source_location location = std::source_location::current());
152void log_warn0(std::string_view str, const std::source_location location = std::source_location::current());
153void log_err0(std::string_view str, const std::source_location location = std::source_location::current());
154void log_damage0(std::string_view str, const std::source_location location = std::source_location::current());
155
157void log(std::string_view str, const std::source_location location = std::source_location::current());
158
159void log_err0(const std::exception& e, const std::source_location location = std::source_location::current());
160
161#define log_(...) log(fmt::format(__VA_ARGS__), std::source_location::current())
162#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__), std::source_location::current())
163#define log_test(...) log_test0(fmt::format(__VA_ARGS__), std::source_location::current())
164#define log_info(...) log_info0(fmt::format(__VA_ARGS__), std::source_location::current())
165#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__), std::source_location::current())
166#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__), std::source_location::current())
167#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__), std::source_location::current())
168#define log_err(...) log_err0(fmt::format(__VA_ARGS__), std::source_location::current())
169#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__), std::source_location::current())
170
171#endif
172
173#if (DL_PLATFORM == DL_PLATFORM_LINUX) || (DL_PLATFORM == DL_PLATFORM_ANDROID)
174
175void log(std::string_view str);
176
177inline void log_pause0(std::string_view str) { log(str); }
178inline void log_test0(std::string_view str) { log(str); }
179inline void log_info0(std::string_view str) { log(str); }
180inline void log_msg0(std::string_view str) { log(str); }
181inline void log_notice0(std::string_view str) { log(str); }
182inline void log_warn0(std::string_view str) { log(str); }
183inline void log_err0(std::string_view str) { log(str); }
184inline void log_damage0(std::string_view str) { log(str); }
185
186
187void log_err0(const std::exception& e);
188
189#define log_(...) log(fmt::format(__VA_ARGS__))
190#define log_pause(...) log_pause0(fmt::format(__VA_ARGS__))
191#define log_test(...) log_test0(fmt::format(__VA_ARGS__))
192#define log_info(...) log_info0(fmt::format(__VA_ARGS__))
193#define log_msg(...) log_msg0(fmt::format(__VA_ARGS__))
194#define log_notice(...) log_notice0(fmt::format(__VA_ARGS__))
195#define log_warn(...) log_warn0(fmt::format(__VA_ARGS__))
196#define log_err(...) log_err0(fmt::format(__VA_ARGS__))
197#define log_damage(...) log_damage0(fmt::format(__VA_ARGS__))
198#endif
199
200
201
202
203
204
205
206}
LogLimit(LogSystem::Level level=LogSystem::Level::DAMAGE)
默认为最高等级,用于屏蔽消息输出
Definition: dl_log.h:122
void SetLevelLimit(Level level)
设置日志输出等级(大于自己不输出)
Definition: dl_log.h:66
void Pop()
出栈设置
Definition: dl_log.h:96
bool IsLevelLimit(Level level)
检查等级是否被限制输出
Definition: dl_log.h:70
Setting & GetSetting()
返回当前设置
Definition: dl_log.h:60
void PushLevel(Level level)
入栈设置
Definition: dl_log.h:88
Level GetLevelLimit()
Definition: dl_log.h:67
static std::string_view Level2String(Level level)
日志等级转字符串
Definition: dl_log.h:104
bool IsLevelMsgBox(Level level)
检查等级是否可以弹窗(小于等于自己)
Definition: dl_log.h:76
Level
日志等级 越小越严重
Definition: dl_log.h:40
void Push(const Setting &setting)
入栈设置
Definition: dl_log.h:82
平台宏
Definition: dl_array2.h:18
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
Definition: dl_log.h:30
void log_warn0(std::string_view str, const std::source_location location=std::source_location::current())
void log_easy(std::string_view str)
输出日志
Definition: dl_log.h:138
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())