ZCLibLog
载入中...
搜索中...
未找到
logger_async.hpp
浏览该文件的文档.
1// Copyright 2026 CZF-H
2// Licensed under the Apache License, Version 2.0
3
4//
5// Created by wanjiangzhi on 2026/3/30.
6//
7
8#ifndef ZCLIBLOG_LOGGER_ASYNC_HPP
9#define ZCLIBLOG_LOGGER_ASYNC_HPP
10
11#include <atomic>
12#include <condition_variable>
13#include <functional>
14#include <memory>
15#include <queue>
16#include <thread>
17#include <mutex>
18
19#include "logger_base.hpp"
20
21#if ZCLIBLOG_LOGGER_CONFIGURATIONS_LOGGER_ASYNC_MUTEX
22#ifndef ZCLibLog_MUTEX
23#if ZCLibLog_CPP >= 17
24#include <shared_mutex>
25#define ZCLibLog_MUTEX std::shared_mutex
26#else
27#define ZCLibLog_MUTEX std::mutex
28#endif
29#endif
30#endif
31
32namespace ZCLibLog {
37 namespace details {
42 class ThreadPool {
43 public:
48 explicit ThreadPool(const size_t numThreads = 1) : stop(false) {
49 for (size_t i = 0; i < numThreads; ++i) {
50 workers.emplace_back(
51 [this] {
52 while (true) {
53 std::function<void()> task;
54 {
55 std::unique_lock<std::mutex> lock(mtx);
56 cv.wait(lock, [this] { return stop || !tasks.empty(); });
57 if (stop && tasks.empty()) return;
58 task = std::move(tasks.front());
59 tasks.pop();
60 }
61 task();
62 }
63 }
64 );
65 }
66 }
67
70 {
71 std::lock_guard<std::mutex> lock(mtx);
72 stop = true;
73 }
74 cv.notify_all();
75 for (auto& t : workers) t.join();
76 }
77
82 void submit(std::function<void()> task) {
83 {
84 std::lock_guard<std::mutex> lock(mtx);
85 tasks.push(std::move(task));
86 }
87 cv.notify_one();
88 }
89
90 private:
91 std::vector<std::thread> workers;
92 std::queue<std::function<void()>> tasks;
93 std::mutex mtx;
94 std::condition_variable cv;
95 std::atomic<bool> stop;
96 };
97 }
98
100 static details::ThreadPool LoggerAsync_ThreadPool{ZCLIBLOG_LOGGER_CONFIGURATIONS_ASYNC_THREAD_NUM};
101
107 template <
108 typename Formatter
109 #if ZCLIBLOG_LOGGER_CONFIGURATIONS_DEFAULT_CSNPRINTF
110 = formatters::csnprintf
111 #endif
112 >
113 struct LoggerAsync : BaseLogger<Formatter> {
114 using BaseLogger<Formatter>::BaseLogger;
116
122 void execute(std::string& message, const LogLevel level) const {
123 if (!message.empty()) {
124 auto message_p = std::make_shared<std::string>(std::move(message));
125 LoggerAsync_ThreadPool.submit(
126 [this, message_p, level] {
127 #if ZCLIBLOG_LOGGER_CONFIGURATIONS_LOGGER_ASYNC_MUTEX
128 std::lock_guard<ZCLibLog_MUTEX> lock(m_mutex);
129 #endif
130 // ReSharper disable once CppUseStructuredBinding
131 // ReSharper disable once CppUseElementsView
132 for (const auto& the_executor_pair : this->m_executors) {
133 the_executor_pair.second->do_execute(*message_p, level);
134 }
135 }
136 );
137 }
138 }
139
141
143 Tag ALL{this, LogLevel::ALL};
145 Tag TRACE{this, LogLevel::TRACE};
147 Tag DEBUG{this, LogLevel::DEBUG};
149 Tag INFO{this, LogLevel::INFO};
151 Tag WARN{this, LogLevel::WARN};
153 Tag ERROR{this, LogLevel::ERROR};
155 Tag FATAL{this, LogLevel::FATAL};
156 };
157}
158
159#endif //ZCLIBLOG_LOGGER_ASYNC_HPP
基本日志器,无执行
Definition logger_base.hpp:53
BaseLogger(std::string name, const std::initializer_list< executor > &executor_ptrs={}, const LogLevelCfg config={})
构造同步日志器
Definition logger_base.hpp:149
std::vector< executor_pair > m_executors
Definition logger_base.hpp:67
内部使用的线程池
Definition logger_async.hpp:42
~ThreadPool()
析构线程池
Definition logger_async.hpp:69
void submit(std::function< void()> task)
提交任务到线程池
Definition logger_async.hpp:82
ThreadPool(const size_t numThreads=1)
构造线程池
Definition logger_async.hpp:48
#define ZCLIBLOG_LOGGER_CONFIGURATIONS_ASYNC_THREAD_NUM
异步Logger默认线程数量
Definition logger_configurations.h:15
ZCLibLog的命名空间
Definition android_log.hpp:16
LogLevel
一些日志等级
Definition logger_types.hpp:35
异步日志器
Definition logger_async.hpp:113
Tag WARN
WARN级别Tag
Definition logger_async.hpp:151
Tag ALL
ALL级别Tag
Definition logger_async.hpp:143
Tag FATAL
FATAL级别Tag
Definition logger_async.hpp:155
Tag DEBUG
DEBUG级别Tag
Definition logger_async.hpp:147
void execute(std::string &message, const LogLevel level) const
调用执行器处理日志信息和等级
Definition logger_async.hpp:122
Tag INFO
INFO级别Tag
Definition logger_async.hpp:149
Tag TRACE
TRACE级别Tag
Definition logger_async.hpp:145
Tag ERROR
ERROR级别Tag
Definition logger_async.hpp:153