JAL-3384 Redirect stdout and stderr to output.log file
authorBen Soares <bsoares@dundee.ac.uk>
Mon, 29 Jul 2019 15:39:36 +0000 (16:39 +0100)
committerBen Soares <bsoares@dundee.ac.uk>
Mon, 29 Jul 2019 15:39:36 +0000 (16:39 +0100)
src/jalview/bin/Jalview.java

index ca2a584..45bb8d1 100755 (executable)
@@ -47,6 +47,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -197,6 +198,37 @@ public class Jalview
   void doMain(String[] args)
   {
     System.setSecurityManager(null);
+
+    // set stdout and stderr to file if launched by getdown
+    String appdirString = System.getProperty("getdownappdir");
+    if (appdirString != null)
+    {
+      String logFilename = "output.log";
+      File logFile = new File(appdirString, logFilename);
+      if (logFile.exists())
+      {
+        logFile.delete();
+      }
+      try
+      {
+        PrintStream logOut = new PrintStream(new FileOutputStream(logFile),
+                true);
+        System.setOut(logOut);
+        System.setErr(logOut);
+
+      } catch (IOException ioe)
+      {
+        System.err.println("Failed to open debug log [path="
+                + logFile.getAbsolutePath() + ", exception=" + ioe + "]");
+      }
+      // if we've redirected our log output, note where to
+      if (logFile != null)
+      {
+        System.out.println("Logging to '" + logFile + "'.");
+      }
+
+    }
+
     System.out
             .println("Java version: " + System.getProperty("java.version"));
     System.out.println(System.getProperty("os.arch") + " "
@@ -408,7 +440,7 @@ public class Jalview
 
     // Move any new getdown-launcher-new.jar into place over old
     // getdown-launcher.jar
-    String appdirString = System.getProperty("getdownappdir");
+    // String appdirString = System.getProperty("getdownappdir");
     if (appdirString != null && appdirString.length() > 0)
     {
       final File appdir = new File(appdirString);