mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-15 18:22:07 +08:00
wip context
This commit is contained in:
parent
27a67b627f
commit
7f3f958f63
@ -24,6 +24,8 @@ class thread_pool;
|
|||||||
class SPDLOG_API context {
|
class SPDLOG_API context {
|
||||||
public:
|
public:
|
||||||
static context &instance();
|
static context &instance();
|
||||||
|
context();
|
||||||
|
~context();
|
||||||
context(const context &) = delete;
|
context(const context &) = delete;
|
||||||
context &operator=(const context &) = delete;
|
context &operator=(const context &) = delete;
|
||||||
|
|
||||||
@ -45,13 +47,10 @@ public:
|
|||||||
|
|
||||||
// clean all resources
|
// clean all resources
|
||||||
void shutdown();
|
void shutdown();
|
||||||
|
|
||||||
std::recursive_mutex &tp_mutex();
|
std::recursive_mutex &tp_mutex();
|
||||||
|
|
||||||
private:
|
|
||||||
context();
|
|
||||||
~context();
|
|
||||||
|
|
||||||
|
private:
|
||||||
std::recursive_mutex tp_mutex_;
|
std::recursive_mutex tp_mutex_;
|
||||||
std::shared_ptr<thread_pool> tp_;
|
std::shared_ptr<thread_pool> tp_;
|
||||||
std::shared_ptr<logger> global_logger_;
|
std::shared_ptr<logger> global_logger_;
|
||||||
|
@ -23,6 +23,10 @@ namespace spdlog {
|
|||||||
|
|
||||||
using default_factory = synchronous_factory;
|
using default_factory = synchronous_factory;
|
||||||
|
|
||||||
|
void set_context(std::shared_ptr<details::context> context);
|
||||||
|
std::shared_ptr<details::context> context();
|
||||||
|
const std::shared_ptr<details::context> &context_ref();
|
||||||
|
|
||||||
// Create a logger with a templated sink type
|
// Create a logger with a templated sink type
|
||||||
// Example:
|
// Example:
|
||||||
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
|
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
|
||||||
@ -75,23 +79,19 @@ SPDLOG_API std::shared_ptr<logger> global_logger();
|
|||||||
SPDLOG_API void set_global_logger(std::shared_ptr<logger> global_logger);
|
SPDLOG_API void set_global_logger(std::shared_ptr<logger> global_logger);
|
||||||
|
|
||||||
|
|
||||||
// Return the raw pointer of the global logger
|
|
||||||
SPDLOG_API logger *global_logger_raw();
|
|
||||||
|
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void log(source_loc source, level lvl, format_string_t<Args...> fmt, Args &&...args) {
|
void log(source_loc source, level lvl, format_string_t<Args...> fmt, Args &&...args) {
|
||||||
global_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
|
global_logger()->log(source, lvl, fmt, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void log(level lvl, format_string_t<Args...> fmt, Args &&...args) {
|
void log(level lvl, format_string_t<Args...> fmt, Args &&...args) {
|
||||||
global_logger_raw()->log(lvl, fmt, std::forward<Args>(args)...);
|
global_logger()->log(lvl, fmt, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void log(level lvl, std::string_view msg) { global_logger_raw()->log(lvl, msg); }
|
inline void log(level lvl, std::string_view msg) { global_logger()->log(lvl, msg); }
|
||||||
|
|
||||||
inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger_raw()->log(loc, lvl, msg); }
|
inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger()->log(loc, lvl, msg); }
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void trace(format_string_t<Args...> fmt, Args &&...args) {
|
void trace(format_string_t<Args...> fmt, Args &&...args) {
|
||||||
@ -168,7 +168,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); }
|
|||||||
|
|
||||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
|
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
|
||||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
||||||
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::global_logger_raw(), __VA_ARGS__)
|
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::global_logger(), __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
|
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
|
||||||
#define SPDLOG_DEBUG(...) (void)0
|
#define SPDLOG_DEBUG(...) (void)0
|
||||||
@ -176,7 +176,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); }
|
|||||||
|
|
||||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
|
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
|
||||||
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
||||||
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::global_logger_raw(), __VA_ARGS__)
|
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::global_logger(), __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
|
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
|
||||||
#define SPDLOG_INFO(...) (void)0
|
#define SPDLOG_INFO(...) (void)0
|
||||||
@ -184,7 +184,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); }
|
|||||||
|
|
||||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
|
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
|
||||||
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
||||||
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::global_logger_raw(), __VA_ARGS__)
|
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::global_logger(), __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
|
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
|
||||||
#define SPDLOG_WARN(...) (void)0
|
#define SPDLOG_WARN(...) (void)0
|
||||||
@ -192,7 +192,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); }
|
|||||||
|
|
||||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
|
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
|
||||||
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
||||||
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::global_logger_raw(), __VA_ARGS__)
|
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::global_logger(), __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
|
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
|
||||||
#define SPDLOG_ERROR(...) (void)0
|
#define SPDLOG_ERROR(...) (void)0
|
||||||
@ -200,7 +200,7 @@ inline void critical(std::string_view msg) { log(level::critical, msg); }
|
|||||||
|
|
||||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
|
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
|
||||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
||||||
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::global_logger_raw(), __VA_ARGS__)
|
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::global_logger(), __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
|
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
|
||||||
#define SPDLOG_CRITICAL(...) (void)0
|
#define SPDLOG_CRITICAL(...) (void)0
|
||||||
|
@ -12,34 +12,39 @@
|
|||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
|
static std::shared_ptr<details::context> s_context = std::make_unique<details::context>();
|
||||||
|
|
||||||
std::shared_ptr<logger> global_logger() { return details::context::instance().global_logger(); }
|
void set_context(std::shared_ptr<details::context> context) { s_context = std::move(context); }
|
||||||
|
|
||||||
logger *global_logger_raw() { return details::context::instance().global_logger_raw(); }
|
std::shared_ptr<details::context> context() {return s_context;}
|
||||||
|
|
||||||
|
const std::shared_ptr<details::context> &context_ref() {return s_context;}
|
||||||
|
|
||||||
|
std::shared_ptr<logger> global_logger() { return context()->global_logger(); }
|
||||||
|
|
||||||
void set_global_logger(std::shared_ptr<logger> global_logger) {
|
void set_global_logger(std::shared_ptr<logger> global_logger) {
|
||||||
details::context::instance().set_logger(std::move(global_logger));
|
context()->set_logger(std::move(global_logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_formatter(std::unique_ptr<formatter> formatter) {
|
void set_formatter(std::unique_ptr<formatter> formatter) {
|
||||||
global_logger_raw()->set_formatter(std::move(formatter));
|
global_logger()->set_formatter(std::move(formatter));
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
||||||
set_formatter(std::make_unique<pattern_formatter>(std::move(pattern), time_type));
|
set_formatter(std::make_unique<pattern_formatter>(std::move(pattern), time_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
level get_level() { return global_logger_raw()->log_level(); }
|
level get_level() { return global_logger()->log_level(); }
|
||||||
|
|
||||||
bool should_log(level level) { return global_logger_raw()->should_log(level); }
|
bool should_log(level level) { return global_logger()->should_log(level); }
|
||||||
|
|
||||||
void set_level(level level) { global_logger_raw()->set_level(level); }
|
void set_level(level level) { global_logger()->set_level(level); }
|
||||||
|
|
||||||
void flush_on(level level) { global_logger_raw()->flush_on(level); }
|
void flush_on(level level) { global_logger()->flush_on(level); }
|
||||||
|
|
||||||
void set_error_handler(void (*handler)(const std::string &msg)) { global_logger_raw()->set_error_handler(handler); }
|
void set_error_handler(void (*handler)(const std::string &msg)) { global_logger()->set_error_handler(handler); }
|
||||||
|
|
||||||
void shutdown() { details::context::instance().shutdown(); }
|
void shutdown() { s_context.reset();}
|
||||||
|
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
Loading…
Reference in New Issue
Block a user