diff options
| author | 2021-09-08 14:36:20 -0400 | |
|---|---|---|
| committer | 2021-09-11 17:19:14 -0400 | |
| commit | 290afc00d36bbdcdc67d66a4586fd2f188734ad3 (patch) | |
| tree | d4f9a8eae42dd93ff1e7393ffea03b30faeab125 /src/common/error.cpp | |
| parent | Merge pull request #6846 from ameerj/nvdec-gpu-decode (diff) | |
| download | yuzu-290afc00d36bbdcdc67d66a4586fd2f188734ad3.tar.gz yuzu-290afc00d36bbdcdc67d66a4586fd2f188734ad3.tar.xz yuzu-290afc00d36bbdcdc67d66a4586fd2f188734ad3.zip | |
common: Move error handling to error.cpp/h
This allows us to avoid implicitly including <string> every time common_funcs.h is included.
Diffstat (limited to 'src/common/error.cpp')
| -rw-r--r-- | src/common/error.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
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 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #ifdef _WIN32 | ||
| 7 | #include <windows.h> | ||
| 8 | #else | ||
| 9 | #include <cerrno> | ||
| 10 | #include <cstring> | ||
| 11 | #endif | ||
| 12 | |||
| 13 | #include "common/error.h" | ||
| 14 | |||
| 15 | namespace Common { | ||
| 16 | |||
| 17 | std::string NativeErrorToString(int e) { | ||
| 18 | #ifdef _WIN32 | ||
| 19 | LPSTR err_str; | ||
| 20 | |||
| 21 | DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | | ||
| 22 | FORMAT_MESSAGE_IGNORE_INSERTS, | ||
| 23 | nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| 24 | reinterpret_cast<LPSTR>(&err_str), 1, nullptr); | ||
| 25 | if (!res) { | ||
| 26 | return "(FormatMessageA failed to format error)"; | ||
| 27 | } | ||
| 28 | std::string ret(err_str); | ||
| 29 | LocalFree(err_str); | ||
| 30 | return ret; | ||
| 31 | #else | ||
| 32 | char err_str[255]; | ||
| 33 | #if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) | ||
| 34 | // Thread safe (GNU-specific) | ||
| 35 | const char* str = strerror_r(e, err_str, sizeof(err_str)); | ||
| 36 | return std::string(str); | ||
| 37 | #else | ||
| 38 | // Thread safe (XSI-compliant) | ||
| 39 | int second_err = strerror_r(e, err_str, sizeof(err_str)); | ||
| 40 | if (second_err != 0) { | ||
| 41 | return "(strerror_r failed to format error)"; | ||
| 42 | } | ||
| 43 | return std::string(err_str); | ||
| 44 | #endif // GLIBC etc. | ||
| 45 | #endif // _WIN32 | ||
| 46 | } | ||
| 47 | |||
| 48 | std::string GetLastErrorMsg() { | ||
| 49 | #ifdef _WIN32 | ||
| 50 | return NativeErrorToString(GetLastError()); | ||
| 51 | #else | ||
| 52 | return NativeErrorToString(errno); | ||
| 53 | #endif | ||
| 54 | } | ||
| 55 | |||
| 56 | } // namespace Common | ||