improve error reporting, remove duplicate code

This commit is contained in:
Lion Kortlepel
2021-08-02 13:54:36 +02:00
parent 9666fff622
commit 1fb7cb6bc1
4 changed files with 49 additions and 89 deletions

View File

@@ -101,3 +101,30 @@ std::string Version::AsString() {
ss << int(major) << "." << int(minor) << "." << int(patch);
return ss.str();
}
std::string GetPlatformAgnosticErrorString() {
#ifdef WIN32
// This will provide us with the error code and an error message, all in one.
int err;
char msgbuf[256];
msgbuf[0] = '\0';
err = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf,
sizeof(msgbuf),
nullptr);
if (*msgbuf) {
return std::to_string(GetLastError()) + " - " + std::string(msgbuf);
} else {
return std::to_string(GetLastError())
}
#else // posix
return std::strerror(errno);
#endif
}