From 92ae57c9ee785134509a38988a6d9040c685819a Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 4 Mar 2023 11:05:32 +0100 Subject: [PATCH] Windows: log more descriptive errors --- Utilities/File.cpp | 4 ++-- Utilities/StrFmt.cpp | 11 +++++++++++ Utilities/StrFmt.h | 7 +++++++ Utilities/Thread.cpp | 6 +++--- rpcs3/Emu/RSX/VK/VKDMA.cpp | 2 +- rpcs3/main.cpp | 2 +- rpcs3/util/cpu_stats.cpp | 4 ++-- rpcs3/util/vm_native.cpp | 2 +- 8 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 67f6d417da..0f5c81c443 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -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 } diff --git a/Utilities/StrFmt.cpp b/Utilities/StrFmt.cpp index 8244ca3e19..66d421977f 100644 --- a/Utilities/StrFmt.cpp +++ b/Utilities/StrFmt.cpp @@ -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::format(std::string& out, u64 arg) +{ + fmt::append(out, "%s", fmt::win_error_to_string(get_object(arg))); +} #endif template <> diff --git a/Utilities/StrFmt.h b/Utilities/StrFmt.h index 5acb3a20eb..898313ee6b 100644 --- a/Utilities/StrFmt.h +++ b/Utilities/StrFmt.h @@ -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 } diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 0bffdbce94..ebaadcea4b 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2710,7 +2710,7 @@ void thread_ctrl::detect_cpu_layout() if (!GetLogicalProcessorInformationEx(relationship, reinterpret_cast(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 diff --git a/rpcs3/Emu/RSX/VK/VKDMA.cpp b/rpcs3/Emu/RSX/VK/VKDMA.cpp index 5918509400..f4ce1ef175 100644 --- a/rpcs3/Emu/RSX/VK/VKDMA.cpp +++ b/rpcs3/Emu/RSX/VK/VKDMA.cpp @@ -290,7 +290,7 @@ namespace vk MEMORY_BASIC_INFORMATION mem_info; if (!::VirtualQuery(vm::get_super_ptr(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; } diff --git a/rpcs3/main.cpp b/rpcs3/main.cpp index 9b11905068..5434cb0793 100644 --- a/rpcs3/main.cpp +++ b/rpcs3/main.cpp @@ -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(res), nullptr})); } #endif diff --git a/rpcs3/util/cpu_stats.cpp b/rpcs3/util/cpu_stats.cpp index 9bbc972983..53d01ad558 100644 --- a/rpcs3/util/cpu_stats.cpp +++ b/rpcs3/util/cpu_stats.cpp @@ -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(status), LoadLibrary(L"pdh.dll")}; } #endif diff --git a/rpcs3/util/vm_native.cpp b/rpcs3/util/vm_native.cpp index a620e9d99e..3c4b25b50c 100644 --- a/rpcs3/util/vm_native.cpp +++ b/rpcs3/util/vm_native.cpp @@ -421,7 +421,7 @@ namespace utils if (!::VirtualProtect(reinterpret_cast(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