summaryrefslogtreecommitdiffstats
path: root/libnetdutils
diff options
context:
space:
mode:
authorLuke Huang <huangluke@google.com>2019-03-14 21:19:13 +0800
committerLuke Huang <huangluke@google.com>2019-03-15 05:39:32 +0000
commitb257d61cd55c00a50d1eaaf4e7fcf436185c9a2c (patch)
tree64d7506a60930613bd01650636c7525b09435b5f /libnetdutils
parent2142a2b0554c9e06e2be272020d02819e046c0b3 (diff)
downloadplatform_system_netd-b257d61cd55c00a50d1eaaf4e7fcf436185c9a2c.tar.gz
platform_system_netd-b257d61cd55c00a50d1eaaf4e7fcf436185c9a2c.tar.bz2
platform_system_netd-b257d61cd55c00a50d1eaaf4e7fcf436185c9a2c.zip
Move DumpWriter to libnetdutils
resolver related component in libnetd_resolv needs it to easily print dump log. Bug: 122564854 Test: built, flashed, booted system/netd/tests/runtests.sh pass adb shell dumpsys netd, worked fine Change-Id: Ic97d5f21b738fc3074e9308f4846191e744ed479
Diffstat (limited to 'libnetdutils')
-rw-r--r--libnetdutils/Android.bp1
-rw-r--r--libnetdutils/DumpWriter.cpp72
-rw-r--r--libnetdutils/include/netdutils/DumpWriter.h68
3 files changed, 141 insertions, 0 deletions
diff --git a/libnetdutils/Android.bp b/libnetdutils/Android.bp
index f06087aa5..a1cbbbb5f 100644
--- a/libnetdutils/Android.bp
+++ b/libnetdutils/Android.bp
@@ -1,6 +1,7 @@
cc_library {
name: "libnetdutils",
srcs: [
+ "DumpWriter.cpp",
"Fd.cpp",
"InternetAddresses.cpp",
"Log.cpp",
diff --git a/libnetdutils/DumpWriter.cpp b/libnetdutils/DumpWriter.cpp
new file mode 100644
index 000000000..092ddbae2
--- /dev/null
+++ b/libnetdutils/DumpWriter.cpp
@@ -0,0 +1,72 @@
+/*
+ * 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 "netdutils/DumpWriter.h"
+
+#include <unistd.h>
+#include <limits>
+
+#include <android-base/stringprintf.h>
+#include <utils/String8.h>
+
+using android::base::StringAppendV;
+
+namespace android {
+namespace netdutils {
+
+namespace {
+
+const char kIndentString[] = " ";
+const size_t kIndentStringLen = strlen(kIndentString);
+
+} // namespace
+
+DumpWriter::DumpWriter(int fd) : mIndentLevel(0), mFd(fd) {}
+
+void DumpWriter::incIndent() {
+ if (mIndentLevel < std::numeric_limits<decltype(mIndentLevel)>::max()) {
+ mIndentLevel++;
+ }
+}
+
+void DumpWriter::decIndent() {
+ if (mIndentLevel > std::numeric_limits<decltype(mIndentLevel)>::min()) {
+ mIndentLevel--;
+ }
+}
+
+void DumpWriter::println(const std::string& line) {
+ if (!line.empty()) {
+ for (int i = 0; i < mIndentLevel; i++) {
+ ::write(mFd, kIndentString, kIndentStringLen);
+ }
+ ::write(mFd, line.c_str(), line.size());
+ }
+ ::write(mFd, "\n", 1);
+}
+
+// NOLINTNEXTLINE(cert-dcl50-cpp): Grandfathered C-style variadic function.
+void DumpWriter::println(const char* fmt, ...) {
+ std::string line;
+ va_list ap;
+ va_start(ap, fmt);
+ StringAppendV(&line, fmt, ap);
+ va_end(ap);
+ println(line);
+}
+
+} // namespace netdutils
+} // namespace android
diff --git a/libnetdutils/include/netdutils/DumpWriter.h b/libnetdutils/include/netdutils/DumpWriter.h
new file mode 100644
index 000000000..a50b5e60a
--- /dev/null
+++ b/libnetdutils/include/netdutils/DumpWriter.h
@@ -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.
+ */
+
+#ifndef NETDUTILS_DUMPWRITER_H_
+#define NETDUTILS_DUMPWRITER_H_
+
+#include <string>
+
+namespace android {
+namespace netdutils {
+
+class DumpWriter {
+ public:
+ DumpWriter(int fd);
+
+ void incIndent();
+ void decIndent();
+
+ void println(const std::string& line);
+ template <size_t n>
+ void println(const char line[n]) {
+ println(std::string(line));
+ }
+ // Hint to the compiler that it should apply printf validation of
+ // arguments (beginning at position 3) of the format (specified in
+ // position 2). Note that position 1 is the implicit "this" argument.
+ void println(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
+ void blankline() { println(""); }
+
+ private:
+ uint8_t mIndentLevel;
+ int mFd;
+};
+
+class ScopedIndent {
+ public:
+ ScopedIndent() = delete;
+ ScopedIndent(const ScopedIndent&) = delete;
+ ScopedIndent(ScopedIndent&&) = delete;
+ explicit ScopedIndent(DumpWriter& dw) : mDw(dw) { mDw.incIndent(); }
+ ~ScopedIndent() { mDw.decIndent(); }
+ ScopedIndent& operator=(const ScopedIndent&) = delete;
+ ScopedIndent& operator=(ScopedIndent&&) = delete;
+
+ // TODO: consider additional {inc,dec}Indent methods and a counter that
+ // can be used to unwind all pending increments on exit.
+
+ private:
+ DumpWriter& mDw;
+};
+
+} // namespace netdutils
+} // namespace android
+
+#endif // NETDUTILS_DUMPWRITER_H_