1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/errors.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <windows.h>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
22*8f0ba417SAndroid Build Coastguard Worker #include "android-base/strings.h"
23*8f0ba417SAndroid Build Coastguard Worker #include "android-base/utf8.h"
24*8f0ba417SAndroid Build Coastguard Worker
25*8f0ba417SAndroid Build Coastguard Worker // A Windows error code is a DWORD. It's simpler to use an int error code for
26*8f0ba417SAndroid Build Coastguard Worker // both Unix and Windows if possible, but if this fails we'll need a different
27*8f0ba417SAndroid Build Coastguard Worker // function signature for each.
28*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(int) >= sizeof(DWORD),
29*8f0ba417SAndroid Build Coastguard Worker "Windows system error codes are too large to fit in an int.");
30*8f0ba417SAndroid Build Coastguard Worker
31*8f0ba417SAndroid Build Coastguard Worker namespace android {
32*8f0ba417SAndroid Build Coastguard Worker namespace base {
33*8f0ba417SAndroid Build Coastguard Worker
34*8f0ba417SAndroid Build Coastguard Worker static constexpr DWORD kErrorMessageBufferSize = 256;
35*8f0ba417SAndroid Build Coastguard Worker
SystemErrorCodeToString(int int_error_code)36*8f0ba417SAndroid Build Coastguard Worker std::string SystemErrorCodeToString(int int_error_code) {
37*8f0ba417SAndroid Build Coastguard Worker WCHAR msgbuf[kErrorMessageBufferSize];
38*8f0ba417SAndroid Build Coastguard Worker DWORD error_code = int_error_code;
39*8f0ba417SAndroid Build Coastguard Worker DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
40*8f0ba417SAndroid Build Coastguard Worker DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
41*8f0ba417SAndroid Build Coastguard Worker kErrorMessageBufferSize, nullptr);
42*8f0ba417SAndroid Build Coastguard Worker if (len == 0) {
43*8f0ba417SAndroid Build Coastguard Worker return android::base::StringPrintf(
44*8f0ba417SAndroid Build Coastguard Worker "Error %lu while retrieving message for error %lu", GetLastError(),
45*8f0ba417SAndroid Build Coastguard Worker error_code);
46*8f0ba417SAndroid Build Coastguard Worker }
47*8f0ba417SAndroid Build Coastguard Worker
48*8f0ba417SAndroid Build Coastguard Worker // Convert UTF-16 to UTF-8.
49*8f0ba417SAndroid Build Coastguard Worker std::string msg;
50*8f0ba417SAndroid Build Coastguard Worker if (!android::base::WideToUTF8(msgbuf, &msg)) {
51*8f0ba417SAndroid Build Coastguard Worker return android::base::StringPrintf(
52*8f0ba417SAndroid Build Coastguard Worker "Error %lu while converting message for error %lu from UTF-16 to UTF-8",
53*8f0ba417SAndroid Build Coastguard Worker GetLastError(), error_code);
54*8f0ba417SAndroid Build Coastguard Worker }
55*8f0ba417SAndroid Build Coastguard Worker
56*8f0ba417SAndroid Build Coastguard Worker // Messages returned by the system end with line breaks.
57*8f0ba417SAndroid Build Coastguard Worker msg = android::base::Trim(msg);
58*8f0ba417SAndroid Build Coastguard Worker
59*8f0ba417SAndroid Build Coastguard Worker // There are many Windows error messages compared to POSIX, so include the
60*8f0ba417SAndroid Build Coastguard Worker // numeric error code for easier, quicker, accurate identification. Use
61*8f0ba417SAndroid Build Coastguard Worker // decimal instead of hex because there are decimal ranges like 10000-11999
62*8f0ba417SAndroid Build Coastguard Worker // for Winsock.
63*8f0ba417SAndroid Build Coastguard Worker android::base::StringAppendF(&msg, " (%lu)", error_code);
64*8f0ba417SAndroid Build Coastguard Worker return msg;
65*8f0ba417SAndroid Build Coastguard Worker }
66*8f0ba417SAndroid Build Coastguard Worker
67*8f0ba417SAndroid Build Coastguard Worker } // namespace base
68*8f0ba417SAndroid Build Coastguard Worker } // namespace android
69