From 231e8be3437a677b53a43658590951261c52c846 Mon Sep 17 00:00:00 2001 From: Tim Kraus Date: Tue, 26 Apr 2022 09:46:08 +0200 Subject: [PATCH] Added function to get the names of all registered loggers --- include/spdlog/details/registry-inl.h | 11 +++++++++++ include/spdlog/details/registry.h | 2 ++ include/spdlog/spdlog-inl.h | 6 ++++++ include/spdlog/spdlog.h | 4 ++++ 4 files changed, 23 insertions(+) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index c55b5eea..e44fe8c9 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -92,6 +92,17 @@ SPDLOG_INLINE std::shared_ptr registry::get(const std::string &logger_na return found == loggers_.end() ? nullptr : found->second; } +SPDLOG_INLINE std::vector registry::get_logger_names(void) +{ + std::lock_guard lock(logger_map_mutex_); + std::vector logger_names; + for (auto &l : loggers_) + { + logger_names.push_back(l.first); + } + return logger_names; +} + SPDLOG_INLINE std::shared_ptr registry::default_logger() { std::lock_guard lock(logger_map_mutex_); diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 97473ea3..85db8300 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace spdlog { @@ -34,6 +35,7 @@ public: void register_logger(std::shared_ptr new_logger); void initialize_logger(std::shared_ptr new_logger); std::shared_ptr get(const std::string &logger_name); + std::vector get_logger_names(void); std::shared_ptr default_logger(); // Return raw ptr to the default logger. diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index 2b875fae..477533c8 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -22,6 +22,12 @@ SPDLOG_INLINE std::shared_ptr get(const std::string &name) return details::registry::instance().get(name); } +SPDLOG_INLINE std::vector get_logger_names(void) +{ + return details::registry::instance().get_logger_names(); +} + + SPDLOG_INLINE void set_formatter(std::unique_ptr formatter) { details::registry::instance().set_formatter(std::move(formatter)); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 65d3e9d5..e82f6ec1 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -19,6 +19,7 @@ #include #include #include +#include namespace spdlog { @@ -51,6 +52,9 @@ SPDLOG_API void initialize_logger(std::shared_ptr logger); // example: spdlog::get("my_logger")->info("hello {}", "world"); SPDLOG_API std::shared_ptr get(const std::string &name); +// Return a list with the names of all registered loggers +SPDLOG_API std::vector get_logger_names(void); + // Set global formatter. Each sink in each logger will get a clone of this object SPDLOG_API void set_formatter(std::unique_ptr formatter);