Further simplify the Logger class

Bug: N/A
Test: N/A
Change-Id: I9598974c0a1aa87afc36512b488fb9f602ec0433
diff --git a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
index d435395..006476d 100644
--- a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
+++ b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/Logger.java
@@ -20,6 +20,8 @@
 import android.system.virtualmachine.VirtualMachineException;
 import android.util.Log;
 
+import libcore.io.Streams;
+
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -29,79 +31,36 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
-import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
 
 /**
  * Forwards VM's console output to a file on the Android side, and VM's log output to Android logd.
  */
 class Logger {
-    private static final String TAG = MainActivity.TAG;
+    private Logger() {}
 
-    Logger(VirtualMachine vm, Path path, Executor executor) {
+    static void setup(VirtualMachine vm, Path path, ExecutorService executor) {
         try {
             InputStream console = vm.getConsoleOutput();
-            OutputStream consoleLogFile =
-                    new LineBufferedOutputStream(
-                            Files.newOutputStream(path, StandardOpenOption.CREATE));
-            executor.execute(new CopyStreamTask("console", console, consoleLogFile));
+            OutputStream file = Files.newOutputStream(path, StandardOpenOption.CREATE);
+            executor.submit(() -> Streams.copy(console, new LineBufferedOutputStream(file)));
 
             InputStream log = vm.getLogOutput();
-            executor.execute(new Reader("log", log));
+            executor.submit(() -> writeToLogd(log, vm.getName()));
         } catch (VirtualMachineException | IOException e) {
             throw new RuntimeException(e);
         }
     }
 
-    /** Reads data from an input stream and posts it to the output data */
-    private static class Reader implements Runnable {
-        private final String mName;
-        private final InputStream mStream;
-
-        Reader(String name, InputStream stream) {
-            mName = name;
-            mStream = stream;
+    private static boolean writeToLogd(InputStream input, String vmName) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
+        String line;
+        while ((line = reader.readLine()) != null && !Thread.interrupted()) {
+            Log.d(vmName, line);
         }
-
-        @Override
-        public void run() {
-            try {
-                BufferedReader reader = new BufferedReader(new InputStreamReader(mStream));
-                String line;
-                while ((line = reader.readLine()) != null && !Thread.interrupted()) {
-                    Log.d(TAG, mName + ": " + line);
-                }
-            } catch (IOException e) {
-                Log.e(TAG, "Exception while posting " + mName + " output: " + e.getMessage());
-            }
-        }
-    }
-
-    private static class CopyStreamTask implements Runnable {
-        private final String mName;
-        private final InputStream mIn;
-        private final OutputStream mOut;
-
-        CopyStreamTask(String name, InputStream in, OutputStream out) {
-            mName = name;
-            mIn = in;
-            mOut = out;
-        }
-
-        @Override
-        public void run() {
-            try {
-                byte[] buffer = new byte[2048];
-                while (!Thread.interrupted()) {
-                    int len = mIn.read(buffer);
-                    if (len < 0) {
-                        break;
-                    }
-                    mOut.write(buffer, 0, len);
-                }
-            } catch (Exception e) {
-                Log.e(TAG, "Exception while posting " + mName, e);
-            }
-        }
+        // TODO: find out why javac complains when the return type of this method is void. It
+        // (incorrectly?) thinks that IOException should be caught inside the lambda.
+        return true;
     }
 
     private static class LineBufferedOutputStream extends BufferedOutputStream {
diff --git a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
index 0dd4ad2..bf032d3 100644
--- a/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/android/VmLauncherApp/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -52,7 +52,6 @@
     // TODO: this path should be from outside of this activity
     private static final String VM_CONFIG_PATH = "/data/local/tmp/vm_config.json";
 
-    private static final boolean DEBUG = true;
     private static final int RECORD_AUDIO_PERMISSION_REQUEST_CODE = 101;
 
     private static final String ACTION_VM_LAUNCHER = "android.virtualization.VM_LAUNCHER";
@@ -115,9 +114,9 @@
 
         mDisplayProvider = new DisplayProvider(surfaceView, cursorSurfaceView);
 
-        if (DEBUG) {
+        if (config.getDebugLevel() == VirtualMachineConfig.DEBUG_LEVEL_FULL) {
             Path logPath = getFileStreamPath("console.log").toPath();
-            new Logger(mVirtualMachine, logPath, mExecutorService);
+            Logger.setup(mVirtualMachine, logPath, mExecutorService);
         }
     }