| 1 | // | 
|---|
| 2 | // Error.cpp | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Core | 
|---|
| 6 | // Module:  Error | 
|---|
| 7 | // | 
|---|
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
|---|
| 9 | // and Contributors. | 
|---|
| 10 | // | 
|---|
| 11 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 12 | // | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | #include "Poco/Foundation.h" | 
|---|
| 16 | #include "Poco/UnicodeConverter.h" | 
|---|
| 17 | #include "Poco/Error.h" | 
|---|
| 18 | #ifdef POCO_OS_FAMILY_WINDOWS | 
|---|
| 19 | #include "Poco/UnWindows.h" | 
|---|
| 20 | #endif | 
|---|
| 21 | #include <string> | 
|---|
| 22 | #include <string.h> | 
|---|
| 23 | #include <errno.h> | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | namespace Poco { | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | #ifdef POCO_OS_FAMILY_WINDOWS | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | Poco::UInt32 Error::last() | 
|---|
| 33 | { | 
|---|
| 34 | return GetLastError(); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | std::string Error::getMessage(Poco::UInt32 errorCode) | 
|---|
| 39 | { | 
|---|
| 40 | std::string errMsg; | 
|---|
| 41 | DWORD dwFlg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; | 
|---|
| 42 | #if !defined(POCO_NO_WSTRING) | 
|---|
| 43 | LPWSTR lpMsgBuf = 0; | 
|---|
| 44 | if (FormatMessageW(dwFlg, 0, errorCode, 0, (LPWSTR) & lpMsgBuf, 0, NULL)) | 
|---|
| 45 | UnicodeConverter::toUTF8(lpMsgBuf, errMsg); | 
|---|
| 46 | #endif | 
|---|
| 47 | LocalFree(lpMsgBuf); | 
|---|
| 48 | return errMsg; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | #else | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | int Error::last() | 
|---|
| 56 | { | 
|---|
| 57 | return errno; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | std::string Error::getMessage(int errorCode) | 
|---|
| 62 | { | 
|---|
| 63 | #if defined _GNU_SOURCE || (_XOPEN_SOURCE >= 600) || POCO_ANDROID || __APPLE__ | 
|---|
| 64 | char errmsg[256] = ""; | 
|---|
| 65 | return std::string(strerror_result(strerror_r(errorCode, errmsg, sizeof(errmsg)), errmsg)); | 
|---|
| 66 | #else | 
|---|
| 67 | return std::string(strerror(errorCode)); | 
|---|
| 68 | #endif | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | } // namespace Poco | 
|---|
| 76 |  | 
|---|