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