mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-28 00:10:21 +08:00
Ported pull #3023 with some changes and tests
This commit is contained in:
parent
167bf989d8
commit
8e10782a58
@ -13,6 +13,7 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../common.h"
|
||||
@ -35,6 +36,8 @@ public:
|
||||
void register_logger(std::shared_ptr<logger> new_logger);
|
||||
void initialize_logger(std::shared_ptr<logger> new_logger);
|
||||
std::shared_ptr<logger> get(const std::string &logger_name);
|
||||
std::shared_ptr<logger> get(std::string_view logger_name);
|
||||
std::shared_ptr<logger> get(const char *logger_name);
|
||||
std::shared_ptr<logger> default_logger();
|
||||
|
||||
// Return raw ptr to the default logger.
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "./common.h"
|
||||
#include "./details/registry.h"
|
||||
@ -50,6 +51,8 @@ SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
|
||||
// exist.
|
||||
// example: spdlog::get("my_logger")->info("hello {}", "world");
|
||||
SPDLOG_API std::shared_ptr<logger> get(const std::string &name);
|
||||
SPDLOG_API std::shared_ptr<logger> get(std::string_view name);
|
||||
SPDLOG_API std::shared_ptr<logger> get(const char *name);
|
||||
|
||||
// Set global formatter. Each sink in each logger will get a clone of this object
|
||||
SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
|
||||
|
@ -12,7 +12,9 @@
|
||||
#ifdef _WIN32
|
||||
#include "spdlog/sinks/wincolor_sink.h"
|
||||
#else
|
||||
|
||||
#include "spdlog/sinks/ansicolor_sink.h"
|
||||
|
||||
#endif
|
||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||
|
||||
@ -74,6 +76,20 @@ std::shared_ptr<logger> registry::get(const std::string &logger_name) {
|
||||
return found == loggers_.end() ? nullptr : found->second;
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> registry::get(std::string_view logger_name) {
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
for (const auto &[key, val]: loggers_) {
|
||||
if (key == logger_name) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> registry::get(const char *logger_name) {
|
||||
return get(std::string_view(logger_name));
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> registry::default_logger() {
|
||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||
return default_logger_;
|
||||
|
@ -16,6 +16,10 @@ void initialize_logger(std::shared_ptr<logger> logger) { details::registry::inst
|
||||
|
||||
std::shared_ptr<logger> get(const std::string &name) { return details::registry::instance().get(name); }
|
||||
|
||||
std::shared_ptr<logger> get(std::string_view name) {return details::registry::instance().get(name); }
|
||||
|
||||
std::shared_ptr<logger> get(const char *name) { return details::registry::instance().get(name); }
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
||||
details::registry::instance().set_formatter(std::move(formatter));
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "includes.h"
|
||||
#include <string_view>
|
||||
#include "spdlog/sinks/daily_file_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
|
||||
@ -106,3 +107,24 @@ TEST_CASE("disable automatic registration", "[registry]") {
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::set_automatic_registration(true);
|
||||
}
|
||||
|
||||
TEST_CASE("get(const char* name)", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("get(std::string_view name)", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
REQUIRE(spdlog::get(std::string_view(tested_logger_name)) != nullptr);
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("get(std::string name)", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
REQUIRE(spdlog::get(std::string(tested_logger_name)) != nullptr);
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user