mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
replace deprected wchar string conversion
This commit is contained in:
parent
22f514aabf
commit
c67974e4c8
@ -459,6 +459,41 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
|
||||
|
||||
throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target)
|
||||
{
|
||||
if (str.size() > static_cast<size_t>((std::numeric_limits<int>::max)()))
|
||||
{
|
||||
throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16");
|
||||
}
|
||||
|
||||
int str_size = static_cast<int>(str.size());
|
||||
if (str_size == 0)
|
||||
{
|
||||
target.resize(0);
|
||||
return;
|
||||
}
|
||||
|
||||
int result_size = static_cast<int>(target.capacity());
|
||||
if ((str_size + 1) * 2 < result_size)
|
||||
{
|
||||
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||
}
|
||||
|
||||
if (result_size > 0)
|
||||
{
|
||||
target.resize(result_size);
|
||||
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, (LPWSTR)target.data(), result_size);
|
||||
|
||||
if (result_size > 0)
|
||||
{
|
||||
target.resize(result_size);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||
}
|
||||
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||
|
||||
// return true on success
|
||||
|
@ -85,6 +85,8 @@ SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
|
||||
|
||||
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);
|
||||
|
||||
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, memory_buf_t &target);
|
||||
#endif
|
||||
|
||||
// Return directory name from given path or empty string
|
||||
|
@ -39,8 +39,6 @@ Windows Registry Editor Version 5.00
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
@ -226,23 +224,36 @@ protected:
|
||||
formatted.push_back('\0');
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
auto buf = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(formatted.data());
|
||||
LPCWSTR lp_wstr = reinterpret_cast<LPCWSTR>(buf.c_str());
|
||||
try
|
||||
{
|
||||
memory_buf_t buf;
|
||||
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
|
||||
|
||||
LPCWSTR lp_wstr = reinterpret_cast<LPCWSTR>(buf.data());
|
||||
succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr);
|
||||
#else
|
||||
LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());
|
||||
|
||||
succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
|
||||
#endif
|
||||
|
||||
if (!succeeded)
|
||||
{
|
||||
SPDLOG_THROW(win32_error("ReportEvent"));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// WCHAR string conversion can fail and if it does, we shouldn't call to report event function.
|
||||
}
|
||||
#else
|
||||
LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());
|
||||
|
||||
succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
|
||||
|
||||
if (!succeeded)
|
||||
{
|
||||
SPDLOG_THROW(win32_error("ReportEvent"));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user