Add cmd to let statsd print all logs it received for debugging.

It only works on eng build. And all code is behind a build flag, so the
code will be stripped out in production builds.

Bug: 78239479
Test: manual
Change-Id: I20ee51822d18e6c77ca324a5327712cbed09593e
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index 0e7b4f9..0ca3caa 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -334,6 +334,10 @@
         if (!args[0].compare(String8("clear-puller-cache"))) {
             return cmd_clear_puller_cache(out);
         }
+
+        if (!args[0].compare(String8("print-logs"))) {
+            return cmd_print_logs(out, args);
+        }
     }
 
     print_cmd_help(out);
@@ -419,6 +423,9 @@
     fprintf(out, "\n");
     fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
     fprintf(out, "  Clear cached puller data.\n");
+    fprintf(out, "\n");
+    fprintf(out, "usage: adb shell cmd stats print-logs\n");
+    fprintf(out, "      Only works on eng build\n");
 }
 
 status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
@@ -738,6 +745,22 @@
     }
 }
 
+status_t StatsService::cmd_print_logs(FILE* out, const Vector<String8>& args) {
+    IPCThreadState* ipc = IPCThreadState::self();
+    VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", ipc->getCallingPid(),
+         ipc->getCallingUid());
+    if (checkCallingPermission(String16(kPermissionDump))) {
+        bool enabled = true;
+        if (args.size() >= 2) {
+            enabled = atoi(args[1].c_str()) != 0;
+        }
+        mProcessor->setPrintLogs(enabled);
+        return NO_ERROR;
+    } else {
+        return PERMISSION_DENIED;
+    }
+}
+
 Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
                                       const vector<String16>& app) {
     ENFORCE_UID(AID_SYSTEM);