JAL-3628 Even more Cache.log.debug statements
[jalview.git] / src / jalview / io / BackupFiles.java
index d18dc35..4f86f3d 100644 (file)
@@ -36,6 +36,7 @@ import jalview.bin.Cache;
 import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 
 /*
  * BackupFiles used for manipulating (naming rolling/deleting) backup/version files when an alignment or project file is saved.
@@ -130,30 +131,47 @@ public class BackupFiles
       {
         String tempfilename = file.getName();
         File tempdir = file.getParentFile();
+        Cache.log.debug(
+                "BACKUPFILES [file!=null] attempting to create temp file for "
+                        + tempfilename + " in dir " + tempdir);
         temp = File.createTempFile(tempfilename,
                 TEMP_FILE_EXT + newTempFileSuffix, tempdir);
+        Cache.log.debug(
+                "BACKUPFILES using temp file " + temp.getAbsolutePath());
       }
       else
       {
+        Cache.log.debug(
+                "BACKUPFILES [file==null] attempting to create default temp file "
+                        + DEFAULT_TEMP_FILE + " with extension "
+                        + TEMP_FILE_EXT);
         temp = File.createTempFile(DEFAULT_TEMP_FILE, TEMP_FILE_EXT);
       }
     } catch (IOException e)
     {
-      Cache.log.error(
-              "Could not create temp file to save into (IOException)");
+      Cache.log
+              .error("Could not create temp file to save to (IOException)");
+      Cache.log.error(e.getMessage());
+      Cache.log.debug(e.getStackTrace());
     } catch (Exception e)
     {
       Cache.log.error("Exception ctreating temp file for saving");
+      Cache.log.debug(e.getStackTrace());
     }
     this.setTempFile(temp);
   }
 
   public static void classInit()
   {
-    setEnabled(Cache.getDefault(ENABLED, true));
+    Cache.log.debug("BACKUPFILES classInit");
+    boolean e = Cache.getDefault(ENABLED, true);
+    setEnabled(e);
+    Cache.log.debug("BACKUPFILES " + (e ? "enabled" : "disabled"));
     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
             .getSavedBackupEntry();
+    Cache.log.debug("BACKUPFILES preset scheme " + bfpe.toString());
     setConfirmDelete(bfpe.confirmDelete);
+    Cache.log.debug("BACKUPFILES confirm delete " + bfpe.confirmDelete);
   }
 
   public static void setEnabled(boolean flag)
@@ -200,6 +218,7 @@ public class BackupFiles
       Cache.log.error(
               "IOException when getting Canonical Path of temp file '"
                       + this.getTempFile().getName() + "'");
+      Cache.log.debug(e.getStackTrace());
     }
     return path;
   }
@@ -243,22 +262,27 @@ public class BackupFiles
       return true;
     }
 
+    Cache.log.debug("BACKUPFILES rollBackupFiles starting");
+
     String dir = "";
     File dirFile;
     try
     {
       dirFile = file.getParentFile();
       dir = dirFile.getCanonicalPath();
-      Cache.log.debug("BACKUPFILES: dir: " + dir);
+      Cache.log.debug("BACKUPFILES dir: " + dir);
     } catch (Exception e)
     {
       Cache.log.error(
               "Could not get canonical path for file '" + file + "'");
+      Cache.log.error(e.getMessage());
+      Cache.log.debug(e.getStackTrace());
       return false;
     }
     String filename = file.getName();
     String basename = filename;
 
+    Cache.log.debug("BACKUPFILES filename is " + filename);
     boolean ret = true;
     // Create/move backups up one
 
@@ -275,6 +299,8 @@ public class BackupFiles
     if (backupFiles.length == 0)
     {
       // No other backup files. Just need to move existing file to backupfile_1
+      Cache.log.debug(
+              "BACKUPFILES no existing backup files, setting index to 1");
       nextIndexNum = 1;
     }
     else
@@ -287,6 +313,7 @@ public class BackupFiles
       if (reverseOrder)
       {
         // backup style numbering
+        Cache.log.debug("BACKUPFILES rolling files in reverse order");
 
         int tempMax = noMax ? -1 : max;
         // noMax == true means no limits
@@ -318,6 +345,7 @@ public class BackupFiles
             // no "oldest" file to delete
             previousFile = backupfile_n;
             fileToBeDeleted = null;
+            Cache.log.debug("BACKUPFILES No oldest file to delete");
             continue;
           }
 
@@ -328,6 +356,10 @@ public class BackupFiles
             File replacementFile = backupfile_n;
             long fileToBeDeletedLMT = fileToBeDeleted.lastModified();
             long replacementFileLMT = replacementFile.lastModified();
+            Cache.log.debug("BACKUPFILES fileToBeDeleted is "
+                    + fileToBeDeleted.getAbsolutePath());
+            Cache.log.debug("BACKUPFILES replacementFile is "
+                    + backupfile_n.getAbsolutePath());
 
             try
             {
@@ -351,6 +383,11 @@ public class BackupFiles
 
                 boolean delete = confirmNewerDeleteFile(fileToBeDeleted,
                         replacementFile, true);
+                Cache.log.debug("BACKUPFILES "
+                        + (delete ? "confirmed" : "not") + " deleting file "
+                        + fileToBeDeleted.getAbsolutePath()
+                        + " which is newer than "
+                        + replacementFile.getAbsolutePath());
 
                 if (delete)
                 {
@@ -359,11 +396,17 @@ public class BackupFiles
                 }
                 else
                 {
+                  Cache.log.debug("BACKUPFILES moving "
+                          + fileToBeDeleted.getAbsolutePath() + " to "
+                          + oldestTempFile.getAbsolutePath());
                   moveFileToFile(fileToBeDeleted, oldestTempFile);
                 }
               }
               else
               {
+                Cache.log.debug("BACKUPFILES going to move "
+                        + fileToBeDeleted.getAbsolutePath() + " to "
+                        + oldestTempFile.getAbsolutePath());
                 moveFileToFile(fileToBeDeleted, oldestTempFile);
                 addDeleteFile(oldestTempFile);
               }
@@ -488,7 +531,7 @@ public class BackupFiles
             if (delete)
             {
               addDeleteFile(fileToBeDeleted);
-              Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDelted): "
+              Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): "
                       + fileToBeDeleted);
             }
 
@@ -507,9 +550,9 @@ public class BackupFiles
     Cache.log.debug("BACKUPFILES Moving old file [" + file
             + "] to latestBackupFilename [" + latestBackupFilename + "]");
     ret |= moveFileToFile(file, new File(latestBackupFilename));
-    Cache.log.debug("BACKUPFILES moving " + latestBackupFilename + " to "
-            + file + " was " + (ret ? "" : "NOT ") + "successful");
-
+    Cache.log.debug(
+            "BACKUPFILES moving " + file + " to " + latestBackupFilename
+                    + " was " + (ret ? "" : "NOT ") + "successful");
     if (tidyUp)
     {
       Cache.log.debug("BACKUPFILES tidying up files");
@@ -588,12 +631,15 @@ public class BackupFiles
           MessageManager.getString("label.delete"),
           MessageManager.getString("label.rename") };
 
-      confirmButton = JvOptionPane.showOptionDialog(Desktop.desktop,
-              messageSB.toString(),
-              MessageManager.getString("label.backupfiles_confirm_delete"),
-              // "Confirm delete"
-              JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE,
-              null, options, options[0]);
+      confirmButton = Platform.isHeadless() ? JvOptionPane.YES_OPTION
+              : JvOptionPane.showOptionDialog(Desktop.desktop,
+                      messageSB.toString(),
+                      MessageManager.getString(
+                              "label.backupfiles_confirm_delete"),
+                      // "Confirm delete"
+                      JvOptionPane.YES_NO_OPTION,
+                      JvOptionPane.WARNING_MESSAGE, null, options,
+                      options[0]);
     }
     else
     {
@@ -613,12 +659,15 @@ public class BackupFiles
           MessageManager.getString("label.delete"),
           MessageManager.getString("label.keep") };
 
-      confirmButton = JvOptionPane.showOptionDialog(Desktop.desktop,
-              messageSB.toString(),
-              MessageManager.getString("label.backupfiles_confirm_delete"),
-              // "Confirm delete"
-              JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE,
-              null, options, options[0]);
+      confirmButton = Platform.isHeadless() ? JvOptionPane.YES_OPTION
+              : JvOptionPane.showOptionDialog(Desktop.desktop,
+                      messageSB.toString(),
+                      MessageManager.getString(
+                              "label.backupfiles_confirm_delete"),
+                      // "Confirm delete"
+                      JvOptionPane.YES_NO_OPTION,
+                      JvOptionPane.WARNING_MESSAGE, null, options,
+                      options[0]);
     }
 
     // return should be TRUE if file is to be deleted
@@ -651,12 +700,14 @@ public class BackupFiles
           // "(modified {0}, size {1})"
         }
 
-        int confirmButton = JvOptionPane.showConfirmDialog(Desktop.desktop,
-                messageSB.toString(),
-                MessageManager
-                        .getString("label.backupfiles_confirm_delete"),
-                // "Confirm delete"
-                JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE);
+        int confirmButton = Platform.isHeadless() ? JvOptionPane.YES_OPTION
+                : JvOptionPane.showConfirmDialog(Desktop.desktop,
+                        messageSB.toString(),
+                        MessageManager.getString(
+                                "label.backupfiles_confirm_delete"),
+                        // "Confirm delete"
+                        JvOptionPane.YES_NO_OPTION,
+                        JvOptionPane.WARNING_MESSAGE);
 
         doDelete = (confirmButton == JvOptionPane.YES_OPTION);
       }
@@ -747,12 +798,13 @@ public class BackupFiles
         // "The new saved file might not be okay."
       }
 
-      int confirmButton = JvOptionPane.showConfirmDialog(Desktop.desktop,
-              messageSB.toString(),
-              MessageManager
-                      .getString("label.backupfiles_confirm_save_file"),
-              // "Confirm save file"
-              JvOptionPane.OK_OPTION, JvOptionPane.WARNING_MESSAGE);
+      int confirmButton = Platform.isHeadless() ? JvOptionPane.OK_OPTION
+              : JvOptionPane.showConfirmDialog(Desktop.desktop,
+                      messageSB.toString(),
+                      MessageManager.getString(
+                              "label.backupfiles_confirm_save_file"),
+                      // "Confirm save file"
+                      JvOptionPane.OK_OPTION, JvOptionPane.WARNING_MESSAGE);
       okay = confirmButton == JvOptionPane.OK_OPTION;
     }
     if (okay)
@@ -818,10 +870,15 @@ public class BackupFiles
     int pos = deleteFiles.indexOf(fileToBeDeleted);
     if (pos > -1)
     {
+      Cache.log.debug("BACKUPFILES not adding file "
+              + fileToBeDeleted.getAbsolutePath()
+              + " to the delete list (already at index" + pos + ")");
       return true;
     }
     else
     {
+      Cache.log.debug("BACKUPFILES adding file "
+              + fileToBeDeleted.getAbsolutePath() + " to the delete list");
       deleteFiles.add(fileToBeDeleted);
     }
     return ret;
@@ -835,19 +892,24 @@ public class BackupFiles
     try
     {
       // delete destination file - not usually necessary but Just In Case...
+      Cache.log.debug("BACKUPFILES deleting " + newFile.getAbsolutePath());
       newFile.delete();
+      Cache.log.debug("BACKUPFILES moving " + oldFile.getAbsolutePath()
+              + " to " + newFile.getAbsolutePath());
       Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
       ret = true;
+      Cache.log.debug("BACKUPFILES move seems to have succeeded");
     } catch (IOException e)
     {
       Cache.log.warn("Could not move file '" + oldPath.toString() + "' to '"
               + newPath.toString() + "'");
-      Cache.log.error(e.getStackTrace());
+      Cache.log.error(e.getMessage());
+      Cache.log.debug(e.getStackTrace());
       ret = false;
     } catch (Exception e)
     {
       Cache.log.error(e.getMessage());
-      Cache.log.error(e.getStackTrace());
+      Cache.log.debug(e.getStackTrace());
       ret = false;
     }
     return ret;