spdlog/src/details/context.cpp

79 lines
2.2 KiB
C++
Raw Normal View History

// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2024-12-06 15:13:47 +08:00
#include "spdlog/details/context.h"
#include "spdlog/logger.h"
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
// support for the global stdout color logger
2024-05-03 23:30:46 +08:00
#ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h"
#else
2024-03-17 05:47:39 +08:00
2024-05-03 23:30:46 +08:00
#include "spdlog/sinks/ansicolor_sink.h"
2024-03-17 05:47:39 +08:00
2024-05-03 23:30:46 +08:00
#endif
#endif // SPDLOG_DISABLE_GLOBAL_LOGGER
#include <memory>
2024-03-17 06:36:52 +08:00
namespace spdlog {
2024-05-03 23:30:46 +08:00
namespace details {
2024-12-06 15:53:23 +08:00
context::context() {
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
// create global logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
2024-05-03 23:30:46 +08:00
#ifdef _WIN32
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
#else
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
#endif
const char *global_logger_name = "";
global_logger_ = std::make_shared<logger>(global_logger_name, std::move(color_sink));
#endif // SPDLOG_DISABLE_GLOBAL_LOGGER
2024-05-03 23:30:46 +08:00
}
2024-12-06 15:13:47 +08:00
context::~context() = default;
2024-05-03 23:30:46 +08:00
std::shared_ptr<logger> context::global_logger() {
return global_logger_;
2024-05-03 23:30:46 +08:00
}
// 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(); }
2024-12-06 17:20:44 +08:00
// set global logger
void context::set_logger(std::shared_ptr<logger> new_global_logger) {
global_logger_ = std::move(new_global_logger);
2024-05-03 23:30:46 +08:00
}
2024-12-06 15:13:47 +08:00
void context::set_tp(std::shared_ptr<thread_pool> tp) {
2024-12-06 16:28:00 +08:00
std::lock_guard lock(tp_mutex_);
2024-05-03 23:30:46 +08:00
tp_ = std::move(tp);
}
2024-12-06 15:13:47 +08:00
std::shared_ptr<thread_pool> context::get_tp() {
2024-12-06 16:28:00 +08:00
std::lock_guard lock(tp_mutex_);
2024-05-03 23:30:46 +08:00
return tp_;
}
// clean all resources and threads started by the registry
2024-12-06 15:13:47 +08:00
void context::shutdown() {
2024-12-06 16:28:00 +08:00
std::lock_guard lock(tp_mutex_);
2024-12-06 15:53:23 +08:00
tp_.reset();
2024-05-03 23:30:46 +08:00
}
2024-12-06 15:13:47 +08:00
std::recursive_mutex &context::tp_mutex() { return tp_mutex_; }
2024-05-03 23:30:46 +08:00
2024-12-06 15:13:47 +08:00
context &context::instance() {
static context s_instance;
2024-05-03 23:30:46 +08:00
return s_instance;
}
} // namespace details
2023-09-25 21:40:05 +08:00
} // namespace spdlog