spdlog/src/spdlog.cpp

58 lines
1.9 KiB
C++
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-12 01:06:17 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-29 05:20:26 +08:00
#include "spdlog/spdlog.h"
2024-12-07 01:21:42 +08:00
#include <cassert>
2023-10-01 17:20:48 +08:00
#include <memory>
#include "spdlog/common.h"
2023-10-01 17:20:48 +08:00
#include "spdlog/logger.h"
#include "spdlog/pattern_formatter.h"
2023-10-01 17:20:48 +08:00
#include "spdlog/sinks/stdout_color_sinks.h"
2023-09-25 01:26:32 +08:00
namespace spdlog {
2024-12-07 01:21:42 +08:00
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>()));
#else
std::make_unique<details::context>(); // empty context
#endif
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_context(std::shared_ptr<details::context> context) { s_context = std::move(context); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
std::shared_ptr<details::context> context() { return s_context; }
2024-12-07 01:21:42 +08:00
const std::shared_ptr<details::context> &context_ref() { return s_context; }
2024-12-07 01:21:42 +08:00
std::shared_ptr<logger> global_logger() { return context_ref()->global_logger(); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_global_logger(std::shared_ptr<logger> global_logger) { context()->set_logger(std::move(global_logger)); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
logger *global_logger_raw() noexcept {
auto *rv = context_ref()->global_logger_raw();
assert(rv != nullptr);
return rv;
}
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_pattern(std::string pattern, pattern_time_type time_type) {
set_formatter(std::make_unique<pattern_formatter>(std::move(pattern), time_type));
}
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
level get_level() { return global_logger()->log_level(); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
bool should_log(level level) { return global_logger()->should_log(level); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_level(level level) { global_logger()->set_level(level); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void flush_on(level level) { global_logger()->flush_on(level); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void set_error_handler(void (*handler)(const std::string &msg)) { global_logger()->set_error_handler(handler); }
2023-09-25 01:26:32 +08:00
2024-12-07 01:21:42 +08:00
void shutdown() { s_context.reset(); }
2023-09-25 01:26:32 +08:00
2023-09-25 21:40:05 +08:00
} // namespace spdlog