Bitcoin ABC 0.32.11
P2P Digital Currency
syserror.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#if defined(HAVE_CONFIG_H)
6#include <config/bitcoin-config.h>
7#endif
8
9#include <tinyformat.h>
10#include <util/syserror.h>
11
12#include <cstring>
13
14#if defined(WIN32)
15#include <codecvt>
16#include <locale>
17#include <windows.h>
18#endif
19
20std::string SysErrorString(int err) {
21 char buf[1024];
26 const char *s = nullptr;
27#ifdef WIN32
28 if (strerror_s(buf, sizeof(buf), err) == 0) {
29 s = buf;
30 }
31#else
32#ifdef STRERROR_R_CHAR_P
33 /* GNU variant can return a pointer outside the passed buffer */
34 s = strerror_r(err, buf, sizeof(buf));
35#else
36 /* POSIX variant always returns message in buffer */
37 if (strerror_r(err, buf, sizeof(buf)) == 0) {
38 s = buf;
39 }
40#endif
41#endif
42 if (s != nullptr) {
43 return strprintf("%s (%d)", s, err);
44 } else {
45 return strprintf("Unknown error (%d)", err);
46 }
47}
48
49#if defined(WIN32)
50std::string Win32ErrorString(int err) {
51 wchar_t buf[256];
52 buf[0] = 0;
53 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
54 FORMAT_MESSAGE_IGNORE_INSERTS |
55 FORMAT_MESSAGE_MAX_WIDTH_MASK,
56 nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
57 buf, ARRAYSIZE(buf), nullptr)) {
58 return strprintf(
59 "%s (%d)",
60 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>()
61 .to_bytes(buf),
62 err);
63 } else {
64 return strprintf("Unknown error (%d)", err);
65 }
66}
67#endif
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:20
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202