diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 5a7521e7..7173f1ca 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -77,20 +77,26 @@ SPDLOG_API std::shared_ptr global_logger(); // Set the global logger. (for example, to replace the global logger with a custom logger) SPDLOG_API void set_global_logger(std::shared_ptr global_logger); +// Return the global logger raw pointer. +// To be used directly by the spdlog default API (e.g. spdlog::info) +// This make the default API faster, but cannot be used concurrently with set_global_logger(). +// e.g do not call set_global_logger() from one thread while calling spdlog::info() from another. +SPDLOG_API logger *global_logger_raw() noexcept; + template void log(source_loc source, level lvl, format_string_t fmt, Args &&...args) { - global_logger()->log(source, lvl, fmt, std::forward(args)...); + global_logger_raw()->log(source, lvl, fmt, std::forward(args)...); } template void log(level lvl, format_string_t fmt, Args &&...args) { - global_logger()->log(lvl, fmt, std::forward(args)...); + global_logger_raw()->log(lvl, fmt, std::forward(args)...); } -inline void log(level lvl, std::string_view msg) { global_logger()->log(lvl, msg); } +inline void log(level lvl, std::string_view msg) { global_logger_raw()->log(lvl, msg); } -inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger()->log(loc, lvl, msg); } +inline void log(source_loc loc, level lvl, std::string_view msg) { global_logger_raw()->log(loc, lvl, msg); } template void trace(format_string_t fmt, Args &&...args) { diff --git a/src/spdlog.cpp b/src/spdlog.cpp index cf9f961e..256c87cc 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -26,6 +26,8 @@ void set_global_logger(std::shared_ptr global_logger) { context()->set_logger(std::move(global_logger)); } +logger *global_logger_raw() noexcept { return context_ref()->global_logger_raw(); } + void set_formatter(std::unique_ptr formatter) { global_logger()->set_formatter(std::move(formatter)); }