misc.cpp 1008 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #else
  8. #include <cerrno>
  9. #include <cstring>
  10. #endif
  11. // Neither Android nor OS X support TLS
  12. #if defined(__APPLE__) || (ANDROID && __clang__)
  13. #define __thread
  14. #endif
  15. // Generic function to get last error message.
  16. // Call directly after the command or use the error num.
  17. // This function might change the error code.
  18. const char* GetLastErrorMsg()
  19. {
  20. static const size_t buff_size = 255;
  21. #ifdef _WIN32
  22. static __declspec(thread) char err_str[buff_size] = {};
  23. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
  24. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  25. err_str, buff_size, nullptr);
  26. #else
  27. static __thread char err_str[buff_size] = {};
  28. // Thread safe (XSI-compliant)
  29. strerror_r(errno, err_str, buff_size);
  30. #endif
  31. return err_str;
  32. }