JAL-629 Move more stdout messages to stderr when outputting file to stdout
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 9 Aug 2023 23:23:50 +0000 (00:23 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 9 Aug 2023 23:23:50 +0000 (00:23 +0100)
src/jalview/bin/Commands.java
src/jalview/bin/Console.java
src/jalview/bin/Jalview.java
src/jalview/bin/Launcher.java
src/jalview/bin/argparser/ArgParser.java
src/jalview/bin/argparser/BootstrapArgs.java
src/jalview/util/LaunchUtils.java

index c08bcc5..17a1af2 100644 (file)
@@ -862,7 +862,7 @@ public class Commands
         String val = av.getValue();
         SubVals subVals = av.getSubVals();
         String fileName = subVals.getContent();
-        boolean stdout = fileName.equals("-");
+        boolean stdout = ArgParser.STDOUTFILENAME.equals(fileName);
         File file = new File(fileName);
         boolean overwrite = ArgParser.getFromSubValArgOrPref(avm,
                 Arg.OVERWRITE, subVals, null, "OVERWRITE_OUTPUT", false);
index 30fd530..7ac793d 100644 (file)
@@ -43,7 +43,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
       t.printStackTrace();
     }
 
@@ -57,7 +57,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
     }
 
   }
@@ -70,7 +70,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
       t.printStackTrace();
     }
   }
@@ -83,7 +83,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
     }
 
   }
@@ -96,7 +96,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
       t.printStackTrace();
     }
 
@@ -110,7 +110,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
     }
 
   }
@@ -123,7 +123,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
     }
   }
 
@@ -135,7 +135,7 @@ public class Console
     }
     else
     {
-      System.out.println(message);
+      outputMessage(message);
       t.printStackTrace();
     }
 
@@ -306,6 +306,21 @@ public class Console
     }
   }
 
+  public static void outputMessage(String message)
+  {
+    // send message to stderr if output to stdout is expected
+    if (Jalview.getInstance() != null
+            && Jalview.getInstance().bootstrapArgs != null
+            && Jalview.getInstance().bootstrapArgs.outputToStdout())
+    {
+      System.err.println(message);
+    }
+    else
+    {
+      System.out.println(message);
+    }
+  }
+
   public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";
 
 }
index 57f2575..e2b5bfb 100755 (executable)
@@ -274,7 +274,7 @@ public class Jalview
      * 
      */
     {
-      System.out.println("not in js");
+      Console.outputMessage("not in js");
     }
 
     // BH - for event debugging in JavaScript (Java mode only)
@@ -383,29 +383,31 @@ public class Jalview
       }.start();
     }
 
-    if (!quiet() || bootstrapArgs.contains(Arg.VERSION))
+    if (!quiet() || !bootstrapArgs.outputToStdout()
+            || bootstrapArgs.contains(Arg.VERSION))
     {
-      System.out.println(
+      Console.outputMessage(
               "Java version: " + System.getProperty("java.version"));
-      System.out.println("Java home: " + System.getProperty("java.home"));
-      System.out.println("Java arch: " + System.getProperty("os.arch") + " "
-              + System.getProperty("os.name") + " "
+      Console.outputMessage(
+              "Java home: " + System.getProperty("java.home"));
+      Console.outputMessage("Java arch: " + System.getProperty("os.arch")
+              + " " + System.getProperty("os.name") + " "
               + System.getProperty("os.version"));
 
       String val = System.getProperty("sys.install4jVersion");
       if (val != null)
       {
-        System.out.println("Install4j version: " + val);
+        Console.outputMessage("Install4j version: " + val);
       }
       val = System.getProperty("installer_template_version");
       if (val != null)
       {
-        System.out.println("Install4j template version: " + val);
+        Console.outputMessage("Install4j template version: " + val);
       }
       val = System.getProperty("launcher_version");
       if (val != null)
       {
-        System.out.println("Launcher version: " + val);
+        Console.outputMessage("Launcher version: " + val);
       }
     }
 
@@ -463,6 +465,7 @@ public class Jalview
     // register SIGTERM listener
     Runtime.getRuntime().addShutdownHook(new Thread()
     {
+      @Override
       public void run()
       {
         Console.debug("Running shutdown hook");
@@ -509,7 +512,7 @@ public class Jalview
     Cache.loadProperties(usrPropsFile);
     if (usrPropsFile != null)
     {
-      System.out.println(
+      Console.outputMessage(
               "CMD [-props " + usrPropsFile + "] executed successfully!");
       testoutput(bootstrapArgs, Arg.PROPS,
               "test/jalview/bin/testProps.jvprops", usrPropsFile);
@@ -541,8 +544,8 @@ public class Jalview
       {
         List<Map.Entry<Type, String>> helpArgs = bootstrapArgs
                 .getList(Arg.HELP);
-        System.out.println(Arg.usage(helpArgs.stream().map(e -> e.getKey())
-                .collect(Collectors.toList())));
+        Console.outputMessage(Arg.usage(helpArgs.stream()
+                .map(e -> e.getKey()).collect(Collectors.toList())));
         Jalview.exit(null, 0);
       }
       if (aparser.contains("help") || aparser.contains("h"))
@@ -551,7 +554,7 @@ public class Jalview
          * Now using new usage statement.
         showUsage();
         */
-        System.out.println(Arg.usage());
+        Console.outputMessage(Arg.usage());
         Jalview.exit(null, 0);
       }
 
@@ -581,7 +584,7 @@ public class Jalview
         try
         {
           Jws2Discoverer.getDiscoverer().setPreferredUrl(jabawsUrl);
-          System.out.println(
+          Console.outputMessage(
                   "CMD [-jabaws " + jabawsUrl + "] executed successfully!");
           testoutput(bootstrapArgs, Arg.JABAWS,
                   "http://www.compbio.dundee.ac.uk/jabaws", jabawsUrl);
@@ -617,7 +620,7 @@ public class Jalview
       }
       else
       {
-        System.out.println("Executing setprop argument: " + setprop);
+        System.err.println("Executing setprop argument: " + setprop);
         if (Platform.isJS())
         {
           Cache.setProperty(setprop.substring(0, p),
@@ -749,7 +752,8 @@ public class Jalview
         }
         else
         {
-          System.out.println("CMD [-nousagestats] executed successfully!");
+          Console.outputMessage(
+                  "CMD [-nousagestats] executed successfully!");
           testoutput(argparser, Arg.NOUSAGESTATS);
         }
 
@@ -765,7 +769,7 @@ public class Jalview
             // questionnaire
             Console.debug("Starting questionnaire url at " + url);
             desktop.checkForQuestionnaire(url);
-            System.out.println("CMD questionnaire[-" + url
+            Console.outputMessage("CMD questionnaire[-" + url
                     + "] executed successfully!");
           }
           else
@@ -786,8 +790,8 @@ public class Jalview
         }
         else
         {
-          System.out
-                  .println("CMD [-noquestionnaire] executed successfully!");
+          Console.outputMessage(
+                  "CMD [-noquestionnaire] executed successfully!");
           testoutput(argparser, Arg.QUESTIONNAIRE);
         }
 
@@ -877,7 +881,8 @@ public class Jalview
                         .getString("status.processing_commandline_args"),
                 progress = System.currentTimeMillis());
       }
-      System.out.println("CMD [-open " + file + "] executed successfully!");
+      Console.outputMessage(
+              "CMD [-open " + file + "] executed successfully!");
 
       if (!Platform.isJS())
       /**
@@ -914,7 +919,7 @@ public class Jalview
               format);
       if (af == null)
       {
-        System.out.println("error");
+        Console.outputMessage("error");
       }
       else
       {
@@ -929,7 +934,7 @@ public class Jalview
 
           if (cs != null)
           {
-            System.out.println(
+            Console.outputMessage(
                     "CMD [-colour " + data + "] executed successfully!");
           }
           af.changeColour(cs);
@@ -941,8 +946,8 @@ public class Jalview
         {
           af.parseFeaturesFile(data,
                   AppletFormatAdapter.checkProtocol(data));
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outputMessage("Added " + data);
+          Console.outputMessage(
                   "CMD groups[-" + data + "]  executed successfully!");
         }
         data = aparser.getValue("features", true);
@@ -950,8 +955,8 @@ public class Jalview
         {
           af.parseFeaturesFile(data,
                   AppletFormatAdapter.checkProtocol(data));
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outputMessage("Added " + data);
+          Console.outputMessage(
                   "CMD [-features " + data + "]  executed successfully!");
         }
 
@@ -959,8 +964,8 @@ public class Jalview
         if (data != null)
         {
           af.loadJalviewDataFile(data, null, null, null);
-          // System.out.println("Added " + data);
-          System.out.println(
+          // Console.outputMessage("Added " + data);
+          Console.outputMessage(
                   "CMD [-annotations " + data + "] executed successfully!");
         }
         // set or clear the sortbytree flag.
@@ -969,7 +974,8 @@ public class Jalview
           af.getViewport().setSortByTree(true);
           if (af.getViewport().getSortByTree())
           {
-            System.out.println("CMD [-sortbytree] executed successfully!");
+            Console.outputMessage(
+                    "CMD [-sortbytree] executed successfully!");
           }
         }
         if (aparser.contains("no-annotation"))
@@ -977,7 +983,8 @@ public class Jalview
           af.getViewport().setShowAnnotation(false);
           if (!af.getViewport().isShowAnnotation())
           {
-            System.out.println("CMD no-annotation executed successfully!");
+            Console.outputMessage(
+                    "CMD no-annotation executed successfully!");
           }
         }
         if (aparser.contains("nosortbytree"))
@@ -985,8 +992,8 @@ public class Jalview
           af.getViewport().setSortByTree(false);
           if (!af.getViewport().getSortByTree())
           {
-            System.out
-                    .println("CMD [-nosortbytree] executed successfully!");
+            Console.outputMessage(
+                    "CMD [-nosortbytree] executed successfully!");
           }
         }
         data = aparser.getValue("tree", true);
@@ -994,7 +1001,7 @@ public class Jalview
         {
           try
           {
-            System.out.println(
+            Console.outputMessage(
                     "CMD [-tree " + data + "] executed successfully!");
             NewickFile nf = new NewickFile(data,
                     AppletFormatAdapter.checkProtocol(data));
@@ -1011,9 +1018,9 @@ public class Jalview
         {
           // Execute the groovy script after we've done all the rendering stuff
           // and before any images or figures are generated.
-          System.out.println("Executing script " + groovyscript);
+          Console.outputMessage("Executing script " + groovyscript);
           executeGroovyScript(groovyscript, af);
-          System.out.println("CMD groovy[" + groovyscript
+          Console.outputMessage("CMD groovy[" + groovyscript
                   + "] executed successfully!");
           groovyscript = null;
         }
@@ -1027,14 +1034,14 @@ public class Jalview
 
             if (outputFormat.equalsIgnoreCase("png"))
             {
-              System.out.println("Creating PNG image: " + file);
+              Console.outputMessage("Creating PNG image: " + file);
               af.createPNG(new File(file));
               imageName = (new File(file)).getName();
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("svg"))
             {
-              System.out.println("Creating SVG image: " + file);
+              Console.outputMessage("Creating SVG image: " + file);
               File imageFile = new File(file);
               imageName = imageFile.getName();
               af.createSVG(imageFile);
@@ -1046,7 +1053,7 @@ public class Jalview
               imageName = imageFile.getName();
               HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel);
 
-              System.out.println("Creating HTML image: " + file);
+              Console.outputMessage("Creating HTML image: " + file);
               htmlSVG.exportHTML(file);
               continue;
             }
@@ -1066,21 +1073,21 @@ public class Jalview
                 e.printStackTrace();
               }
               BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel);
-              System.out.println(
+              Console.outputMessage(
                       "Creating BioJS MSA Viwer HTML file: " + file);
               bjs.exportHTML(file);
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("imgMap"))
             {
-              System.out.println("Creating image map: " + file);
+              Console.outputMessage("Creating image map: " + file);
               af.createImageMap(new File(file), imageName);
               continue;
             }
             else if (outputFormat.equalsIgnoreCase("eps"))
             {
               File outputFile = new File(file);
-              System.out.println(
+              Console.outputMessage(
                       "Creating EPS file: " + outputFile.getAbsolutePath());
               af.createEPS(outputFile);
               continue;
@@ -1092,14 +1099,14 @@ public class Jalview
               outFormat = FileFormats.getInstance().forName(outputFormat);
             } catch (Exception formatP)
             {
-              System.out.println("Couldn't parse " + outFormat
+              Console.outputMessage("Couldn't parse " + outFormat
                       + " as a valid Jalview format string.");
             }
             if (outFormat != null)
             {
               if (!outFormat.isWritable())
               {
-                System.out.println(
+                Console.outputMessage(
                         "This version of Jalview does not support alignment export as "
                                 + outputFormat);
               }
@@ -1108,19 +1115,19 @@ public class Jalview
                 af.saveAlignment(file, outFormat);
                 if (af.isSaveAlignmentSuccessful())
                 {
-                  System.out.println("Written alignment in "
+                  Console.outputMessage("Written alignment in "
                           + outFormat.getName() + " format to " + file);
                 }
                 else
                 {
-                  System.out.println("Error writing file " + file + " in "
-                          + outFormat.getName() + " format!!");
+                  Console.outputMessage("Error writing file " + file
+                          + " in " + outFormat.getName() + " format!!");
                 }
               }
             }
           } catch (ImageOutputException ioexc)
           {
-            System.out.println(
+            Console.outputMessage(
                     "Unexpected error whilst exporting image to " + file);
             ioexc.printStackTrace();
           }
@@ -1129,7 +1136,7 @@ public class Jalview
 
         while (aparser.getSize() > 0)
         {
-          System.out.println("Unknown arg: " + aparser.nextValue());
+          Console.outputMessage("Unknown arg: " + aparser.nextValue());
         }
       }
     }
@@ -1197,7 +1204,7 @@ public class Jalview
     {
       if (Cache.groovyJarsPresent())
       {
-        System.out.println("Executing script " + groovyscript);
+        Console.outputMessage("Executing script " + groovyscript);
         executeGroovyScript(groovyscript, startUpAlframe);
       }
       else
@@ -1786,7 +1793,7 @@ public class Jalview
       {
         if (exitcode == 0)
         {
-          System.out.println(message);
+          Console.outputMessage(message);
         }
         else
         {
@@ -1882,7 +1889,7 @@ public class Jalview
     if (yes && ((s1 == null && s2 == null)
             || (s1 != null && s1.equals(s2))))
     {
-      System.out.println("[TESTOUTPUT] arg " + a.argString() + "='" + s1
+      Console.outputMessage("[TESTOUTPUT] arg " + a.argString() + "='" + s1
               + "' was set");
     }
   }
@@ -1940,6 +1947,6 @@ public class Jalview
     {
       message = a.argString() + (yes ? " was set" : " was not set");
     }
-    System.out.println("[TESTOUTPUT] arg " + message);
+    Console.outputMessage("[TESTOUTPUT] arg " + message);
   }
 }
index a87d322..6e820fd 100644 (file)
@@ -127,11 +127,13 @@ public class Launcher
     boolean debug = false;
     boolean wait = true;
     boolean quiet = false;
+    boolean stdout = false;
     // must set --debug before --launcher...
     boolean launcherstop = false;
     boolean launcherprint = false;
     boolean launcherwait = false;
     ArrayList<String> arguments = new ArrayList<>();
+    String previousArg = null;
     for (String arg : args)
     {
       if (arg.equals("--debug"))
@@ -142,6 +144,11 @@ public class Launcher
       {
         quiet = true;
       }
+      if (arg.equals("--output=-")
+              || (arg.equals("-") && "--output".equals(previousArg)))
+      {
+        stdout = true;
+      }
       if (debug && arg.equals("--launcherprint"))
       {
         launcherprint = true;
@@ -159,6 +166,7 @@ public class Launcher
       {
         wait = false;
       }
+      previousArg = arg;
       // Don't add the --launcher... args to Jalview launch
       if (arg.startsWith("--launcher"))
       {
@@ -280,7 +288,7 @@ public class Launcher
     String scalePropertyArg = HiDPISetting.getScalePropertyArg();
     if (scalePropertyArg != null)
     {
-      sysout(debug, quiet, "Running " + startClass + " with scale setting "
+      syserr(debug, quiet, "Running " + startClass + " with scale setting "
               + scalePropertyArg);
       command.add(scalePropertyArg);
     }
@@ -293,10 +301,10 @@ public class Launcher
     if ((Boolean.parseBoolean(System.getProperty("launcherprint", "false"))
             || launcherprint))
     {
-      sysout(debug, quiet,
+      syserr(debug, quiet,
               "LAUNCHER COMMAND: " + String.join(" ", builder.command()));
     }
-    sysout(debug, quiet,
+    syserr(debug, quiet,
             "Running " + startClass + " with "
                     + (memSetting == null ? "no memory setting"
                             : ("memory setting " + memSetting)));
@@ -304,7 +312,7 @@ public class Launcher
     if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))
             || (debug && launcherstop))
     {
-      sysout(debug, quiet,
+      syserr(debug, quiet,
               "System property 'launcherstop' is set and not 'false'. Exiting.");
       System.exit(0);
     }
@@ -314,17 +322,17 @@ public class Launcher
       Process process = builder.start();
       if (wait || launcherwait)
       {
-        sysout(debug, quiet, "Launching application process");
+        syserr(debug, quiet, "Launching application process");
         process.waitFor();
       }
       else
       {
         int waitInt = 0;
-        sysout(debug, quiet,
+        syserr(debug, quiet,
                 "Wait time for application process is " + waitInt + "ms");
         process.waitFor(waitInt, TimeUnit.MILLISECONDS);
       }
-      sysout(debug, quiet, "Launcher process ending");
+      syserr(debug, quiet, "Launcher process ending");
     } catch (IOException e)
     {
       if (e.getMessage().toLowerCase(Locale.ROOT).contains("memory"))
@@ -364,11 +372,11 @@ public class Launcher
     }
   }
 
-  private static void sysout(boolean debug, boolean quiet, String message)
+  private static void syserr(boolean debug, boolean quiet, String message)
   {
     if (debug && !quiet)
     {
-      System.out.println("LAUNCHERDEBUG - " + message);
+      System.err.println("LAUNCHERDEBUG - " + message);
     }
   }
 
index 907b1fa..9c66f32 100644 (file)
@@ -50,6 +50,8 @@ public class ArgParser
 
   public static final char EQUALS = '=';
 
+  public static final String STDOUTFILENAME = "-";
+
   protected static final String NEGATESTRING = "no";
 
   /**
index 4b7b180..a29f7b8 100644 (file)
@@ -28,6 +28,8 @@ public class BootstrapArgs
 
   private Set<Type> argsTypes = new HashSet<>();
 
+  private boolean outputToStdout = false;
+
   public static BootstrapArgs getBootstrapArgs(String[] args)
   {
     List<String> argList = new ArrayList<>(Arrays.asList(args));
@@ -127,11 +129,6 @@ public class BootstrapArgs
           }
         }
 
-        if (ArgParser.argMap.containsKey(argName) && val == null)
-        {
-          val = "true";
-        }
-
         Arg a = ArgParser.argMap.get(argName);
 
         if (a != null)
@@ -152,7 +149,24 @@ public class BootstrapArgs
 
         if (a == null || !a.hasOption(Opt.BOOTSTRAP))
         {
-          // not a valid bootstrap arg
+          // not a bootstrap arg
+
+          // make a check for an output going to stdout
+          if (a != null && a.hasOption(Opt.OUTPUTFILE))
+          {
+            System.err.println("###### Found an output");
+            System.err.println("######   val='" + val + "'");
+            System.err
+                    .println("######   next arg='" + args.get(i + 1) + "'");
+            if ((val == null && i + 1 < args.size()
+                    && ArgParser.STDOUTFILENAME.equals(args.get(i + 1)))
+                    || ArgParser.STDOUTFILENAME.equals(val))
+            {
+              this.outputToStdout = true;
+              System.err.println("###### Expecting output to stdout");
+            }
+          }
+
           continue;
         }
 
@@ -188,6 +202,11 @@ public class BootstrapArgs
         }
         else
         {
+          if (val == null)
+          {
+            val = "true";
+          }
+
           add(a, type, val);
         }
       }
@@ -373,4 +392,9 @@ public class BootstrapArgs
     }
     return isHeadless;
   }
+
+  public boolean outputToStdout()
+  {
+    return this.outputToStdout;
+  }
 }
index 5bd4a08..eb43363 100644 (file)
@@ -29,6 +29,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Properties;
 
+import jalview.bin.Console;
+
 public class LaunchUtils
 {
 
@@ -103,7 +105,7 @@ public class LaunchUtils
               null);
       if (JCV == null)
       {
-        System.out.println(
+        Console.outputMessage(
                 "Could not obtain JAVA_COMPILE_VERSION for comparison");
         return -2;
       }
@@ -142,7 +144,8 @@ public class LaunchUtils
       String JV = System.getProperty("java.version");
       if (JV == null)
       {
-        System.out.println("Could not obtain java.version for comparison");
+        Console.outputMessage(
+                "Could not obtain java.version for comparison");
         return -2;
       }
       if (JV.startsWith("1."))
@@ -174,7 +177,7 @@ public class LaunchUtils
 
     if (java_compile_version <= 0 || java_version <= 0)
     {
-      System.out.println("Could not make Java version check");
+      Console.outputMessage("Could not make Java version check");
       return true;
     }
     // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist