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
26namespace 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 LocalFree(lpMsgBuf);
47 #endif
48
49 return errMsg;
50 }
51
52
53#else
54
55
56 int Error::last()
57 {
58 return errno;
59 }
60
61
62 std::string Error::getMessage(int errorCode)
63 {
64#if defined _GNU_SOURCE || (_XOPEN_SOURCE >= 600) || POCO_OS == POCO_OS_ANDROID || __APPLE__
65 char errmsg[256] = "";
66 return std::string(strerror_result(strerror_r(errorCode, errmsg, sizeof(errmsg)), errmsg));
67#else
68 return std::string(strerror(errorCode));
69#endif
70 }
71
72
73#endif
74
75
76} // namespace Poco
77