From 03d9cc08fb8152554b2f25f1a725fc60f73ae764 Mon Sep 17 00:00:00 2001 From: "M. Galib Uludag" Date: Sun, 26 Mar 2023 22:19:01 +0200 Subject: [PATCH] added src_loc feature --- include/spdlog/common.h | 72 ++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e69201a8..e9d18e2c 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -16,6 +17,8 @@ #include #include + + #ifdef SPDLOG_USE_STD_FORMAT # include # if __cpp_lib_format >= 202207L @@ -114,6 +117,45 @@ namespace spdlog { class formatter; +struct source_loc +{ + SPDLOG_CONSTEXPR source_loc() = default; + SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) + : filename{filename_in} + , line{line_in} + , funcname{funcname_in} + {} + + SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT + { + return line == 0; + } + const char *filename{nullptr}; + int line{0}; + const char *funcname{nullptr}; +}; + +template +struct format_string_wrapper +{ + format_string_wrapper(const char *fmt, details::source_location loc = details::source_location::current()) + : fmt_{fmt} + , loc_{loc} + {} + T fmt() + { + return fmt_; + } + source_loc loc() + { + return source_loc{loc_.file_name(), loc_.line(), loc_.function_name()}; + } + +private: + T fmt_; + spdlog::details::source_location loc_; +}; + namespace sinks { class sink; } @@ -138,13 +180,15 @@ namespace fmt_lib = std; using string_view_t = std::string_view; using memory_buf_t = std::string; + template # if __cpp_lib_format >= 202207L -using format_string_t = std::format_string; +using format_string_t = format_string_wrapper>; # else -using format_string_t = std::string_view; +using format_string_t = format_string_wrapper; # endif + template struct is_convertible_to_basic_format_string : std::integral_constant>::value> {}; @@ -155,9 +199,9 @@ using wmemory_buf_t = std::wstring; template # if __cpp_lib_format >= 202207L -using wformat_string_t = std::wformat_string; +using wformat_string_t = format_string_wrapper>; # else -using wformat_string_t = std::wstring_view; +using wformat_string_t = format_string_wrapper; # endif # endif # define SPDLOG_BUF_TO_STRING(x) x @@ -168,7 +212,7 @@ using string_view_t = fmt::basic_string_view; using memory_buf_t = fmt::basic_memory_buffer; template -using format_string_t = fmt::format_string; +using format_string_t = format_string_wrapper>; template using remove_cvref_t = typename std::remove_cv::type>::type; @@ -301,24 +345,6 @@ private: [[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno); [[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg); -struct source_loc -{ - SPDLOG_CONSTEXPR source_loc() = default; - SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) - : filename{filename_in} - , line{line_in} - , funcname{funcname_in} - {} - - SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT - { - return line == 0; - } - const char *filename{nullptr}; - int line{0}; - const char *funcname{nullptr}; -}; - struct file_event_handlers { file_event_handlers()