From 59cbdaaf4987ff55f65debc407187dca039a677e Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Wed, 24 Jul 2019 13:26:42 -0400 Subject: [PATCH 1/4] Add more source_loc overloads in spdlog namespace --- include/spdlog/spdlog.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 4abb3098..b4996688 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -162,6 +162,12 @@ inline void critical(string_view_t fmt, const Args &... args) default_logger_raw()->critical(fmt, args...); } +template +inline void log(source_loc source, level::level_enum lvl, const T &msg) +{ + default_logger_raw()->log(source, lvl, msg); +} + template inline void log(level::level_enum lvl, const T &msg) { @@ -205,6 +211,12 @@ inline void critical(const T &msg) } #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +inline void log(source_loc source, level::level_enum lvl, wstring_view_t fmt, const Args &... args) +{ + default_logger_raw()->log(source, lvl, fmt, args...); +} + template inline void log(level::level_enum lvl, wstring_view_t fmt, const Args &... args) { From 2ce9a3f70fa9e85d1e3bd158bc686ec22c5e95bb Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Wed, 24 Jul 2019 13:27:54 -0400 Subject: [PATCH 2/4] Add overload to logger when T can be statically converted to wstring_view_t --- include/spdlog/logger.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 8d991e58..69e3721f 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -228,7 +228,7 @@ public: fmt::format_to(wbuf, fmt, args...); fmt::memory_buffer buf; - details::os::wstr_to_utf8buf(basic_string_view_t(wbuf.data(), wbuf.size()), buf); + details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); details::log_msg log_msg(source, name_, lvl, string_view_t(buf.data(), buf.size())); sink_it_(log_msg); @@ -277,6 +277,26 @@ public: { log(level::critical, fmt, args...); } + + // T can be statically converted to wstring_view + template::value, T>::type * = nullptr> + void log(source_loc loc, level::level_enum lvl, const T &msg) + { + if (!should_log(lvl)) + { + return; + } + + try + { + fmt::memory_buffer buf; + details::os::wstr_to_utf8buf(msg, buf); + + details::log_msg log_msg(loc, name_, lvl, buf); + sink_it_(log_msg); + } + SPDLOG_LOGGER_CATCH() + } #endif // _WIN32 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT From 4ef4d0659d1131c297103ac0a0f62cc17ab64b14 Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Wed, 24 Jul 2019 13:30:43 -0400 Subject: [PATCH 3/4] Improve correctness of convertion checks --- include/spdlog/logger.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 69e3721f..b9300bb8 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -143,7 +143,7 @@ public: } // T can be statically converted to string_view - template::value, T>::type * = nullptr> + template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) @@ -156,7 +156,7 @@ public: } // T cannot be statically converted to string_view - template::value, T>::type * = nullptr> + template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) @@ -279,7 +279,7 @@ public: } // T can be statically converted to wstring_view - template::value, T>::type * = nullptr> + template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) From eb51f37c671158691afcc013bdecf3a494bf442a Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Wed, 24 Jul 2019 13:46:59 -0400 Subject: [PATCH 4/4] Fix ambiguous overload errors --- include/spdlog/common.h | 6 ++++++ include/spdlog/logger.h | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 49625c0b..35941aff 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -95,7 +95,13 @@ using string_view_t = basic_string_view_t; #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows #else using wstring_view_t = basic_string_view_t; + +template +struct is_convertible_to_wstring_view : std::is_convertible { }; #endif // _WIN32 +#else +template +struct is_convertible_to_wstring_view : std::false_type { }; #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT #if defined(SPDLOG_NO_ATOMIC_LEVELS) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index b9300bb8..63287d43 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -155,8 +155,8 @@ public: sink_it_(log_msg); } - // T cannot be statically converted to string_view - template::value, T>::type * = nullptr> + // T cannot be statically converted to string_view or wstring_view + template::value && !is_convertible_to_wstring_view::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) @@ -279,7 +279,7 @@ public: } // T can be statically converted to wstring_view - template::value, T>::type * = nullptr> + template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) @@ -292,7 +292,7 @@ public: fmt::memory_buffer buf; details::os::wstr_to_utf8buf(msg, buf); - details::log_msg log_msg(loc, name_, lvl, buf); + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); sink_it_(log_msg); } SPDLOG_LOGGER_CATCH()