From 035efac8bc30883f44b396af995cae55f674251d Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 28 Dec 2024 16:08:15 +0200 Subject: [PATCH] Remove context --- CMakeLists.txt | 4 +-- include/spdlog/details/context.h | 58 -------------------------------- include/spdlog/spdlog.h | 5 --- src/details/context.cpp | 44 ------------------------ src/spdlog.cpp | 21 ++++-------- 5 files changed, 8 insertions(+), 124 deletions(-) delete mode 100644 include/spdlog/details/context.h delete mode 100644 src/details/context.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b7ddf7b0..2fd70be6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,7 +156,6 @@ set(SPDLOG_HEADERS "include/spdlog/details/mpmc_blocking_q.h" "include/spdlog/details/null_mutex.h" "include/spdlog/details/os.h" - "include/spdlog/details/context.h" "include/spdlog/bin_to_hex.h" "include/spdlog/sinks/android_sink.h" "include/spdlog/sinks/base_sink.h" @@ -192,8 +191,7 @@ set(SPDLOG_SRCS "src/details/os_filesystem.cpp" "src/details/log_msg.cpp" "src/details/async_log_msg.cpp" - "src/details/context.cpp" - "src/sinks/base_sink.cpp" + "src/sinks/base_sink.cpp" "src/sinks/basic_file_sink.cpp" "src/sinks/rotating_file_sink.cpp" "src/sinks/stdout_sinks.cpp" diff --git a/include/spdlog/details/context.h b/include/spdlog/details/context.h deleted file mode 100644 index dd27b045..00000000 --- a/include/spdlog/details/context.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Loggers registry of unique name->logger pointer -// An attempt to create a logger with an already existing name will result with spdlog_ex exception. -// If user requests a non-existing logger, nullptr will be returned -// This class is thread safe - -#include -#include -#include - -#include "../common.h" - -namespace spdlog { -class logger; - -namespace details { -class thread_pool; - -class SPDLOG_API context { -public: - context() = default; - explicit context(std::unique_ptr global_logger); - ~context() = default; - context(const context &) = delete; - context &operator=(const context &) = delete; - - [[nodiscard]] std::shared_ptr global_logger(); - - // Return raw ptr to the global logger. - // To be used directly by the spdlog global api (e.g. spdlog::info) - // This make the global API faster, but cannot be used concurrently with set_global_logger(). - // e.g do not call set_global_logger() from one thread while calling spdlog::info() from - // another. - [[nodiscard]] logger *global_logger_raw() const noexcept; - - // set logger instance. - void set_logger(std::shared_ptr new_logger); - - void set_tp(std::shared_ptr tp); - - [[nodiscard]] std::shared_ptr get_tp(); - - // clean all resources - void shutdown(); - [[nodiscard]] std::recursive_mutex &tp_mutex(); - -private: - std::recursive_mutex tp_mutex_; - std::shared_ptr tp_; - std::shared_ptr global_logger_; -}; - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 15eb10d4..45527eb4 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -14,15 +14,10 @@ #include #include "./common.h" -#include "./details/context.h" #include "./logger.h" namespace spdlog { -SPDLOG_API void set_context(std::shared_ptr context); -SPDLOG_API std::shared_ptr context(); -SPDLOG_API const std::shared_ptr &context_ref(); - // Create a logger with a templated sink type // Example: // spdlog::create("logger_name", "dailylog_filename", 11, 59); diff --git a/src/details/context.cpp b/src/details/context.cpp deleted file mode 100644 index eb462c77..00000000 --- a/src/details/context.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#include "spdlog/details/context.h" -#include "spdlog/logger.h" - -#include - -namespace spdlog { -namespace details { - -context::context(std::unique_ptr global_logger) { global_logger_ = std::move(global_logger); } - -std::shared_ptr context::global_logger() { return global_logger_; } - -// Return raw ptr to the global logger. -// To be used directly by the spdlog default api (e.g. spdlog::info) -// This make the default API faster, but cannot be used concurrently with set_global_logger(). -// e.g do not call set_global_logger() from one thread while calling spdlog::info() from another. -logger *context::global_logger_raw() const noexcept { return global_logger_.get(); } - -// set global logger -void context::set_logger(std::shared_ptr new_global_logger) { global_logger_ = std::move(new_global_logger); } - -void context::set_tp(std::shared_ptr tp) { - std::lock_guard lock(tp_mutex_); - tp_ = std::move(tp); -} - -std::shared_ptr context::get_tp() { - std::lock_guard lock(tp_mutex_); - return tp_; -} - -// clean all resources and threads started by the registry -void context::shutdown() { - std::lock_guard lock(tp_mutex_); - tp_.reset(); -} - -std::recursive_mutex &context::tp_mutex() { return tp_mutex_; } - -} // namespace details -} // namespace spdlog diff --git a/src/spdlog.cpp b/src/spdlog.cpp index f364fa3c..667099ce 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -13,27 +13,20 @@ namespace spdlog { -static std::shared_ptr s_context = + #ifndef SPDLOG_DISABLE_GLOBAL_LOGGER - std::make_unique(std::make_unique(std::string(), std::make_unique())); + static std::shared_ptr s_logger = std::make_shared("global", std::make_shared()); #else - std::make_unique(); // empty context + static std::short_ptr s_logger = nullptr; #endif -void set_context(std::shared_ptr context) { s_context = std::move(context); } - -std::shared_ptr context() { return s_context; } - -const std::shared_ptr &context_ref() { return s_context; } -std::shared_ptr global_logger() { return context_ref()->global_logger(); } +std::shared_ptr global_logger() { return s_logger; } -void set_global_logger(std::shared_ptr global_logger) { context()->set_logger(std::move(global_logger)); } +void set_global_logger(std::shared_ptr global_logger) { s_logger = std::move(global_logger); } logger *global_logger_raw() noexcept { - auto *rv = context_ref()->global_logger_raw(); - assert(rv != nullptr); - return rv; + return s_logger.get(); } void set_formatter(std::unique_ptr formatter) { global_logger()->set_formatter(std::move(formatter)); } @@ -52,6 +45,6 @@ void flush_on(level level) { global_logger()->flush_on(level); } void set_error_handler(void (*handler)(const std::string &msg)) { global_logger()->set_error_handler(handler); } -void shutdown() { s_context.reset(); } +void shutdown() { s_logger.reset(); } } // namespace spdlog