mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-24 06:32:06 +08:00
convert message back to wchar
This commit is contained in:
parent
5e33a7e58b
commit
211478e13e
@ -39,6 +39,8 @@ Windows Registry Editor Version 5.00
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <locale>
|
||||||
|
#include <codecvt>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@ -55,34 +57,20 @@ struct win32_error : public spdlog_ex
|
|||||||
{
|
{
|
||||||
std::string system_message;
|
std::string system_message;
|
||||||
|
|
||||||
auto fFormatResult = [&system_message](bool format_message_succeeded, void* format_message_result)
|
|
||||||
{
|
|
||||||
if (format_message_succeeded && format_message_result)
|
|
||||||
{
|
|
||||||
system_message = fmt::format(" ({})", format_message_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format_message_result)
|
|
||||||
{
|
|
||||||
LocalFree((HLOCAL)format_message_result);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
|
||||||
LPWSTR format_message_result{};
|
|
||||||
auto format_message_succeeded =
|
|
||||||
::FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
|
||||||
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&format_message_result, 0, nullptr);
|
|
||||||
|
|
||||||
fFormatResult(format_message_succeeded, format_message_result);
|
|
||||||
#else
|
|
||||||
LPSTR format_message_result{};
|
LPSTR format_message_result{};
|
||||||
auto format_message_succeeded =
|
auto format_message_succeeded =
|
||||||
::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
||||||
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr);
|
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr);
|
||||||
|
|
||||||
fFormatResult(format_message_succeeded, format_message_result);
|
if (format_message_succeeded && format_message_result)
|
||||||
#endif
|
{
|
||||||
|
system_message = fmt::format(" ({})", format_message_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (format_message_result)
|
||||||
|
{
|
||||||
|
LocalFree((HLOCAL)format_message_result);
|
||||||
|
}
|
||||||
|
|
||||||
return fmt::format("{}: {}{}", user_message, error_code, system_message);
|
return fmt::format("{}: {}{}", user_message, error_code, system_message);
|
||||||
}
|
}
|
||||||
@ -238,10 +226,10 @@ protected:
|
|||||||
formatted.push_back('\0');
|
formatted.push_back('\0');
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
LPCWSTR lp_str = reinterpret_cast<LPCWSTR>(formatted.data());
|
auto buf = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(formatted.data());
|
||||||
|
|
||||||
succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
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_str, nullptr);
|
current_user_sid_.as_sid(), 1, 0, (LPCWSTR*)buf.c_str(), nullptr);
|
||||||
#else
|
#else
|
||||||
LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());
|
LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user