wip context

This commit is contained in:
gabime 2024-12-06 14:13:35 +02:00
parent 27a67b627f
commit 7f3f958f63
3 changed files with 31 additions and 27 deletions

View File

@ -24,6 +24,8 @@ class thread_pool;
class SPDLOG_API context {
public:
static context &instance();
context();
~context();
context(const context &) = delete;
context &operator=(const context &) = delete;
@ -45,13 +47,10 @@ public:
// clean all resources
void shutdown();
std::recursive_mutex &tp_mutex();
private:
context();
~context();
private:
std::recursive_mutex tp_mutex_;
std::shared_ptr<thread_pool> tp_;
std::shared_ptr<logger> global_logger_;

View File

@ -23,6 +23,10 @@ namespace spdlog {
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
// Example:
// 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);
// Return the raw pointer of the global logger
SPDLOG_API logger *global_logger_raw();
template <typename... 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>
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>
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
#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
#define SPDLOG_LOGGER_DEBUG(logger, ...) (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
#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
#define SPDLOG_LOGGER_INFO(logger, ...) (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
#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
#define SPDLOG_LOGGER_WARN(logger, ...) (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
#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
#define SPDLOG_LOGGER_ERROR(logger, ...) (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
#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
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
#define SPDLOG_CRITICAL(...) (void)0

View File

@ -12,34 +12,39 @@
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) {
details::context::instance().set_logger(std::move(global_logger));
context()->set_logger(std::move(global_logger));
}
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) {
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