1#ifndef MSTL_CORE_LOGGING_LOGGER_HPP__
2#define MSTL_CORE_LOGGING_LOGGER_HPP__
3#include "MSTL/core/container/queue.hpp"
10class MSTL_API logger {
13 _MSTL atomic<bool> async_;
15 vector<shared_ptr<log_sink>> sinks_;
16 _MSTL mutex sinks_mutex_;
18 queue<log_event> queue_;
19 _MSTL mutex queue_mutex_;
20 _MSTL condition_variable cv_;
23 _MSTL atomic<bool> running_;
25 _MSTL atomic<bool> flush_requested_{
false};
26 _MSTL mutex flush_mutex_;
27 _MSTL condition_variable flush_cv_;
29 function<bool(
const log_event&)> filter_;
30 _MSTL mutex filter_mutex_;
32 unordered_map<string, string> context_;
33 _MSTL mutex context_mutex_;
35 void enqueue(log_event&& ev);
36 void enqueue(
const log_event& ev);
43 explicit logger(LOG_LEVEL level = LOG_LEVEL::INFO,
bool async =
false);
46 static logger& instance() {
51 logger(
const logger&) =
delete;
52 logger& operator =(
const logger&) =
delete;
53 logger(logger&&) =
delete;
54 logger& operator =(logger&&) =
delete;
58 void add_sink(shared_ptr<log_sink> sink);
60 void set_level(LOG_LEVEL level);
61 void set_filter(function<
bool(
const log_event&)> filter);
63 void add_context(
const string& key,
string value);
64 void remove_context(
const string& key);
67 void enable_async(
bool async);
69 void log(LOG_LEVEL level,
string msg,
string file,
string func,
int line);
71 void trace(
string msg,
string file,
string func,
int line) {
72 log(LOG_LEVEL::TRACE,
move(msg),
move(file),
move(func), line);
74 void debug(
string msg,
string file,
string func,
int line) {
75 log(LOG_LEVEL::DEBUG,
move(msg),
move(file),
move(func), line);
77 void info(
string msg,
string file,
string func,
int line) {
78 log(LOG_LEVEL::INFO,
move(msg),
move(file),
move(func), line);
80 void warn(
string msg,
string file,
string func,
int line) {
81 log(LOG_LEVEL::WARN,
move(msg),
move(file),
move(func), line);
83 void error(
string msg,
string file,
string func,
int line) {
84 log(LOG_LEVEL::ERROR,
move(msg),
move(file),
move(func), line);
86 void fatal(
string msg,
string file,
string func,
int line) {
87 log(LOG_LEVEL::FATAL,
move(msg),
move(file),
move(func), line);
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__)
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__)
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)
移动范围元素