JAL-4344 Some fixes of waiting blocks, and headless mode for export. Control logging...
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Nov 2023 16:21:08 +0000 (16:21 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Nov 2023 16:21:08 +0000 (16:21 +0000)
src/jalview/bin/Commands.java
src/jalview/bin/Console.java
src/jalview/gui/AppJmol.java

index 475eba2..6aab95b 100644 (file)
@@ -657,18 +657,19 @@ public class Commands
           }
           try
           {
-            long tries = 1000;
-            while (structureViewer.isBusy() && tries > 0)
+            int tries = 0;
+            int maxTries = 1000;
+            while (structureViewer.isBusy() && tries < maxTries)
             {
-              Thread.sleep(25);
-              if (structureViewer.isBusy())
+              if (tries % 100 == 0)
               {
-                tries--;
                 Console.debug(
                         "Waiting for viewer for " + structureFilepath);
               }
+              Thread.sleep(10);
+              tries++;
             }
-            if (tries == 0 && structureViewer.isBusy())
+            if (tries >= maxTries && structureViewer.isBusy())
             {
               addWarn("Gave up waiting for structure viewer to load file '"
                       + structureFile
@@ -823,6 +824,7 @@ public class Commands
                 // we can do this with every viewer
                 //
 
+                boolean doContinue = false;
                 try
                 {
                   // We don't expect class cast exception
@@ -847,11 +849,12 @@ public class Commands
                   addError(
                           "Unexpected error when restoring structure viewer session after custom view operations.");
                   isError = true;
-                  continue;
+                  doContinue = true;
                 } finally
                 {
                   try
                   {
+                    // restore colour scheme
                     this.colourAlignFrame(af, originalColourScheme);
                   } catch (Exception t)
                   {
@@ -859,6 +862,10 @@ public class Commands
                             "Unexpected error when restoring colourscheme to alignment after temporary change for export.",
                             t);
                   }
+                  if (doContinue)
+                  {
+                    continue;
+                  }
                 }
               }
             }
index 1bb8162..cd5683c 100644 (file)
@@ -264,6 +264,35 @@ public class Console
 
       // The main application logger
       log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
+
+      // Jmol logging. Reduce logging level for INFO.
+      int jmolLogLevel = org.jmol.util.Logger.LEVEL_INFO;
+      switch (logLevel)
+      {
+      case FATAL:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_FATAL;
+        break;
+      case ERROR:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
+        break;
+      case WARN:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
+        break;
+      case INFO:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
+        break;
+      case DEBUG:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_INFO;
+        break;
+      case TRACE:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_DEBUGHIGH;
+        break;
+      case ALL:
+        jmolLogLevel = org.jmol.util.Logger.LEVEL_MAX;
+        break;
+      }
+      org.jmol.util.Logger.setLogLevel(jmolLogLevel);
+      // org.jmol.util.Logger.setLogLevel(org.jmol.util.Logger.LEVEL_ERROR);
     } catch (NoClassDefFoundError e)
     {
       jalview.bin.Console
index 0aa4878..cb6aa13 100644 (file)
@@ -41,6 +41,7 @@ import javax.swing.event.InternalFrameEvent;
 
 import jalview.api.AlignmentViewPanel;
 import jalview.bin.Console;
+import jalview.bin.Jalview;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.StructureViewerModel;
@@ -453,7 +454,6 @@ public class AppJmol extends StructureViewerBase
     float usescale = bis.scale();
     int usewidth = bis.width();
     int useheight = bis.height();
-
     ImageWriterI writer = new ImageWriterI()
     {
       @Override
@@ -484,35 +484,38 @@ public class AppJmol extends StructureViewerBase
     final Throwable[] exceptions = new Throwable[1];
     exceptions[0] = null;
     final AppJmol us = this;
+    Runnable exportRunnable = () -> {
+      try
+      {
+        exporter.doExport(file, us, width, height, view, renderer, userBis);
+      } catch (Throwable t)
+      {
+        Console.debug("Problem when exporting structure image", t);
+        exceptions[0] = t;
+      }
+    };
     try
     {
-      Thread runner = Executors.defaultThreadFactory()
-              .newThread(new Runnable()
-              {
-                @Override
-                public void run()
-                {
-                  try
-                  {
-                    exporter.doExport(file, us, width, height, view,
-                            renderer, userBis);
-                  } catch (Throwable t)
-                  {
-                    exceptions[0] = t;
-                  }
-                }
-              });
-      runner.start();
-      long time = 0;
-      do
+      if (Jalview.isHeadlessMode())
       {
-        Thread.sleep(25);
-      } while (runner.isAlive() && time++ < 4000);
-      if (time >= 4000)
+        exportRunnable.run();
+      }
+      else
       {
-        runner.interrupt();
-        throw new ImageOutputException(
-                "Jmol took too long to export. Waited for 100 seconds.");
+        Thread runner = Executors.defaultThreadFactory()
+                .newThread(exportRunnable);
+        runner.start();
+        long time = 0;
+        do
+        {
+          Thread.sleep(25);
+        } while (runner.isAlive() && time++ < 4000);
+        if (time >= 4000)
+        {
+          runner.interrupt();
+          throw new ImageOutputException(
+                  "Jmol took too long to export. Waited for 100 seconds.");
+        }
       }
     } catch (Throwable e)
     {