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
diff --git a/server/Android.bp b/server/Android.bp
index b655988..40b7baa 100644
--- a/server/Android.bp
+++ b/server/Android.bp
@@ -161,7 +161,6 @@
     srcs: [
         "CommandListener.cpp",
         "DummyNetwork.cpp",
-        "DumpWriter.cpp",
         "EventReporter.cpp",
         "FwmarkServer.cpp",
         "LocalNetwork.cpp",
diff --git a/server/Dns64Configuration.cpp b/server/Dns64Configuration.cpp
index 6df108b..1c14889 100644
--- a/server/Dns64Configuration.cpp
+++ b/server/Dns64Configuration.cpp
@@ -29,13 +29,14 @@
 #include <netd_resolv/resolv.h>
 #include <netd_resolv/resolv_stub.h>
 
-#include "DumpWriter.h"
 #include "NetworkController.h"
 #include "netdutils/BackoffSequence.h"
+#include "netdutils/DumpWriter.h"
 #include "netid_client.h"
 
 namespace android {
 
+using netdutils::DumpWriter;
 using netdutils::IPAddress;
 using netdutils::IPPrefix;
 
diff --git a/server/Dns64Configuration.h b/server/Dns64Configuration.h
index 8024b50..9f8f646 100644
--- a/server/Dns64Configuration.h
+++ b/server/Dns64Configuration.h
@@ -24,6 +24,7 @@
 #include <unordered_map>
 
 #include <android-base/thread_annotations.h>
+#include "netdutils/DumpWriter.h"
 #include "netdutils/InternetAddresses.h"
 
 struct android_net_context;
@@ -31,7 +32,6 @@
 namespace android {
 namespace net {
 
-class DumpWriter;
 class NetworkController;
 
 /**
@@ -80,7 +80,7 @@
     void stopPrefixDiscovery(unsigned netId);
     netdutils::IPPrefix getPrefix64(unsigned netId) const;
 
-    void dump(DumpWriter& dw, unsigned netId);
+    void dump(netdutils::DumpWriter& dw, unsigned netId);
 
   private:
     struct Dns64Config {
diff --git a/server/DumpWriter.cpp b/server/DumpWriter.cpp
deleted file mode 100644
index ef3ffc8..0000000
--- a/server/DumpWriter.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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 "DumpWriter.h"
-
-#include <unistd.h>
-#include <limits>
-
-#include <android-base/stringprintf.h>
-#include <utils/String8.h>
-
-using android::base::StringAppendV;
-
-namespace android {
-namespace net {
-
-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 net
-}  // namespace android
diff --git a/server/DumpWriter.h b/server/DumpWriter.h
deleted file mode 100644
index e45a76d..0000000
--- a/server/DumpWriter.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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 NETD_SERVER_DUMPWRITER_H_
-#define NETD_SERVER_DUMPWRITER_H_
-
-#include <string>
-
-namespace android {
-namespace net {
-
-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 net
-}  // namespace android
-
-#endif  // NETD_SERVER_DUMPWRITER_H_
diff --git a/server/NetdNativeService.cpp b/server/NetdNativeService.cpp
index dcf8e02..7728e0e 100644
--- a/server/NetdNativeService.cpp
+++ b/server/NetdNativeService.cpp
@@ -37,7 +37,6 @@
 #include <openssl/base64.h>
 
 #include "Controllers.h"
-#include "DumpWriter.h"
 #include "InterfaceController.h"
 #include "NetdConstants.h"  // SHA256_SIZE
 #include "NetdNativeService.h"
@@ -48,12 +47,15 @@
 #include "SockDiag.h"
 #include "UidRanges.h"
 #include "android/net/BnNetd.h"
+#include "netdutils/DumpWriter.h"
 #include "netid_client.h"  // NETID_UNSET
 
 using android::base::StringPrintf;
 using android::base::WriteStringToFile;
 using android::net::TetherStatsParcel;
 using android::net::UidRangeParcel;
+using android::netdutils::DumpWriter;
+using android::netdutils::ScopedIndent;
 using android::os::ParcelFileDescriptor;
 
 namespace android {
diff --git a/server/NetworkController.cpp b/server/NetworkController.cpp
index b3297bb..a77a547 100644
--- a/server/NetworkController.cpp
+++ b/server/NetworkController.cpp
@@ -36,16 +36,18 @@
 
 #include "Controllers.h"
 #include "DummyNetwork.h"
-#include "DumpWriter.h"
 #include "Fwmark.h"
 #include "LocalNetwork.h"
 #include "PhysicalNetwork.h"
 #include "RouteController.h"
 #include "VirtualNetwork.h"
+#include "netdutils/DumpWriter.h"
 #include "netid_client.h"
 
 #define DBG 0
 
+using android::netdutils::DumpWriter;
+
 namespace android {
 namespace net {
 
diff --git a/server/NetworkController.h b/server/NetworkController.h
index 984f507..d765ae7 100644
--- a/server/NetworkController.h
+++ b/server/NetworkController.h
@@ -23,6 +23,7 @@
 
 #include "NetdConstants.h"
 #include "Permission.h"
+#include "netdutils/DumpWriter.h"
 
 #include <sys/types.h>
 #include <list>
@@ -70,7 +71,6 @@
     return (((net_handle_t)fromNetId << 32) | kHandleMagic);
 }
 
-class DumpWriter;
 class Network;
 class UidRanges;
 class VirtualNetwork;
@@ -140,9 +140,9 @@
     void allowProtect(const std::vector<uid_t>& uids);
     void denyProtect(const std::vector<uid_t>& uids);
 
-    void dump(DumpWriter& dw);
+    void dump(netdutils::DumpWriter& dw);
 
-private:
+  private:
     bool isValidNetworkLocked(unsigned netId) const;
     Network* getNetworkLocked(unsigned netId) const;
     uint32_t getNetworkForDnsLocked(unsigned* netId, uid_t uid) const;
diff --git a/server/Process.cpp b/server/Process.cpp
index 1890620..f43e82d 100644
--- a/server/Process.cpp
+++ b/server/Process.cpp
@@ -39,11 +39,12 @@
 namespace android {
 
 using base::StringPrintf;
+using netdutils::DumpWriter;
 using netdutils::Fd;
-using netdutils::UniqueFd;
 using netdutils::isOk;
 using netdutils::makeCleanup;
 using netdutils::makeSlice;
+using netdutils::UniqueFd;
 
 namespace net {
 namespace process {
diff --git a/server/Process.h b/server/Process.h
index 66c25c5..317da44 100644
--- a/server/Process.h
+++ b/server/Process.h
@@ -17,7 +17,7 @@
 #ifndef NETD_SERVER_PROCESS_H_
 #define NETD_SERVER_PROCESS_H_
 
-#include "DumpWriter.h"
+#include "netdutils/DumpWriter.h"
 
 #include <string>
 
@@ -51,7 +51,7 @@
     const std::string pidFile;
 };
 
-void dump(DumpWriter& dw);
+void dump(netdutils::DumpWriter& dw);
 
 }  // namespace process
 }  // namespace net
diff --git a/server/ResolverController.cpp b/server/ResolverController.cpp
index 87acca7..27ef3a2 100644
--- a/server/ResolverController.cpp
+++ b/server/ResolverController.cpp
@@ -39,7 +39,6 @@
 #include <android/net/metrics/INetdEventListener.h>
 
 #include "Controllers.h"
-#include "DumpWriter.h"
 #include "EventReporter.h"
 #include "Fwmark.h"
 #include "NetdConstants.h"
@@ -50,8 +49,12 @@
 #include "netd_resolv/resolv.h"
 #include "netd_resolv/resolv_stub.h"
 #include "netd_resolv/stats.h"
+#include "netdutils/DumpWriter.h"
 
 namespace android {
+
+using netdutils::DumpWriter;
+
 namespace net {
 
 namespace {
diff --git a/server/ResolverController.h b/server/ResolverController.h
index 1a3b315..0c71be5 100644
--- a/server/ResolverController.h
+++ b/server/ResolverController.h
@@ -21,6 +21,7 @@
 #include <vector>
 
 #include "Dns64Configuration.h"
+#include "netdutils/DumpWriter.h"
 #include "netdutils/InternetAddresses.h"
 
 struct res_params;
@@ -28,7 +29,6 @@
 namespace android {
 namespace net {
 
-class DumpWriter;
 struct ResolverStats;
 
 class ResolverController {
@@ -71,7 +71,7 @@
 
     bool initResolver();
 
-    void dump(DumpWriter& dw, unsigned netId);
+    void dump(netdutils::DumpWriter& dw, unsigned netId);
 
   private:
     Dns64Configuration mDns64Configuration;
diff --git a/server/TcpSocketMonitor.cpp b/server/TcpSocketMonitor.cpp
index 5478e5d..f4b505d 100644
--- a/server/TcpSocketMonitor.cpp
+++ b/server/TcpSocketMonitor.cpp
@@ -26,9 +26,12 @@
 #include <linux/tcp.h>
 
 #include "Controllers.h"
-#include "DumpWriter.h"
 #include "SockDiag.h"
 #include "TcpSocketMonitor.h"
+#include "netdutils/DumpWriter.h"
+
+using android::netdutils::DumpWriter;
+using android::netdutils::ScopedIndent;
 
 namespace android {
 namespace net {
diff --git a/server/TcpSocketMonitor.h b/server/TcpSocketMonitor.h
index 43c2c04..785f8ce 100644
--- a/server/TcpSocketMonitor.h
+++ b/server/TcpSocketMonitor.h
@@ -24,6 +24,7 @@
 #include <unordered_map>
 
 #include <android-base/thread_annotations.h>
+#include "netdutils/DumpWriter.h"
 #include "utils/String16.h"
 
 #include "Fwmark.h"
@@ -36,8 +37,6 @@
 
 using std::chrono::milliseconds;
 
-class DumpWriter;
-
 class TcpSocketMonitor {
   public:
     using time_point = std::chrono::time_point<std::chrono::steady_clock>;
@@ -78,7 +77,7 @@
     TcpSocketMonitor();
     ~TcpSocketMonitor();
 
-    void dump(DumpWriter& dw);
+    void dump(netdutils::DumpWriter& dw);
     void setPollingInterval(milliseconds duration);
     void resumePolling();
     void suspendPolling();
diff --git a/server/TrafficController.cpp b/server/TrafficController.cpp
index 9e9e257..74a80fe 100644
--- a/server/TrafficController.cpp
+++ b/server/TrafficController.cpp
@@ -47,10 +47,10 @@
 #include "TrafficController.h"
 #include "bpf/BpfMap.h"
 
-#include "DumpWriter.h"
 #include "FirewallController.h"
 #include "InterfaceController.h"
 #include "NetlinkListener.h"
+#include "netdutils/DumpWriter.h"
 #include "qtaguid/qtaguid.h"
 
 using namespace android::bpf;  // NOLINT(google-build-using-namespace): grandfathered
@@ -60,7 +60,9 @@
 
 using base::StringPrintf;
 using base::unique_fd;
+using netdutils::DumpWriter;
 using netdutils::extract;
+using netdutils::ScopedIndent;
 using netdutils::Slice;
 using netdutils::sSyscalls;
 using netdutils::Status;
diff --git a/server/TrafficController.h b/server/TrafficController.h
index 1576882..e0a4017 100644
--- a/server/TrafficController.h
+++ b/server/TrafficController.h
@@ -27,6 +27,7 @@
 #include "android-base/unique_fd.h"
 #include "bpf/BpfMap.h"
 #include "netdbpf/bpf_shared.h"
+#include "netdutils/DumpWriter.h"
 #include "netdutils/StatusOr.h"
 #include "utils/String16.h"
 
@@ -39,8 +40,6 @@
 namespace android {
 namespace net {
 
-class DumpWriter;
-
 class TrafficController {
   public:
     TrafficController();
@@ -103,7 +102,7 @@
     netdutils::Status updateOwnerMapEntry(UidOwnerMatchType match, uid_t uid, FirewallRule rule,
                                           FirewallType type);
 
-    void dump(DumpWriter& dw, bool verbose);
+    void dump(netdutils::DumpWriter& dw, bool verbose);
 
     netdutils::Status replaceUidsInMap(UidOwnerMatchType match, const std::vector<int32_t>& uids);
 
diff --git a/server/XfrmController.cpp b/server/XfrmController.cpp
index de9aa0b..b7f6649 100644
--- a/server/XfrmController.cpp
+++ b/server/XfrmController.cpp
@@ -54,7 +54,6 @@
 #include <log/log.h>
 #include <log/log_properties.h>
 #include <logwrap/logwrap.h>
-#include "DumpWriter.h"
 #include "Fwmark.h"
 #include "InterfaceController.h"
 #include "NetdConstants.h"
@@ -65,12 +64,15 @@
 #include "android-base/stringprintf.h"
 #include "android-base/strings.h"
 #include "android-base/unique_fd.h"
+#include "netdutils/DumpWriter.h"
 #include "netdutils/Fd.h"
 #include "netdutils/Slice.h"
 #include "netdutils/Syscalls.h"
 
 using android::net::INetd;
+using android::netdutils::DumpWriter;
 using android::netdutils::Fd;
+using android::netdutils::ScopedIndent;
 using android::netdutils::Slice;
 using android::netdutils::Status;
 using android::netdutils::StatusOr;
diff --git a/server/XfrmController.h b/server/XfrmController.h
index fb60be9..a38bbeb 100644
--- a/server/XfrmController.h
+++ b/server/XfrmController.h
@@ -32,6 +32,7 @@
 
 #include "NetdConstants.h"
 #include "android-base/unique_fd.h"
+#include "netdutils/DumpWriter.h"
 #include "netdutils/Slice.h"
 #include "netdutils/Status.h"
 #include "sysutils/SocketClient.h"
@@ -51,7 +52,6 @@
 // Suggest we avoid the smallest and largest ints
 class XfrmMessage;
 class TransportModeSecurityAssociation;
-class DumpWriter;
 
 class XfrmSocket {
 public:
@@ -259,7 +259,7 @@
 
     static netdutils::Status ipSecRemoveTunnelInterface(const std::string& deviceName);
 
-    void dump(DumpWriter& dw);
+    void dump(netdutils::DumpWriter& dw);
 
     // Some XFRM netlink attributes comprise a header, a struct, and some data
     // after the struct. We wrap all of those in one struct for easier