mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Merge pull request #1127 from TranslucentTB/v1.x
Fix deprecation warnings in filename_to_str and improve performance of wbuf_to_utf8buf
This commit is contained in:
commit
d8e17111b9
@ -26,11 +26,6 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif //_WIN32
|
#endif //_WIN32
|
||||||
|
|
||||||
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
|
||||||
#include <codecvt>
|
|
||||||
#include <locale>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SPDLOG_COMPILED_LIB
|
#ifdef SPDLOG_COMPILED_LIB
|
||||||
#undef SPDLOG_HEADER_ONLY
|
#undef SPDLOG_HEADER_ONLY
|
||||||
#define SPDLOG_INLINE
|
#define SPDLOG_INLINE
|
||||||
@ -80,11 +75,6 @@ class sink;
|
|||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
using filename_t = std::wstring;
|
using filename_t = std::wstring;
|
||||||
#define SPDLOG_FILENAME_T(s) L##s
|
#define SPDLOG_FILENAME_T(s) L##s
|
||||||
inline std::string filename_to_str(const filename_t &filename)
|
|
||||||
{
|
|
||||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
|
|
||||||
return c.to_bytes(filename);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
#define SPDLOG_FILENAME_T(s) s
|
#define SPDLOG_FILENAME_T(s) s
|
||||||
@ -97,10 +87,13 @@ using err_handler = std::function<void(const std::string &err_msg)>;
|
|||||||
|
|
||||||
// string_view type - either std::string_view or fmt::string_view (pre c++17)
|
// string_view type - either std::string_view or fmt::string_view (pre c++17)
|
||||||
#if defined(FMT_USE_STD_STRING_VIEW)
|
#if defined(FMT_USE_STD_STRING_VIEW)
|
||||||
using string_view_t = std::string_view;
|
template<typename T>
|
||||||
|
using basic_string_view_t = std::basic_string_view<T>;
|
||||||
#else
|
#else
|
||||||
using string_view_t = fmt::string_view;
|
template<typename T>
|
||||||
|
using basic_string_view_t = fmt::basic_string_view<T>;
|
||||||
#endif
|
#endif
|
||||||
|
using string_view_t = basic_string_view_t<char>;
|
||||||
|
|
||||||
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
||||||
using level_t = details::null_atomic_int;
|
using level_t = details::null_atomic_int;
|
||||||
|
@ -36,6 +36,10 @@
|
|||||||
#include <share.h>
|
#include <share.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
|
#include <limits>
|
||||||
|
#endif
|
||||||
|
|
||||||
#else // unix
|
#else // unix
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -342,13 +346,14 @@ SPDLOG_INLINE void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT
|
|||||||
|
|
||||||
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
|
||||||
{
|
{
|
||||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
|
fmt::memory_buffer buf;
|
||||||
return c.to_bytes(filename);
|
wstr_to_utf8buf(filename, buf);
|
||||||
|
return fmt::to_string(buf);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
|
||||||
{
|
{
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
@ -398,28 +403,42 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) && defined(_WIN32)
|
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
SPDLOG_INLINE void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer &target)
|
SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memory_buffer &target)
|
||||||
{
|
{
|
||||||
int wbuf_size = static_cast<int>(wbuf.size());
|
if (wstr.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
|
||||||
if (wbuf_size == 0)
|
|
||||||
{
|
{
|
||||||
|
throw spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
int wstr_size = static_cast<int>(wstr.size());
|
||||||
|
if (wstr_size == 0)
|
||||||
|
{
|
||||||
|
target.resize(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result_size = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, NULL, 0, NULL, NULL);
|
int result_size = static_cast<int>(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)
|
if (result_size > 0)
|
||||||
{
|
{
|
||||||
target.resize(result_size);
|
target.resize(result_size);
|
||||||
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, &target.data()[0], result_size, NULL, NULL);
|
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);
|
||||||
}
|
|
||||||
else
|
if (result_size > 0)
|
||||||
{
|
{
|
||||||
throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
target.resize(result_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||||
}
|
}
|
||||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT) && _WIN32
|
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
|
|
||||||
} // namespace os
|
} // namespace os
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
@ -68,7 +68,7 @@ size_t thread_id() SPDLOG_NOEXCEPT;
|
|||||||
// See https://github.com/gabime/spdlog/issues/609
|
// See https://github.com/gabime/spdlog/issues/609
|
||||||
void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT;
|
void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::string filename_to_str(const filename_t &filename) SPDLOG_NOEXCEPT;
|
std::string filename_to_str(const filename_t &filename);
|
||||||
|
|
||||||
int pid() SPDLOG_NOEXCEPT;
|
int pid() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
@ -80,8 +80,8 @@ bool is_color_terminal() SPDLOG_NOEXCEPT;
|
|||||||
// Source: https://github.com/agauniyal/rang/
|
// Source: https://github.com/agauniyal/rang/
|
||||||
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
|
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) && defined(_WIN32)
|
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer &target);
|
void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, fmt::memory_buffer &target);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace os
|
} // namespace os
|
||||||
|
@ -243,8 +243,10 @@ public:
|
|||||||
// format to wmemory_buffer and convert to utf8
|
// format to wmemory_buffer and convert to utf8
|
||||||
fmt::wmemory_buffer wbuf;
|
fmt::wmemory_buffer wbuf;
|
||||||
fmt::format_to(wbuf, fmt, args...);
|
fmt::format_to(wbuf, fmt, args...);
|
||||||
|
|
||||||
fmt::memory_buffer buf;
|
fmt::memory_buffer buf;
|
||||||
details::os::wbuf_to_utf8buf(wbuf, 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()));
|
details::log_msg log_msg(source, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||||
sink_it_(log_msg);
|
sink_it_(log_msg);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user