diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index dd4fc8f9..3c2fc9e9 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -348,7 +348,9 @@ SPDLOG_INLINE void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { - return wstr_to_str(filename); + fmt::memory_buffer buf; + wstr_to_utf8buf(filename, buf); + return fmt::to_string(buf); } #else SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) @@ -402,7 +404,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT } #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -SPDLOG_INLINE std::string wstr_to_str(basic_string_view_t wstr) +SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memory_buffer &target) { if (wstr.size() > static_cast(std::numeric_limits::max())) { @@ -412,21 +414,25 @@ SPDLOG_INLINE std::string wstr_to_str(basic_string_view_t wstr) int wstr_size = static_cast(wstr.size()); if (wstr_size == 0) { - return { }; + target.resize(0); + return; } - int result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); + int result_size = target.capacity(); + if ((wstr_size + 1) * 2 > result_size) + { + result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); + } if (result_size > 0) { - std::string result; - result.resize(result_size); - result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, &result[0], result_size, NULL, NULL); + target.resize(result_size); + result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL); if (result_size > 0) { - result.resize(result_size); - return result; + target.resize(result_size); + return; } } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 30c569c5..27694494 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -81,7 +81,7 @@ bool is_color_terminal() SPDLOG_NOEXCEPT; bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -std::string wstr_to_str(basic_string_view_t wstr); +void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memory_buffer &target); #endif } // namespace os diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 59cb5db6..1d831362 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -244,8 +244,10 @@ public: fmt::wmemory_buffer wbuf; fmt::format_to(wbuf, fmt, args...); - const auto payload = details::os::wstr_to_str({ wbuf.data(), wbuf.size() }); - details::log_msg log_msg(source, name_, lvl, payload); + fmt::memory_buffer buf; + details::os::wstr_to_utf8buf(basic_string_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); } catch (const std::exception &ex)