From 290afc00d36bbdcdc67d66a4586fd2f188734ad3 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 14:36:20 -0400 Subject: common: Move error handling to error.cpp/h This allows us to avoid implicitly including every time common_funcs.h is included. --- src/common/CMakeLists.txt | 3 ++- src/common/common_funcs.h | 11 --------- src/common/error.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/common/error.h | 21 +++++++++++++++++ src/common/misc.cpp | 52 ---------------------------------------- src/common/thread.cpp | 6 ++--- src/core/network/network.cpp | 5 ++-- 7 files changed, 85 insertions(+), 69 deletions(-) create mode 100644 src/common/error.cpp create mode 100644 src/common/error.h delete mode 100644 src/common/misc.cpp diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 57922b51c..b18a2a2f5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -53,6 +53,8 @@ add_library(common STATIC div_ceil.h dynamic_library.cpp dynamic_library.h + error.cpp + error.h fiber.cpp fiber.h fs/file.cpp @@ -88,7 +90,6 @@ add_library(common STATIC microprofile.cpp microprofile.h microprofileui.h - misc.cpp nvidia_flags.cpp nvidia_flags.h page_table.cpp diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 53bd7da60..85f8bdd22 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -6,7 +6,6 @@ #include #include -#include #if !defined(ARCHITECTURE_x86_64) #include // for exit @@ -49,16 +48,6 @@ __declspec(dllimport) void __stdcall DebugBreak(void); #endif // _MSC_VER ndef -// Generic function to get last error message. -// Call directly after the command or use the error num. -// This function might change the error code. -// Defined in misc.cpp. -[[nodiscard]] std::string GetLastErrorMsg(); - -// Like GetLastErrorMsg(), but passing an explicit error code. -// Defined in misc.cpp. -[[nodiscard]] std::string NativeErrorToString(int e); - #define DECLARE_ENUM_FLAG_OPERATORS(type) \ [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \ using T = std::underlying_type_t; \ diff --git a/src/common/error.cpp b/src/common/error.cpp new file mode 100644 index 000000000..d4455e310 --- /dev/null +++ b/src/common/error.cpp @@ -0,0 +1,56 @@ +// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +#include "common/error.h" + +namespace Common { + +std::string NativeErrorToString(int e) { +#ifdef _WIN32 + LPSTR err_str; + + DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&err_str), 1, nullptr); + if (!res) { + return "(FormatMessageA failed to format error)"; + } + std::string ret(err_str); + LocalFree(err_str); + return ret; +#else + char err_str[255]; +#if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) + // Thread safe (GNU-specific) + const char* str = strerror_r(e, err_str, sizeof(err_str)); + return std::string(str); +#else + // Thread safe (XSI-compliant) + int second_err = strerror_r(e, err_str, sizeof(err_str)); + if (second_err != 0) { + return "(strerror_r failed to format error)"; + } + return std::string(err_str); +#endif // GLIBC etc. +#endif // _WIN32 +} + +std::string GetLastErrorMsg() { +#ifdef _WIN32 + return NativeErrorToString(GetLastError()); +#else + return NativeErrorToString(errno); +#endif +} + +} // namespace Common diff --git a/src/common/error.h b/src/common/error.h new file mode 100644 index 000000000..e084d4b0f --- /dev/null +++ b/src/common/error.h @@ -0,0 +1,21 @@ +// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +namespace Common { + +// Generic function to get last error message. +// Call directly after the command or use the error num. +// This function might change the error code. +// Defined in error.cpp. +[[nodiscard]] std::string GetLastErrorMsg(); + +// Like GetLastErrorMsg(), but passing an explicit error code. +// Defined in error.cpp. +[[nodiscard]] std::string NativeErrorToString(int e); + +} // namespace Common diff --git a/src/common/misc.cpp b/src/common/misc.cpp deleted file mode 100644 index 495385b9e..000000000 --- a/src/common/misc.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#ifdef _WIN32 -#include -#else -#include -#include -#endif - -#include "common/common_funcs.h" - -std::string NativeErrorToString(int e) { -#ifdef _WIN32 - LPSTR err_str; - - DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&err_str), 1, nullptr); - if (!res) { - return "(FormatMessageA failed to format error)"; - } - std::string ret(err_str); - LocalFree(err_str); - return ret; -#else - char err_str[255]; -#if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) - // Thread safe (GNU-specific) - const char* str = strerror_r(e, err_str, sizeof(err_str)); - return std::string(str); -#else - // Thread safe (XSI-compliant) - int second_err = strerror_r(e, err_str, sizeof(err_str)); - if (second_err != 0) { - return "(strerror_r failed to format error)"; - } - return std::string(err_str); -#endif // GLIBC etc. -#endif // _WIN32 -} - -std::string GetLastErrorMsg() { -#ifdef _WIN32 - return NativeErrorToString(GetLastError()); -#else - return NativeErrorToString(errno); -#endif -} diff --git a/src/common/thread.cpp b/src/common/thread.cpp index d2c1ac60d..946a1114d 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -2,7 +2,9 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "common/common_funcs.h" +#include + +#include "common/error.h" #include "common/logging/log.h" #include "common/thread.h" #ifdef __APPLE__ @@ -21,8 +23,6 @@ #include #endif -#include - #ifdef __FreeBSD__ #define cpu_set_t cpuset_t #endif diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 4732d4485..72eea52f0 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -7,7 +7,8 @@ #include #include #include -#include "common/common_funcs.h" + +#include "common/error.h" #ifdef _WIN32 #include @@ -223,7 +224,7 @@ Errno GetAndLogLastError() { if (err == Errno::AGAIN) { return err; } - LOG_ERROR(Network, "Socket operation error: {}", NativeErrorToString(e)); + LOG_ERROR(Network, "Socket operation error: {}", Common::NativeErrorToString(e)); return err; } -- cgit v1.2.3 From eb1e3f19bb58802497ad2d71a36964d182839471 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 14:47:01 -0400 Subject: common_funcs: Replace with --- src/common/common_funcs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 85f8bdd22..1e74d6930 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -4,8 +4,8 @@ #pragma once -#include #include +#include #if !defined(ARCHITECTURE_x86_64) #include // for exit -- cgit v1.2.3 From ae028ddf22e35178f4335baa9f65648e83087699 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 15:09:01 -0400 Subject: codec: Add missing include --- src/video_core/command_classes/codecs/codec.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/video_core/command_classes/codecs/codec.h b/src/video_core/command_classes/codecs/codec.h index 1508d36c2..f9a80886f 100644 --- a/src/video_core/command_classes/codecs/codec.h +++ b/src/video_core/command_classes/codecs/codec.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include "common/common_types.h" #include "video_core/command_classes/nvdec_common.h" -- cgit v1.2.3 From 4a6a73e887502b02c88de6eedf0ffa1f58de553d Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 15:13:21 -0400 Subject: file_sys/kernel_executable: Add missing include --- src/core/file_sys/kernel_executable.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/file_sys/kernel_executable.h b/src/core/file_sys/kernel_executable.h index 044c554d3..79ca82f8b 100644 --- a/src/core/file_sys/kernel_executable.h +++ b/src/core/file_sys/kernel_executable.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include "common/common_funcs.h" -- cgit v1.2.3 From 6f307f15218e41eb243e899427412e6dd38198a7 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 20:04:29 -0400 Subject: kernel: Add missing include --- src/core/hle/kernel/kernel.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 3a6db0b1c..901d43da9 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include -- cgit v1.2.3 From 554c46d18600ca8cd7fbd85e4e05a108760ca400 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 21:43:02 -0400 Subject: video_core/memory_manager: Add missing include --- src/video_core/memory_manager.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp index c60ed6453..dce00e829 100644 --- a/src/video_core/memory_manager.cpp +++ b/src/video_core/memory_manager.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include + #include "common/alignment.h" #include "common/assert.h" #include "common/logging/log.h" -- cgit v1.2.3 From 76abf55f254627e904ceea7e1367c23257f7e63b Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 23:25:48 -0400 Subject: slot_vector: Add missing include --- src/video_core/texture_cache/slot_vector.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/video_core/texture_cache/slot_vector.h b/src/video_core/texture_cache/slot_vector.h index 6180b8c0e..74cd3c9d8 100644 --- a/src/video_core/texture_cache/slot_vector.h +++ b/src/video_core/texture_cache/slot_vector.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include -- cgit v1.2.3 From 63b4c8f9f76cef3c82ac3c2fabe8a044c1d7e5eb Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 8 Sep 2021 23:49:08 -0400 Subject: vk_descriptor_pool: Add missing include --- src/video_core/renderer_vulkan/vk_descriptor_pool.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/video_core/renderer_vulkan/vk_descriptor_pool.cpp b/src/video_core/renderer_vulkan/vk_descriptor_pool.cpp index 8e77e4796..adb557f60 100644 --- a/src/video_core/renderer_vulkan/vk_descriptor_pool.cpp +++ b/src/video_core/renderer_vulkan/vk_descriptor_pool.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include #include -- cgit v1.2.3 From e67463df2419feb10915eb91bba2b0485154a586 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Thu, 9 Sep 2021 12:36:00 -0400 Subject: shader_environment: Add missing include --- src/video_core/shader_environment.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/video_core/shader_environment.cpp b/src/video_core/shader_environment.cpp index 8a4581c19..81a878bb2 100644 --- a/src/video_core/shader_environment.cpp +++ b/src/video_core/shader_environment.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include #include -- cgit v1.2.3