Use stack allocated space when possible

This commit is contained in:
Charles Milette 2019-06-30 21:34:19 -04:00
parent 3bcd3cef2f
commit f529afa625
No known key found for this signature in database
GPG Key ID: 1A5AE81377AD973A
3 changed files with 20 additions and 12 deletions

View File

@ -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<wchar_t> wstr)
SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memory_buffer &target)
{
if (wstr.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
{
@ -412,21 +414,25 @@ SPDLOG_INLINE std::string wstr_to_str(basic_string_view_t<wchar_t> wstr)
int wstr_size = static_cast<int>(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;
}
}

View File

@ -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<wchar_t> wstr);
void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memory_buffer &target);
#endif
} // namespace os

View File

@ -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<wchar_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)