From 81f29a9a7ab9ad99b850ae39916dec342d518eef Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 10 Jul 2018 17:01:49 +0300 Subject: [PATCH] Fixed wchar support in messages --- include/spdlog/common.h | 2 +- include/spdlog/details/logger_impl.h | 31 ++++++++++++++-------------- include/spdlog/logger.h | 10 +++++---- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index c18d203e..668889f0 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -18,7 +18,7 @@ #include #include -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) +#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) #include #include #endif diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 9b9b467f..6ad2b6fd 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -173,24 +173,25 @@ inline void spdlog::logger::critical(const T &msg) } #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -#include -#include - -template -inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *msg) -{ - std::wstring_convert> conv; - - log(lvl, conv.to_bytes(msg)); -} - template inline void spdlog::logger::log(level::level_enum lvl, const wchar_t *fmt, const Args &... args) -{ - fmt::WMemoryWriter wWriter; +{ + if (!should_log(lvl)) + { + return; + } + + decltype(wstring_converter_)::byte_string utf8_string; - wWriter.write(fmt, args...); - log(lvl, wWriter.c_str()); + try + { + { + std::lock_guard lock(wstring_converter_mutex_); + utf8_string = wstring_converter_.to_bytes(fmt); + } + log(lvl, utf8_string.c_str(), args...); + } + SPDLOG_CATCH_AND_HANDLE } template diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 372d8314..5399dabd 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -63,10 +63,7 @@ public: template void critical(const char *fmt, const Args &... args); -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - template - void log(level::level_enum lvl, const wchar_t *msg); - +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT template void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args); @@ -152,6 +149,11 @@ protected: log_err_handler err_handler_; std::atomic last_err_time_; std::atomic msg_counter_; + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + std::wstring_convert> wstring_converter_; + std::mutex wstring_converter_mutex_; +#endif }; } // namespace spdlog