Put VM log to the system log

MicrodroidHostTestCases now uses the new "--log" option to save console
output from the VM to a file. Then the file is redirected to the system
log in background. That allows us to see what was happening inside
microdroid when it crashes during booting.

Bug: 191613547
Test: atest MicrodroidHostTestCases and see the device logcat. It has
log from the VM.

Change-Id: I153c64acdf46da23b0b1f3d35e32d7a6664661eb
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 2457797..d16f307 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -46,6 +46,8 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -234,6 +236,8 @@
         // Create payload.img
         createPayloadImage(apkName, packageName, configPath);
 
+        final String logPath = TEST_ROOT + "log.txt";
+
         // Run the VM
         runOnAndroid("start", "virtualizationservice");
         String ret =
@@ -241,8 +245,20 @@
                         VIRT_APEX + "bin/vm",
                         "run",
                         "--daemonize",
+                        "--log " + logPath,
                         VIRT_APEX + "etc/microdroid.json");
 
+        // Redirect log.txt to logd using logwrapper
+        ExecutorService executor = Executors.newFixedThreadPool(1);
+        executor.execute(
+                () -> {
+                    try {
+                        runOnAndroid("logwrapper", "tail", "-f", "-n +0", logPath);
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                });
+
         // Retrieve the CID from the vm tool output
         Pattern pattern = Pattern.compile("with CID (\\d+)");
         Matcher matcher = pattern.matcher(ret);