diff options
| author | Leo Lam <leolino.lam@gmail.com> | 2017-08-20 23:41:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-20 23:41:47 +0200 |
| commit | aab4cced1153db168504705aa9a70c7fd16dbc44 (patch) | |
| tree | 8cd5425ae8bfd5e82cbea3a6dee35ee6e695ffcc /Source/Core/Common/CommonFuncs.cpp | |
| parent | 16d6ccbdf7bed6a1da122e0bcd0747d9bcbd650f (diff) | |
| parent | 4e5fe6366a2041b09616a9a2e3b2ccb038a4d2f1 (diff) | |
Merge pull request #5938 from sepalani/err-msg
CommonFuncs: LastStrerrorString added
Diffstat (limited to 'Source/Core/Common/CommonFuncs.cpp')
| -rw-r--r-- | Source/Core/Common/CommonFuncs.cpp | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/Source/Core/Common/CommonFuncs.cpp b/Source/Core/Common/CommonFuncs.cpp index bfb2878dd3..ef370672d2 100644 --- a/Source/Core/Common/CommonFuncs.cpp +++ b/Source/Core/Common/CommonFuncs.cpp @@ -22,27 +22,35 @@ #ifdef _WIN32 #include <windows.h> +#define strerror_r(err, buf, len) strerror_s(buf, len, err) #endif -// Generic function to get last error message. -// Call directly after the command or use the error num. +constexpr size_t BUFFER_SIZE = 256; + +// Wrapper function to get last strerror(errno) string. // This function might change the error code. -std::string GetLastErrorMsg() +std::string LastStrerrorString() { - const size_t buff_size = 256; - char err_str[buff_size]; + char error_message[BUFFER_SIZE]; -#ifdef _WIN32 - FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); -#else // 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); + const int result = strerror_r(errno, error_message, BUFFER_SIZE); if (result != 0) return ""; -#endif + return std::string(error_message); +} - return std::string(err_str); +#ifdef _WIN32 +// Wrapper function to get GetLastError() string. +// This function might change the error code. +std::string GetLastErrorString() +{ + char error_message[BUFFER_SIZE]; + + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr); + return std::string(error_message); } +#endif |
