Windows: log more descriptive errors

This commit is contained in:
Megamouse 2023-03-04 11:05:32 +01:00
parent 8409979dc5
commit 92ae57c9ee
8 changed files with 28 additions and 10 deletions

View File

@ -1783,7 +1783,7 @@ const std::string& fs::get_config_dir()
if (GetEnvironmentVariable(L"RPCS3_CONFIG_DIR", buf, size) - 1 >= size - 1 &&
GetModuleFileName(nullptr, buf, size) - 1 >= size - 1)
{
MessageBoxA(nullptr, fmt::format("GetModuleFileName() failed: error %u.", GetLastError()).c_str(), "fs::get_config_dir()", MB_ICONERROR);
MessageBoxA(nullptr, fmt::format("GetModuleFileName() failed: error: %s", fmt::win_error{GetLastError(), nullptr}).c_str(), "fs::get_config_dir()", MB_ICONERROR);
return dir; // empty
}
@ -1868,7 +1868,7 @@ const std::string& fs::get_temp_dir()
wchar_t buf[MAX_PATH + 2]{};
if (GetTempPathW(MAX_PATH + 1, buf) - 1 > MAX_PATH)
{
MessageBoxA(nullptr, fmt::format("GetTempPath() failed: error %u.", GetLastError()).c_str(), "fs::get_temp_dir()", MB_ICONERROR);
MessageBoxA(nullptr, fmt::format("GetTempPath() failed: error: %s", fmt::win_error{GetLastError(), nullptr}).c_str(), "fs::get_temp_dir()", MB_ICONERROR);
return dir; // empty
}

View File

@ -79,6 +79,17 @@ std::string fmt::win_error_to_string(unsigned long error, void* module_handle)
return message;
}
std::string fmt::win_error_to_string(const fmt::win_error& error)
{
return fmt::win_error_to_string(error.error, error.module_handle);
}
template <>
void fmt_class_string<fmt::win_error>::format(std::string& out, u64 arg)
{
fmt::append(out, "%s", fmt::win_error_to_string(get_object(arg)));
}
#endif
template <>

View File

@ -10,8 +10,15 @@ namespace fmt
static std::string format(const CharT(&)[N], const Args&...);
#ifdef _WIN32
struct win_error
{
unsigned long error{};
void* module_handle{};
};
// Get a string for a windows error (DWORD). Optionally a module HANDLE can be passed.
std::string win_error_to_string(unsigned long error, void* module_handle = nullptr);
std::string win_error_to_string(const win_error& error);
#endif
}

View File

@ -2710,7 +2710,7 @@ void thread_ctrl::detect_cpu_layout()
if (!GetLogicalProcessorInformationEx(relationship,
reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>(buffer.data()), &buffer_size))
{
sig_log.error("GetLogicalProcessorInformationEx failed (size=%u, error=%u)", buffer_size, GetLastError());
sig_log.error("GetLogicalProcessorInformationEx failed (size=%u, error=%s)", buffer_size, fmt::win_error{GetLastError(), nullptr});
}
else
{
@ -2957,7 +2957,7 @@ void thread_ctrl::set_native_priority(int priority)
if (!SetThreadPriority(_this_thread, native_priority))
{
sig_log.error("SetThreadPriority() failed: 0x%x", GetLastError());
sig_log.error("SetThreadPriority() failed: %s", fmt::win_error{GetLastError(), nullptr});
}
#else
int policy;
@ -3009,7 +3009,7 @@ void thread_ctrl::set_thread_affinity_mask(u64 mask)
HANDLE _this_thread = GetCurrentThread();
if (!SetThreadAffinityMask(_this_thread, !mask ? process_affinity_mask : mask))
{
sig_log.error("Failed to set thread affinity 0x%x: error 0x%x.", mask, GetLastError());
sig_log.error("Failed to set thread affinity 0x%x: error: %s", mask, fmt::win_error{GetLastError(), nullptr});
}
#elif __APPLE__
// Supports only one core

View File

@ -290,7 +290,7 @@ namespace vk
MEMORY_BASIC_INFORMATION mem_info;
if (!::VirtualQuery(vm::get_super_ptr<const void>(base_address), &mem_info, sizeof(mem_info)))
{
rsx_log.error("VirtualQuery failed! LastError=0x%x", GetLastError());
rsx_log.error("VirtualQuery failed! LastError=%s", fmt::win_error{GetLastError(), nullptr});
return false;
}

View File

@ -503,7 +503,7 @@ int main(int argc, char** argv)
WSADATA wsa_data{};
if (const int res = WSAStartup(MAKEWORD(2, 2), &wsa_data); res != 0)
{
report_fatal_error(fmt::format("WSAStartup failed (error=%s)", fmt::win_error_to_string(res, nullptr)));
report_fatal_error(fmt::format("WSAStartup failed (error=%s)", fmt::win_error{static_cast<unsigned long>(res), nullptr}));
}
#endif

View File

@ -54,9 +54,9 @@ LOG_CHANNEL(perf_log, "PERF");
namespace utils
{
#ifdef _WIN32
std::string pdh_error(PDH_STATUS status)
fmt::win_error pdh_error(PDH_STATUS status)
{
return fmt::win_error_to_string(status, LoadLibrary(L"pdh.dll"));
return fmt::win_error{static_cast<unsigned long>(status), LoadLibrary(L"pdh.dll")};
}
#endif

View File

@ -421,7 +421,7 @@ namespace utils
if (!::VirtualProtect(reinterpret_cast<LPVOID>(addr), block_size, +prot, &old))
{
fmt::throw_exception("VirtualProtect failed (%p, 0x%x, addr=0x%x, error=%#x)", pointer, size, addr, GetLastError());
fmt::throw_exception("VirtualProtect failed (%p, 0x%x, addr=0x%x, error=%s)", pointer, size, addr, fmt::win_error{GetLastError(), nullptr});
}
// Next region