2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2017-05-13 03:22:30 -06:00
|
|
|
// The code in GetErrorMessage can't handle some systems having the
|
|
|
|
// GNU version of strerror_r and other systems having the XSI version,
|
|
|
|
// so we undefine _GNU_SOURCE here in an attempt to always get the XSI version.
|
|
|
|
// We include cstring before all other headers in case cstring is included
|
|
|
|
// indirectly (without undefining _GNU_SOURCE) by some other header.
|
|
|
|
#ifdef _GNU_SOURCE
|
|
|
|
#undef _GNU_SOURCE
|
|
|
|
#include <cstring>
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#else
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstring>
|
2017-05-13 03:22:30 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstddef>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <errno.h>
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2014-09-18 22:17:41 -06:00
|
|
|
#include "Common/CommonFuncs.h"
|
|
|
|
|
2017-01-23 00:08:45 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-07 01:35:01 -07:00
|
|
|
// Generic function to get last error message.
|
|
|
|
// Call directly after the command or use the error num.
|
|
|
|
// This function might change the error code.
|
2015-04-07 14:15:21 -06:00
|
|
|
std::string GetLastErrorMsg()
|
2009-03-07 01:35:01 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
const size_t buff_size = 256;
|
|
|
|
char err_str[buff_size];
|
2011-03-16 15:08:20 -06:00
|
|
|
|
2009-03-07 01:35:01 -07:00
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr);
|
2009-03-07 01:35:01 -07:00
|
|
|
#else
|
2017-05-13 03:22:30 -06:00
|
|
|
// We assume that the XSI-compliant version of strerror_r (returns int) is used
|
|
|
|
// rather than the GNU version (returns char*). The returned value is stored to
|
|
|
|
// an int variable to get a compile-time check that the return type is not char*.
|
|
|
|
const int result = strerror_r(errno, err_str, buff_size);
|
|
|
|
if (result != 0)
|
2016-06-24 02:43:46 -06:00
|
|
|
return "";
|
2009-03-07 01:35:01 -07:00
|
|
|
#endif
|
2011-03-16 15:08:20 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::string(err_str);
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|