diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index da4a7de9..ab390aeb 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -84,6 +84,22 @@ SPDLOG_INLINE std::shared_ptr registry::get(const std::string &logger_na return found == loggers_.end() ? nullptr : found->second; } +#if __cplusplus >= 201703L // C++17 +SPDLOG_INLINE std::shared_ptr registry::get(std::string_view logger_name) { + std::lock_guard lock(logger_map_mutex_); + for (const auto &[key, val] : loggers_) { + if (key == logger_name) { + return val; + } + } + return nullptr; +} + +SPDLOG_INLINE std::shared_ptr registry::get(const char *logger_name) { + return get(std::string_view(logger_name)); +} +#endif + SPDLOG_INLINE std::shared_ptr registry::default_logger() { std::lock_guard lock(logger_map_mutex_); return default_logger_; diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 00492151..1e9e6bde 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -18,6 +18,10 @@ #include #include +#if __cplusplus >= 201703L // C++17 + #include +#endif + namespace spdlog { class logger; @@ -33,6 +37,10 @@ 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); +#if __cplusplus >= 201703L // C++17 + std::shared_ptr get(std::string_view logger_name); + std::shared_ptr get(const char *logger_name); +#endif 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 97c36222..54cdc2cc 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -20,6 +20,16 @@ SPDLOG_INLINE std::shared_ptr get(const std::string &name) { return details::registry::instance().get(name); } +#if __cplusplus >= 201703L // C++17 +SPDLOG_INLINE std::shared_ptr get(std::string_view name) { + return details::registry::instance().get(name); +} + +SPDLOG_INLINE std::shared_ptr get(const char *name) { + return details::registry::instance().get(name); +} +#endif + 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 a8afbcec..d2982633 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -20,6 +20,10 @@ #include #include +#if __cplusplus >= 201703L // C++17 + #include +#endif + namespace spdlog { using default_factory = synchronous_factory; @@ -50,6 +54,10 @@ SPDLOG_API void initialize_logger(std::shared_ptr logger); // exist. // example: spdlog::get("my_logger")->info("hello {}", "world"); SPDLOG_API std::shared_ptr get(const std::string &name); +#if __cplusplus >= 201703L // C++17 +SPDLOG_API std::shared_ptr get(std::string_view name); +SPDLOG_API std::shared_ptr get(const char *name); +#endif // Set global formatter. Each sink in each logger will get a clone of this object SPDLOG_API void set_formatter(std::unique_ptr formatter);