JAL-3384 -out can now be used without getdown launch. -out cannot use '../' in pathname
[jalview.git] / src / jalview / bin / Jalview.java
index 45bb8d1..0fc92f9 100755 (executable)
@@ -199,32 +199,47 @@ public class Jalview
   {
     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)
+    if (appdirString != null || logFilename != null)
     {
-      String logFilename = "output.log";
-      File logFile = new File(appdirString, logFilename);
-      if (logFile.exists())
+
+      if (logFilename == null
+              || logFilename.indexOf(".." + File.separator) > -1)
       {
-        logFile.delete();
+        logFilename = "output.log";
       }
-      try
-      {
-        PrintStream logOut = new PrintStream(new FileOutputStream(logFile),
-                true);
-        System.setOut(logOut);
-        System.setErr(logOut);
 
-      } catch (IOException ioe)
+      String parent = logFilename.startsWith(File.separator) ? null
+              : appdirString;
+
+      File logFile = new File(parent, logFilename);
+
+      if (logFile.exists())
       {
-        System.err.println("Failed to open debug log [path="
-                + logFile.getAbsolutePath() + ", exception=" + ioe + "]");
+        logFile.delete();
       }
-      // if we've redirected our log output, note where to
+
       if (logFile != null)
       {
-        System.out.println("Logging to '" + logFile + "'.");
+        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() + "'.");
       }
 
     }
@@ -237,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"))