Merge "base: add SystemErrorCodeToString() function." am: 79d8f07a6f
am: cbb5d714d7

* commit 'cbb5d714d79dbc785a90fd17082227321cee9d92':
  base: add SystemErrorCodeToString() function.
diff --git a/Android.mk b/Android.mk
index 18f8686..d20a81f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -24,10 +24,18 @@
     strings.cpp \
     test_utils.cpp \
 
+libbase_linux_src_files := \
+    errors_unix.cpp \
+
+libbase_darwin_src_files := \
+    errors_unix.cpp \
+
 libbase_windows_src_files := \
+    errors_windows.cpp \
     utf8.cpp \
 
 libbase_test_src_files := \
+    errors_test.cpp \
     file_test.cpp \
     logging_test.cpp \
     parseint_test.cpp \
@@ -55,10 +63,7 @@
 include $(CLEAR_VARS)
 LOCAL_MODULE := libbase
 LOCAL_CLANG := true
-LOCAL_SRC_FILES := $(libbase_src_files)
-LOCAL_SRC_FILES_darwin := $(libbase_darwin_src_files)
-LOCAL_SRC_FILES_linux := $(libbase_linux_src_files)
-LOCAL_SRC_FILES_windows := $(libbase_windows_src_files)
+LOCAL_SRC_FILES := $(libbase_src_files) $(libbase_linux_src_files)
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 LOCAL_CPPFLAGS := $(libbase_cppflags) $(libbase_linux_cppflags)
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
diff --git a/errors_test.cpp b/errors_test.cpp
new file mode 100644
index 0000000..8e7cdd1
--- /dev/null
+++ b/errors_test.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace base {
+
+// Error strings aren't consistent enough across systems to test the output,
+// just make sure we can compile correctly and nothing crashes even if we send
+// it possibly bogus error codes.
+TEST(ErrorsTest, TestSystemErrorString) {
+  SystemErrorCodeToString(-1);
+  SystemErrorCodeToString(0);
+  SystemErrorCodeToString(1);
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/errors_unix.cpp b/errors_unix.cpp
new file mode 100644
index 0000000..296995e
--- /dev/null
+++ b/errors_unix.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <errno.h>
+
+namespace android {
+namespace base {
+
+std::string SystemErrorCodeToString(int error_code) {
+  return strerror(error_code);
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/errors_windows.cpp b/errors_windows.cpp
new file mode 100644
index 0000000..a5ff511
--- /dev/null
+++ b/errors_windows.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/errors.h"
+
+#include <windows.h>
+
+#include "android-base/stringprintf.h"
+#include "android-base/strings.h"
+#include "android-base/utf8.h"
+
+// A Windows error code is a DWORD. It's simpler to use an int error code for
+// both Unix and Windows if possible, but if this fails we'll need a different
+// function signature for each.
+static_assert(sizeof(int) >= sizeof(DWORD),
+              "Windows system error codes are too large to fit in an int.");
+
+namespace android {
+namespace base {
+
+static constexpr DWORD kErrorMessageBufferSize = 256;
+
+std::string SystemErrorCodeToString(int int_error_code) {
+  WCHAR msgbuf[kErrorMessageBufferSize];
+  DWORD error_code = int_error_code;
+  DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
+  DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
+                             kErrorMessageBufferSize, nullptr);
+  if (len == 0) {
+    return android::base::StringPrintf(
+        "Error %lu while retrieving message for error %lu", GetLastError(),
+        error_code);
+  }
+
+  // Convert UTF-16 to UTF-8.
+  std::string msg;
+  if (!android::base::WideToUTF8(msgbuf, &msg)) {
+    return android::base::StringPrintf(
+        "Error %lu while converting message for error %lu from UTF-16 to UTF-8",
+        GetLastError(), error_code);
+  }
+
+  // Messages returned by the system end with line breaks.
+  msg = android::base::Trim(msg);
+
+  // There are many Windows error messages compared to POSIX, so include the
+  // numeric error code for easier, quicker, accurate identification. Use
+  // decimal instead of hex because there are decimal ranges like 10000-11999
+  // for Winsock.
+  android::base::StringAppendF(&msg, " (%lu)", error_code);
+  return msg;
+}
+
+}  // namespace base
+}  // namespace android
diff --git a/include/android-base/errors.h b/include/android-base/errors.h
new file mode 100644
index 0000000..ca621fa
--- /dev/null
+++ b/include/android-base/errors.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Portable error handling functions. This is only necessary for host-side
+// code that needs to be cross-platform; code that is only run on Unix should
+// just use errno and strerror() for simplicity.
+//
+// There is some complexity since Windows has (at least) three different error
+// numbers, not all of which share the same type:
+//   * errno: for C runtime errors.
+//   * GetLastError(): Windows non-socket errors.
+//   * WSAGetLastError(): Windows socket errors.
+// errno can be passed to strerror() on all platforms, but the other two require
+// special handling to get the error string. Refer to Microsoft documentation
+// to determine which error code to check for each function.
+
+#ifndef BASE_ERRORS_H
+#define BASE_ERRORS_H
+
+#include <string>
+
+namespace android {
+namespace base {
+
+// Returns a string describing the given system error code. |error_code| must
+// be errno on Unix or GetLastError()/WSAGetLastError() on Windows. Passing
+// errno on Windows has undefined behavior.
+std::string SystemErrorCodeToString(int error_code);
+
+}  // namespace base
+}  // namespace android
+
+#endif  // BASE_ERRORS_H