1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-29 20:13:52 +00:00

Ported pull #3023 with some changes and tests

This commit is contained in:
gabime 2024-03-16 17:12:46 +02:00
parent 167bf989d8
commit 8e10782a58
5 changed files with 223 additions and 175 deletions

View File

@ -13,6 +13,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <string_view>
#include <unordered_map> #include <unordered_map>
#include "../common.h" #include "../common.h"
@ -35,6 +36,8 @@ public:
void register_logger(std::shared_ptr<logger> new_logger); void register_logger(std::shared_ptr<logger> new_logger);
void initialize_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(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(); std::shared_ptr<logger> default_logger();
// Return raw ptr to the default logger. // Return raw ptr to the default logger.

View File

@ -14,6 +14,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <string_view>
#include "./common.h" #include "./common.h"
#include "./details/registry.h" #include "./details/registry.h"
@ -50,6 +51,8 @@ SPDLOG_API void initialize_logger(std::shared_ptr<logger> logger);
// exist. // exist.
// example: spdlog::get("my_logger")->info("hello {}", "world"); // 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(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 // 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); SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);

View File

@ -12,7 +12,9 @@
#ifdef _WIN32 #ifdef _WIN32
#include "spdlog/sinks/wincolor_sink.h" #include "spdlog/sinks/wincolor_sink.h"
#else #else
#include "spdlog/sinks/ansicolor_sink.h" #include "spdlog/sinks/ansicolor_sink.h"
#endif #endif
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER #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; 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::shared_ptr<logger> registry::default_logger() {
std::lock_guard<std::mutex> lock(logger_map_mutex_); std::lock_guard<std::mutex> lock(logger_map_mutex_);
return default_logger_; return default_logger_;

View File

@ -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(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) { void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
details::registry::instance().set_formatter(std::move(formatter)); details::registry::instance().set_formatter(std::move(formatter));
} }

View File

@ -1,4 +1,5 @@
#include "includes.h" #include "includes.h"
#include <string_view>
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_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_level(spdlog::level::info);
spdlog::set_automatic_registration(true); 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();
}