Error.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Error.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Error.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Error
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Foundation.h"
  16. #include "Poco/UnicodeConverter.h"
  17. #include "Poco/Error.h"
  18. #include <string>
  19. #include <string.h>
  20. #include <errno.h>
  21. namespace Poco {
  22. #ifdef POCO_OS_FAMILY_WINDOWS
  23. DWORD Error::last()
  24. {
  25. return GetLastError();
  26. }
  27. std::string Error::getMessage(DWORD errorCode)
  28. {
  29. std::string errMsg;
  30. DWORD dwFlg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
  31. #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
  32. LPWSTR lpMsgBuf = 0;
  33. if (FormatMessageW(dwFlg, 0, errorCode, 0, (LPWSTR) & lpMsgBuf, 0, NULL))
  34. UnicodeConverter::toUTF8(lpMsgBuf, errMsg);
  35. #else
  36. LPTSTR lpMsgBuf = 0;
  37. if (FormatMessageA(dwFlg, 0, errorCode, 0, (LPTSTR) & lpMsgBuf, 0, NULL))
  38. errMsg = lpMsgBuf;
  39. #endif
  40. LocalFree(lpMsgBuf);
  41. return errMsg;
  42. }
  43. #else
  44. int Error::last()
  45. {
  46. return errno;
  47. }
  48. std::string Error::getMessage(int errorCode)
  49. {
  50. /* Reentrant version of `strerror'.
  51. There are 2 flavors of `strerror_r', GNU which returns the string
  52. and may or may not use the supplied temporary buffer and POSIX one
  53. which fills the string into the buffer.
  54. To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
  55. without -D_GNU_SOURCE is needed, otherwise the GNU version is
  56. preferred.
  57. */
  58. #if defined _GNU_SOURCE && !POCO_ANDROID
  59. char errmsg[256] = "";
  60. return std::string(strerror_r(errorCode, errmsg, 256));
  61. #elif (_XOPEN_SOURCE >= 600) || POCO_ANDROID
  62. char errmsg[256] = "";
  63. strerror_r(errorCode, errmsg, 256);
  64. return errmsg;
  65. #else
  66. return std::string(strerror(errorCode));
  67. #endif
  68. }
  69. #endif
  70. } // namespace Poco