MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
logger.hpp
1#ifndef MSTL_CORE_LOGGING_LOGGER_HPP__
2#define MSTL_CORE_LOGGING_LOGGER_HPP__
3#include "MSTL/core/container/queue.hpp"
7#include "log_sink.hpp"
9
10class MSTL_API logger {
11private:
12 LOG_LEVEL level_;
13 _MSTL atomic<bool> async_;
14
15 vector<shared_ptr<log_sink>> sinks_;
16 _MSTL mutex sinks_mutex_;
17
18 queue<log_event> queue_;
19 _MSTL mutex queue_mutex_;
20 _MSTL condition_variable cv_;
21
22 _MSTL thread worker_;
23 _MSTL atomic<bool> running_;
24
25 _MSTL atomic<bool> flush_requested_{false};
26 _MSTL mutex flush_mutex_;
27 _MSTL condition_variable flush_cv_;
28
29 function<bool(const log_event&)> filter_;
30 _MSTL mutex filter_mutex_;
31
32 unordered_map<string, string> context_;
33 _MSTL mutex context_mutex_;
34
35 void enqueue(log_event&& ev);
36 void enqueue(const log_event& ev);
37
38 void start_worker();
39 void stop_worker();
40
41 void worker_loop();
42
43 explicit logger(LOG_LEVEL level = LOG_LEVEL::INFO, bool async = false);
44
45public:
46 static logger& instance() {
47 static logger log;
48 return log;
49 }
50
51 logger(const logger&) = delete;
52 logger& operator =(const logger&) = delete;
53 logger(logger&&) = delete;
54 logger& operator =(logger&&) = delete;
55
56 ~logger();
57
58 void add_sink(shared_ptr<log_sink> sink);
59
60 void set_level(LOG_LEVEL level);
61 void set_filter(function<bool(const log_event&)> filter);
62
63 void add_context(const string& key, string value);
64 void remove_context(const string& key);
65 void clear_context();
66
67 void enable_async(bool async);
68
69 void log(LOG_LEVEL level, string msg, string file, string func, int line);
70
71 void trace(string msg, string file, string func, int line) {
72 log(LOG_LEVEL::TRACE, move(msg), move(file), move(func), line);
73 }
74 void debug(string msg, string file, string func, int line) {
75 log(LOG_LEVEL::DEBUG, move(msg), move(file), move(func), line);
76 }
77 void info(string msg, string file, string func, int line) {
78 log(LOG_LEVEL::INFO, move(msg), move(file), move(func), line);
79 }
80 void warn(string msg, string file, string func, int line) {
81 log(LOG_LEVEL::WARN, move(msg), move(file), move(func), line);
82 }
83 void error(string msg, string file, string func, int line) {
84 log(LOG_LEVEL::ERROR, move(msg), move(file), move(func), line);
85 }
86 void fatal(string msg, string file, string func, int line) {
87 log(LOG_LEVEL::FATAL, move(msg), move(file), move(func), line);
88 }
89
90 void flush();
91};
92
93#define MSTL_LOG_TRACE(msg) _MSTL logger::instance().trace(msg, __FILE__, __func__, __LINE__)
94#define MSTL_LOG_DEBUG(msg) _MSTL logger::instance().debug(msg, __FILE__, __func__, __LINE__)
95#define MSTL_LOG_INFO(msg) _MSTL logger::instance().info(msg, __FILE__, __func__, __LINE__)
96#define MSTL_LOG_WARN(msg) _MSTL logger::instance().warn(msg, __FILE__, __func__, __LINE__)
97#define MSTL_LOG_ERROR(msg) _MSTL logger::instance().error(msg, __FILE__, __func__, __LINE__)
98#define MSTL_LOG_FATAL(msg) _MSTL logger::instance().fatal(msg, __FILE__, __func__, __LINE__)
99
100#define MSTL_LOGF_TRACE(msg, ...) _MSTL logger::instance().trace(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
101#define MSTL_LOGF_DEBUG(msg, ...) _MSTL logger::instance().debug(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
102#define MSTL_LOGF_INFO(msg, ...) _MSTL logger::instance().info(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
103#define MSTL_LOGF_WARN(msg, ...) _MSTL logger::instance().warn(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
104#define MSTL_LOGF_ERROR(msg, ...) _MSTL logger::instance().error(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
105#define MSTL_LOGF_FATAL(msg, ...) _MSTL logger::instance().fatal(_MSTL format(msg, __VA_ARGS__), __FILE__, __func__, __LINE__)
106
108#endif // MSTL_CORE_LOGGING_LOGGER_HPP__
MSTL条件变量行为
MSTL通用函数包装器
MSTL_NODISCARD future< async_result_t< Func, Args... > > async(launch policy, Func &&function, Args &&... args)
异步执行函数(指定策略)
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL共享智能指针实现