Files
eden/src/common/error.cpp
T

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

59 lines
1.8 KiB
C++
Raw Normal View History

2026-06-04 05:49:07 +02:00
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
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
// glibc, mlibc, musl, and newlib all define their own variants of strerror_r
// We don't need to use the preprocessor, we can just select depending on return type
template<typename T> std::string HandleStrerrorR(T r, char *err_str);
template<> std::string HandleStrerrorR(char* r, char *) { return std::string{r}; }
template<> std::string HandleStrerrorR(const char* r, char *) { return std::string{r}; }
template<> std::string HandleStrerrorR(int r, char *err_str) {
return std::string{r != 0
? "(strerror_r failed to format error)"
: err_str};
}
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),
LPSTR(&err_str), 1, nullptr);
if (res) {
std::string ret(err_str);
LocalFree(err_str);
return ret;
2021-01-24 15:17:02 -05:00
}
return "(FormatMessageA failed to format error)";
2021-01-24 15:17:02 -05:00
#else
char err_str[255];
return HandleStrerrorR(strerror_r(e, err_str, sizeof(err_str)), err_str);
2021-01-24 15:17:02 -05:00
#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