mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
feature: adds string view overloads for logger accessor (#3023)
Co-authored-by: Ben Leadbetter <ben.leadbetter@native-instruments.com>
This commit is contained in:
parent
60faedb025
commit
5532231bbc
@ -84,6 +84,22 @@ SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_na
|
|||||||
return found == loggers_.end() ? nullptr : found->second;
|
return found == loggers_.end() ? nullptr : found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
SPDLOG_INLINE 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE std::shared_ptr<logger> registry::get(const char *logger_name) {
|
||||||
|
return get(std::string_view(logger_name));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> registry::default_logger() {
|
SPDLOG_INLINE 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_;
|
||||||
|
@ -18,6 +18,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
class logger;
|
class logger;
|
||||||
|
|
||||||
@ -33,6 +37,10 @@ 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);
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
std::shared_ptr<logger> get(std::string_view logger_name);
|
||||||
|
std::shared_ptr<logger> get(const char *logger_name);
|
||||||
|
#endif
|
||||||
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.
|
||||||
|
@ -20,6 +20,16 @@ SPDLOG_INLINE std::shared_ptr<logger> get(const std::string &name) {
|
|||||||
return details::registry::instance().get(name);
|
return details::registry::instance().get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
SPDLOG_INLINE std::shared_ptr<logger> get(std::string_view name) {
|
||||||
|
return details::registry::instance().get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE std::shared_ptr<logger> get(const char *name) {
|
||||||
|
return details::registry::instance().get(name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
SPDLOG_INLINE void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
SPDLOG_INLINE 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));
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,10 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
using default_factory = synchronous_factory;
|
using default_factory = synchronous_factory;
|
||||||
@ -50,6 +54,10 @@ 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);
|
||||||
|
#if __cplusplus >= 201703L // C++17
|
||||||
|
SPDLOG_API std::shared_ptr<logger> get(std::string_view name);
|
||||||
|
SPDLOG_API std::shared_ptr<logger> get(const char *name);
|
||||||
|
#endif
|
||||||
|
|
||||||
// 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user