dl
dl_log.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <iostream>
16#include <stack>
17#include <array>
18
19#include "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:
122 {
123 _levelPre = g_log->GetLevelLimit();
124 g_log->SetLevelLimit(level);
125 }
127 {
128 g_log->SetLevelLimit(_levelPre);
129 }
130private:
131 LogSystem::Level _levelPre;
132};
133
137#if (DL_PLATFORM == DL_PLATFORM_WIN32)
138//void log_imp(std::string_view str, const std::source_location location = std::source_location::current());
139
140
141void log_pause0(std::string_view str, const std::source_location location = std::source_location::current());
142void log_test0(std::string_view str, const std::source_location location = std::source_location::current());
143void log_info0(std::string_view str, const std::source_location location = std::source_location::current());
144void log_msg0(std::string_view str, const std::source_location location = std::source_location::current());
145void log_notice0(std::string_view str, const std::source_location location = std::source_location::current());
146void log_warn0(std::string_view str, const std::source_location location = std::source_location::current());
147void log_err0(std::string_view str, const std::source_location location = std::source_location::current());
148void log_damage0(std::string_view str, const std::source_location location = std::source_location::current());
149
151void log(std::string_view str, const std::source_location location = std::source_location::current());
152
153void log_err0(const std::exception& e, const std::source_location location = std::source_location::current());
154
155#define log_(str, ...) log(fmt::format(str, __VA_ARGS__), std::source_location::current())
156#define log_pause(str, ...) log_pause0(fmt::format(str, __VA_ARGS__), std::source_location::current())
157#define log_test(str, ...) log_test0(fmt::format(str, __VA_ARGS__), std::source_location::current())
158#define log_info(str, ...) log_info0(fmt::format(str, __VA_ARGS__), std::source_location::current())
159#define log_msg(str, ...) log_msg0(fmt::format(str, __VA_ARGS__), std::source_location::current())
160#define log_notice(str, ...) log_notice0(fmt::format(str, __VA_ARGS__), std::source_location::current())
161#define log_warn(str, ...) log_warn0(fmt::format(str, __VA_ARGS__), std::source_location::current())
162#define log_err(str, ...) log_err0(fmt::format(str, __VA_ARGS__), std::source_location::current())
163#define log_damage(str, ...) log_damage0(fmt::format(str, __VA_ARGS__), std::source_location::current())
164
165#endif
166
167#if (DL_PLATFORM == DL_PLATFORM_LINUX) || (DL_PLATFORM == DL_PLATFORM_ANDROID)
168
169void log(std::string_view str);
170
171inline void log_pause0(std::string_view str) { log(str); }
172inline void log_test0(std::string_view str) { log(str); }
173inline void log_info0(std::string_view str) { log(str); }
174inline void log_msg0(std::string_view str) { log(str); }
175inline void log_notice0(std::string_view str) { log(str); }
176inline void log_warn0(std::string_view str) { log(str); }
177inline void log_err0(std::string_view str) { log(str); }
178inline void log_damage0(std::string_view str) { log(str); }
179
180
181void log_err0(const std::exception& e);
182
183#define log_(str, ...) log(fmt::format(str, __VA_ARGS__))
184#define log_pause(str, ...) log_pause0(fmt::format(str, __VA_ARGS__))
185#define log_test(str, ...) log_test0(fmt::format(str, __VA_ARGS__))
186#define log_info(str, ...) log_info0(fmt::format(str, __VA_ARGS__))
187#define log_msg(str, ...) log_msg0(fmt::format(str, __VA_ARGS__))
188#define log_notice(str, ...) log_notice0(fmt::format(str, __VA_ARGS__))
189#define log_warn(str, ...) log_warn0(fmt::format(str, __VA_ARGS__))
190#define log_err(str, ...) log_err0(fmt::format(str, __VA_ARGS__))
191#define log_damage(str, ...) log_damage0(fmt::format(str, __VA_ARGS__))
192#endif
193
194
195
196
197
198
199
200}
LogLimit(LogSystem::Level level=LogSystem::Level::DAMAGE)
Definition: dl_log.h:121
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:17
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_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())