StrFmt: print src_loc error as string

This commit is contained in:
Megamouse 2022-03-16 19:59:20 +01:00
parent 04df392866
commit 7a1a4541c0
2 changed files with 29 additions and 2 deletions

View File

@ -33,6 +33,28 @@ std::wstring utf8_to_wchar(std::string_view src)
MultiByteToWideChar(CP_UTF8, 0, src.data(), src.size(), wchar_string.data(), tmp_size);
return wchar_string;
}
std::string fmt::win_error_to_string(unsigned long error)
{
std::string message;
LPWSTR message_buffer = nullptr;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error, 0, (LPWSTR)&message_buffer, 0, nullptr))
{
message = fmt::format("%s (0x%x)", fmt::trim(wchar_to_utf8(message_buffer), " \t\n\r\f\v"), error);
}
else
{
message = fmt::format("0x%x", error);
}
if (message_buffer)
{
LocalFree(message_buffer);
}
return message;
}
#endif
template <>
@ -332,12 +354,12 @@ void fmt_class_string<src_loc>::format(std::string& out, u64 arg)
#ifdef _WIN32
if (DWORD error = GetLastError())
{
fmt::append(out, " (e=0x%08x[%u])", error, error);
fmt::append(out, " (error=%s)", error, fmt::win_error_to_string(error));
}
#else
if (int error = errno)
{
fmt::append(out, " (errno=%d)", error);
fmt::append(out, " (errno=%d=%s)", error, strerror(errno));
}
#endif
}

View File

@ -8,6 +8,11 @@ namespace fmt
{
template <typename CharT, usz N, typename... Args>
static std::string format(const CharT(&)[N], const Args&...);
#ifdef _WIN32
// Get a string for a windows error (DWORD)
std::string win_error_to_string(unsigned long error);
#endif
}
template <typename T, typename = void>