ZCLibLog
载入中...
搜索中...
未找到
logger_types.hpp
浏览该文件的文档.
1//
2// Created by wanjiangzhi on 2026/3/30.
3//
4
5#ifndef ZCLIBLOG_LOGGER_TYPES_HPP
6#define ZCLIBLOG_LOGGER_TYPES_HPP
7
8#include <string>
9#include <chrono>
10#include <memory>
11#include <utility>
12#include <type_traits>
13
14#include "logger_macros.h"
15
16namespace ZCLibLog {
18 using LogLevelBase = uint16_t;
19
24 #define ZCLIBLOG_HELPER_LEVELS(Register) \
25 Register(ALL, 0) \
26 Register(TRACE, 1) \
27 Register(DEBUG, 2) \
28 Register(INFO, 3) \
29 Register(WARN, 4) \
30 Register(ERROR, 5) \
31 Register(FATAL, 6) \
32 Register(OFF, std::numeric_limits<LogLevelBase>::max())
33
35 enum class LogLevel : LogLevelBase {
36 #define ZCLIBLOG_HELPER_ENUM_CASE(name, value) name = value,
38 #undef ZCLIBLOG_HELPER_ENUM_CASE
39 };
40
46 inline const char* LogLevelToString(const LogLevel level) {
47 switch (level) {
48 #define ZCLIBLOG_HELPER_ENUM_CASE(name, value) case LogLevel::name: return #name;
50 #undef ZCLIBLOG_HELPER_ENUM_CASE
51 }
52 return "UNKNOWN";
53 }
54
55 #undef ZCLIBLOG_HELPER_LEVELS
56
61 struct LogPack {
63 const std::string* name = {};
65 uint64_t time = {};
68 };
69
74 struct LogLevelCfg {
79
80 // ReSharper disable once CppNonExplicitConvertingConstructor
85 LogLevelCfg(const LogLevel level = {}) : min_level(level),
86 max_level(LogLevel::OFF) {}
87
95 };
96
98 using ELogLevel = const LogLevel;
100 using ELString = const std::string&;
101
108 virtual void do_execute(ELString, ELogLevel) = 0;
110 virtual ~executor_api() = default;
111
112 protected:
119 };
120
126 template <typename Executor>
127 using is_executor_api = std::is_base_of<executor_api, Executor>;
128
133 class executor {
134 public:
135 using base_ptr_type = std::shared_ptr<executor_api>;
136 using inside_type = base_ptr_type::element_type;
137
138 private:
139 base_ptr_type executor_ptr;
140
141 public:
142 executor() = default;
143 // ReSharper disable once CppNonExplicitConvertingConstructor
151 executor(inside_type* executor_ptr) : executor_ptr(executor_ptr) {}
152
160 template <typename Executor, typename... Args>
161 static executor make(Args&&... args) {
162 static_assert(is_executor_api<Executor>::value, "Executor must be a real executor");
163
165 made.executor_ptr = std::move(std::make_shared<Executor>(std::forward<Args>(args)...));
166
167 return std::move(made);
168 }
169
172 return executor_ptr.get();
173 }
174
177 // return executor_ptr.unique();
178 // unique成员函数在MSVC下不存在?不知为何通不过多平台构建测试
179 return executor_ptr.use_count() == 1;
180 // 这里使用等价逻辑代替
181 }
182
185 return executor_ptr.use_count();
186 }
187
190 return executor_ptr.operator->();
191 }
192
195 return executor_ptr.operator*();
196 }
197
198 // ReSharper disable once CppNonExplicitConversionOperator
200 operator bool() const noexcept {
201 return executor_ptr.get() != nullptr;
202 }
203 };
204
206 using FLogPack = const LogPack&;
208 using FLString = std::string;
209
214 struct format_api {
215 protected:
222 };
223
230 template <typename FormatAPI>
232 : std::integral_constant<
233 bool,
234 std::is_base_of<format_api, FormatAPI>::value &&
235 !std::is_same<FormatAPI, format_api>::value
236 > {};
237
244 template <typename Formatter>
246 : std::integral_constant<
247 bool,
248 std::is_base_of<format_api, typename Formatter::Format_API>::value &&
249 !std::is_same<typename Formatter::Format_API, format_api>::value &&
250 std::is_base_of<typename Formatter::Format_API, Formatter>::value &&
251 !std::is_same<typename Formatter::Format_API, Formatter>::value
252 > {};
253
259 template <typename Logger>
261 protected:
264 const auto now = std::chrono::system_clock::now();
265 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
266 now.time_since_epoch()
267 ).count();
268
269 LogPack p;
270 p.name = &m_logger->name();
271 p.level = m_level;
272 p.time = ms;
273
274 return p;
275 }
276
279 if (
280 m_logger->has_executor() &&
281 m_logger->be_executable(level())
282 )
283 return true;
284 return {};
285 }
286
287 const Logger* const m_logger{};
289 public:
291 ZCLibLog_NODISCARD const LogLevel& level() const noexcept {
292 return m_level;
293 }
294
300 LogTagBase(const Logger* const logger, const LogLevel level) : m_logger(logger),
301 m_level(level) {}
302 };
303
310 template <typename FormatterAPI, typename Logger>
311 struct LogTag : LogTagBase<Logger> {
312 using LogTagBase<Logger>::LogTagBase;
313 static_assert(
315 "Formatter must be format_api"
316 );
317 };
318}
319
320#endif //ZCLIBLOG_LOGGER_TYPES_HPP
根基Tag类,构造无用,仅继承
Definition logger_types.hpp:260
LogTagBase(const Logger *const logger, const LogLevel level)
构造Tag
Definition logger_types.hpp:300
ZCLibLog_NODISCARD const LogLevel & level() const noexcept
获取当前Tag的等级
Definition logger_types.hpp:291
ZCLibLog_NODISCARD bool check_executable() const
检查是否可执行
Definition logger_types.hpp:278
ZCLibLog_NODISCARD LogPack get_log_pack() const
获取当前的日志信息包
Definition logger_types.hpp:263
const LogLevel m_level
Definition logger_types.hpp:288
const Logger *const m_logger
Definition logger_types.hpp:287
执行器包装类
Definition logger_types.hpp:133
std::shared_ptr< executor_api > base_ptr_type
Definition logger_types.hpp:135
static executor make(Args &&... args)
通过工厂函数构造
Definition logger_types.hpp:161
base_ptr_type::element_type inside_type
Definition logger_types.hpp:136
inside_type & operator*() const noexcept
获取内部执行器对象
Definition logger_types.hpp:194
ZCLibLog_NODISCARD bool unique() const noexcept
获取执行器是否独占状态
Definition logger_types.hpp:176
inside_type * operator->() const noexcept
访问内部执行器内部成员
Definition logger_types.hpp:189
executor(inside_type *executor_ptr)
通过裸指针构造
Definition logger_types.hpp:151
ZCLibLog_NODISCARD long use_count() const noexcept
获取执行器使用计数
Definition logger_types.hpp:184
ZCLibLog_NODISCARD inside_type * get() const noexcept
获取内部执行器的指针
Definition logger_types.hpp:171
#define ZCLibLog_NODISCARD
Definition logger_macros.h:50
#define ZCLIBLOG_HELPER_LEVELS(Register)
注册日志等级
Definition logger_types.hpp:24
ZCLibLog的命名空间
Definition android_log.hpp:16
std::is_base_of< executor_api, Executor > is_executor_api
判断是否是基于executor api
Definition logger_types.hpp:127
uint16_t LogLevelBase
日志等级的基础类型
Definition logger_types.hpp:18
const std::string & ELString
执行器接受的字符串
Definition logger_types.hpp:100
std::string FLString
格格式化输出的字符串
Definition logger_types.hpp:208
LogLevel
一些日志等级
Definition logger_types.hpp:35
ZCLIBLOG_HELPER_ENUM_CASE(name, value)
const char * LogLevelToString(const LogLevel level)
获取日志等级的文本字符串
Definition logger_types.hpp:46
Log等级范围
Definition logger_types.hpp:74
LogLevel max_level
配置的最高等级
Definition logger_types.hpp:78
LogLevelCfg(const LogLevel min_level, const LogLevel max_level)
等级范围构造
Definition logger_types.hpp:93
LogLevelCfg(const LogLevel level={})
单等级构造
Definition logger_types.hpp:85
LogLevel min_level
配置的最低等级
Definition logger_types.hpp:76
Log格式化的参数包
Definition logger_types.hpp:61
const std::string * name
该日志的名字
Definition logger_types.hpp:63
uint64_t time
该日志的时间戳
Definition logger_types.hpp:65
LogLevel level
该日志的级别
Definition logger_types.hpp:67
泛型模板,无法特化
Definition logger_types.hpp:311
执行器的基类抽象类
Definition logger_types.hpp:106
LogLevel LogLevel
简化写法
Definition logger_types.hpp:118
virtual ~executor_api()=default
自定义执行器可能重载的析构函数
virtual void do_execute(ELString, ELogLevel)=0
自定义执行器需要重载的执行函数
ELogLevel ELogLevel
简化写法
Definition logger_types.hpp:116
ELString ELString
简化写法
Definition logger_types.hpp:114
定义了一些简化写法
Definition logger_types.hpp:214
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
FLString FLString
简化写法
Definition logger_types.hpp:219
LogLevel LogLevel
简化写法
Definition logger_types.hpp:221
判断是否是基于format api
Definition logger_types.hpp:236
判断是否是是基于format api的formatter
Definition logger_types.hpp:252