AAPT2: Better debugging output

Test: make aapt2_tests
Change-Id: I7778b773201381538dc1f2e376abee4eb33e44c0
diff --git a/tools/aapt2/cmd/Dump.cpp b/tools/aapt2/cmd/Dump.cpp
index bc8f1dc..bc7f5a8 100644
--- a/tools/aapt2/cmd/Dump.cpp
+++ b/tools/aapt2/cmd/Dump.cpp
@@ -14,8 +14,10 @@
  * limitations under the License.
  */
 
+#include <cinttypes>
 #include <vector>
 
+#include "android-base/stringprintf.h"
 #include "androidfw/StringPiece.h"
 
 #include "Debug.h"
@@ -27,9 +29,12 @@
 #include "io/FileStream.h"
 #include "io/ZipArchive.h"
 #include "process/IResourceTableConsumer.h"
+#include "text/Printer.h"
 #include "util/Files.h"
 
+using ::aapt::text::Printer;
 using ::android::StringPiece;
+using ::android::base::StringPrintf;
 
 namespace aapt {
 
@@ -48,16 +53,28 @@
 }
 
 static void DumpCompiledFile(const ResourceFile& file, const Source& source, off64_t offset,
-                             size_t len) {
-  std::cout << "Resource: " << file.name << "\n"
-            << "Config:   " << file.config << "\n"
-            << "Source:   " << file.source << "\n"
-            << "Type:     " << ResourceFileTypeToString(file.type) << "\n"
-            << "DataOff:  " << offset << "\n"
-            << "DataLen:  " << len << "\n";
+                             size_t len, Printer* printer) {
+  printer->Print("Resource: ");
+  printer->Println(file.name.to_string());
+
+  printer->Print("Config:   ");
+  printer->Println(file.config.to_string());
+
+  printer->Print("Source:   ");
+  printer->Println(file.source.to_string());
+
+  printer->Print("Type:     ");
+  printer->Println(ResourceFileTypeToString(file.type));
+
+  printer->Println(StringPrintf("Data:     offset=%" PRIi64 " length=%zd", offset, len));
 }
 
 static bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
+  // Use a smaller buffer so that there is less latency for dumping to stdout.
+  constexpr size_t kStdOutBufferSize = 1024u;
+  io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize);
+  Printer printer(&fout);
+
   DebugPrintTableOptions print_options;
   print_options.show_sources = true;
 
@@ -83,6 +100,8 @@
                                          << "failed to parse table: " << err);
         return false;
       }
+
+      printer.Println("Proto APK");
     } else if (io::IFile* file = zip->FindFile("resources.arsc")) {
       std::unique_ptr<io::IData> data = file->OpenAsData();
       if (!data) {
@@ -95,9 +114,11 @@
       if (!parser.Parse()) {
         return false;
       }
+
+      printer.Println("Binary APK");
     }
 
-    Debug::PrintTable(table, print_options);
+    Debug::PrintTable(table, print_options, &printer);
     return true;
   }
 
@@ -118,9 +139,12 @@
     return false;
   }
 
+  printer.Println("AAPT2 Container (APC)");
   ContainerReaderEntry* entry;
   while ((entry = reader.Next()) != nullptr) {
     if (entry->Type() == ContainerEntryType::kResTable) {
+      printer.Println("kResTable");
+
       pb::ResourceTable pb_table;
       if (!entry->GetResTable(&pb_table)) {
         context->GetDiagnostics()->Error(DiagMessage(file_path)
@@ -136,8 +160,11 @@
         continue;
       }
 
-      Debug::PrintTable(table, print_options);
+      printer.Indent();
+      Debug::PrintTable(table, print_options, &printer);
+      printer.Undent();
     } else if (entry->Type() == ContainerEntryType::kResFile) {
+      printer.Println("kResFile");
       pb::internal::CompiledFile pb_compiled_file;
       off64_t offset;
       size_t length;
@@ -155,7 +182,9 @@
         continue;
       }
 
-      DumpCompiledFile(file, Source(file_path), offset, length);
+      printer.Indent();
+      DumpCompiledFile(file, Source(file_path), offset, length, &printer);
+      printer.Undent();
     }
   }
   return true;