Remove context
This commit is contained in:
parent
7cfe4fc0bc
commit
035efac8bc
@ -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"
|
||||
|
@ -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 <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
namespace spdlog {
|
||||
class logger;
|
||||
|
||||
namespace details {
|
||||
class thread_pool;
|
||||
|
||||
class SPDLOG_API context {
|
||||
public:
|
||||
context() = default;
|
||||
explicit context(std::unique_ptr<logger> global_logger);
|
||||
~context() = default;
|
||||
context(const context &) = delete;
|
||||
context &operator=(const context &) = delete;
|
||||
|
||||
[[nodiscard]] std::shared_ptr<logger> 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<logger> new_logger);
|
||||
|
||||
void set_tp(std::shared_ptr<thread_pool> tp);
|
||||
|
||||
[[nodiscard]] std::shared_ptr<thread_pool> get_tp();
|
||||
|
||||
// clean all resources
|
||||
void shutdown();
|
||||
[[nodiscard]] std::recursive_mutex &tp_mutex();
|
||||
|
||||
private:
|
||||
std::recursive_mutex tp_mutex_;
|
||||
std::shared_ptr<thread_pool> tp_;
|
||||
std::shared_ptr<logger> global_logger_;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@ -14,15 +14,10 @@
|
||||
#include <string_view>
|
||||
|
||||
#include "./common.h"
|
||||
#include "./details/context.h"
|
||||
#include "./logger.h"
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
SPDLOG_API void set_context(std::shared_ptr<details::context> context);
|
||||
SPDLOG_API std::shared_ptr<details::context> context();
|
||||
SPDLOG_API 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);
|
||||
|
@ -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 <memory>
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
context::context(std::unique_ptr<logger> global_logger) { global_logger_ = std::move(global_logger); }
|
||||
|
||||
std::shared_ptr<logger> 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<logger> new_global_logger) { global_logger_ = std::move(new_global_logger); }
|
||||
|
||||
void context::set_tp(std::shared_ptr<thread_pool> tp) {
|
||||
std::lock_guard lock(tp_mutex_);
|
||||
tp_ = std::move(tp);
|
||||
}
|
||||
|
||||
std::shared_ptr<thread_pool> 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
|
@ -13,27 +13,20 @@
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
static std::shared_ptr s_context =
|
||||
|
||||
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
|
||||
std::make_unique<details::context>(std::make_unique<logger>(std::string(), std::make_unique<sinks::stdout_color_sink_mt>()));
|
||||
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("global", std::make_shared<sinks::stdout_color_sink_mt>());
|
||||
#else
|
||||
std::make_unique<details::context>(); // empty context
|
||||
static std::short_ptr<logger> s_logger = nullptr;
|
||||
#endif
|
||||
|
||||
void set_context(std::shared_ptr<details::context> context) { s_context = std::move(context); }
|
||||
|
||||
std::shared_ptr<details::context> context() { return s_context; }
|
||||
std::shared_ptr<logger> global_logger() { return s_logger; }
|
||||
|
||||
const std::shared_ptr<details::context> &context_ref() { return s_context; }
|
||||
|
||||
std::shared_ptr<logger> global_logger() { return context_ref()->global_logger(); }
|
||||
|
||||
void set_global_logger(std::shared_ptr<logger> global_logger) { context()->set_logger(std::move(global_logger)); }
|
||||
void set_global_logger(std::shared_ptr<logger> 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> 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
|
||||
|
Loading…
Reference in New Issue
Block a user