Files
eden/src/common/error.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.6 KiB
C++
Raw Normal View History

2022-05-15 02:06:02 +02:00
// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2013-09-04 20:17:46 -04:00
2015-06-21 13:12:49 +01:00
#include <cstddef>
2013-09-08 20:41:23 -04:00
#ifdef _WIN32
2018-08-14 04:28:24 +08:00
#include <windows.h>
2015-05-06 04:06:12 -03:00
#else
2015-06-21 13:12:49 +01:00
#include <cerrno>
#include <cstring>
2013-09-08 20:41:23 -04:00
#endif
2021-09-08 14:36:20 -04:00
#include "common/error.h"
namespace Common {
2013-09-04 20:17:46 -04:00
2021-01-24 15:17:02 -05:00
std::string NativeErrorToString(int e) {
2013-09-08 20:41:23 -04:00
#ifdef _WIN32
2021-01-24 15:17:02 -05:00
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<LPSTR>(&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];
2022-12-17 23:37:37 -08:00
#if defined(ANDROID) || \
(defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)))
// Thread safe (GNU-specific)
2021-01-24 15:17:02 -05:00
const char* str = strerror_r(e, err_str, sizeof(err_str));
return std::string(str);
2013-09-08 20:41:23 -04:00
#else
2014-04-01 18:20:08 -04:00
// Thread safe (XSI-compliant)
2021-01-24 15:17:02 -05:00
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);
2021-01-24 15:17:02 -05:00
#endif // GLIBC etc.
#endif // _WIN32
}
std::string GetLastErrorMsg() {
#ifdef _WIN32
return NativeErrorToString(GetLastError());
#else
return NativeErrorToString(errno);
2013-09-08 20:41:23 -04:00
#endif
}
2021-09-08 14:36:20 -04:00
} // namespace Common