JAL-3384 -out can now be used without getdown launch. -out cannot use '../' in pathname
[jalview.git] / src / jalview / bin / Jalview.java
index ca2a584..0fc92f9 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,52 @@ public class Jalview
   void doMain(String[] args)
   {
     System.setSecurityManager(null);
+
+    ArgsParser aparser = new ArgsParser(args);
+    String logFilename = aparser.getValue("out");
+    // set stdout and stderr to file if launched by getdown
+    String appdirString = System.getProperty("getdownappdir");
+    if (appdirString != null || logFilename != null)
+    {
+
+      if (logFilename == null
+              || logFilename.indexOf(".." + File.separator) > -1)
+      {
+        logFilename = "output.log";
+      }
+
+      String parent = logFilename.startsWith(File.separator) ? null
+              : appdirString;
+
+      File logFile = new File(parent, logFilename);
+
+      if (logFile.exists())
+      {
+        logFile.delete();
+      }
+
+      if (logFile != null)
+      {
+        System.out.println("About to start logging to '"
+                + logFile.getAbsolutePath() + "'.");
+        try
+        {
+          PrintStream logOut = new PrintStream(
+                  new FileOutputStream(logFile), true);
+          System.setOut(logOut);
+          System.setErr(logOut);
+
+        } catch (IOException ioe)
+        {
+          System.err.println("Failed to open log file [path="
+                  + logFile.getAbsolutePath() + ", exception=" + ioe + "]");
+        }
+        System.out.println(
+                "Now logging to '" + logFile.getAbsolutePath() + "'.");
+      }
+
+    }
+
     System.out
             .println("Java version: " + System.getProperty("java.version"));
     System.out.println(System.getProperty("os.arch") + " "
@@ -205,7 +252,6 @@ public class Jalview
     // report Jalview version
     Cache.loadBuildProperties(true);
 
-    ArgsParser aparser = new ArgsParser(args);
     boolean headless = false;
 
     if (aparser.contains("help") || aparser.contains("h"))
@@ -408,7 +454,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);