X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=6e80578ea14e9ec1b0df1a0dc1fe8cb36052ad14;hb=9495cd9a452073db15529f4844dc9888a6847d00;hp=5594857866be98ab878411f4a5bf79255e0b9aa5;hpb=6c9f1f0278e44bdb3d777c0e4227c22fa09425bf;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 5594857..6e80578 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -131,17 +131,17 @@ public class BackupFiles { String tempfilename = file.getName(); File tempdir = file.getParentFile(); - Cache.log.debug( + Cache.trace( "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( + Cache.debug( "BACKUPFILES using temp file " + temp.getAbsolutePath()); } else { - Cache.log.debug( + Cache.trace( "BACKUPFILES [file==null] attempting to create default temp file " + DEFAULT_TEMP_FILE + " with extension " + TEMP_FILE_EXT); @@ -149,29 +149,29 @@ public class BackupFiles } } catch (IOException e) { - Cache.log - .error("Could not create temp file to save to (IOException)"); - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Cache.error("Could not create temp file to save to (IOException)"); + Cache.error(e.getMessage()); + Cache.debug(Cache.getStackTraceString(e)); } catch (Exception e) { - Cache.log.error("Exception creating temp file for saving"); - Cache.log.debug(e.getStackTrace()); + Cache.error("Exception creating temp file for saving"); + Cache.debug(Cache.getStackTraceString(e)); } this.setTempFile(temp); } public static void classInit() { - Cache.log.debug("BACKUPFILES classInit"); + Cache.initLogger(); + Cache.trace("BACKUPFILES classInit"); boolean e = Cache.getDefault(ENABLED, !Platform.isJS()); setEnabled(e); - Cache.log.debug("BACKUPFILES " + (e ? "enabled" : "disabled")); + Cache.trace("BACKUPFILES " + (e ? "enabled" : "disabled")); BackupFilesPresetEntry bfpe = BackupFilesPresetEntry .getSavedBackupEntry(); - Cache.log.debug("BACKUPFILES preset scheme " + bfpe.toString()); + Cache.trace("BACKUPFILES preset scheme " + bfpe.toString()); setConfirmDelete(bfpe.confirmDelete); - Cache.log.debug("BACKUPFILES confirm delete " + bfpe.confirmDelete); + Cache.trace("BACKUPFILES confirm delete " + bfpe.confirmDelete); } public static void setEnabled(boolean flag) @@ -215,10 +215,9 @@ public class BackupFiles path = this.getTempFile().getCanonicalPath(); } catch (IOException e) { - Cache.log.error( - "IOException when getting Canonical Path of temp file '" - + this.getTempFile().getName() + "'"); - Cache.log.debug(e.getStackTrace()); + Cache.error("IOException when getting Canonical Path of temp file '" + + this.getTempFile().getName() + "'"); + Cache.debug(Cache.getStackTraceString(e)); } return path; } @@ -254,7 +253,7 @@ public class BackupFiles || suffix.length() == 0) { // nothing to do - Cache.log.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + Cache.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + "filename: " + (file != null ? file.getName() : "null") + ", " + "file exists: " + file.exists() + ", " + "enabled: " + enabled + ", " + "max: " + max + ", " + "suffix: '" + suffix @@ -262,7 +261,7 @@ public class BackupFiles return true; } - Cache.log.debug("BACKUPFILES rollBackupFiles starting"); + Cache.trace("BACKUPFILES rollBackupFiles starting"); String dir = ""; File dirFile; @@ -270,19 +269,18 @@ public class BackupFiles { dirFile = file.getParentFile(); dir = dirFile.getCanonicalPath(); - Cache.log.debug("BACKUPFILES dir: " + dir); + Cache.trace("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()); + Cache.error("Could not get canonical path for file '" + file + "'"); + Cache.error(e.getMessage()); + Cache.debug(Cache.getStackTraceString(e)); return false; } String filename = file.getName(); String basename = filename; - Cache.log.debug("BACKUPFILES filename is " + filename); + Cache.trace("BACKUPFILES filename is " + filename); boolean ret = true; // Create/move backups up one @@ -294,12 +292,11 @@ public class BackupFiles File[] backupFiles = dirFile.listFiles(bff); int nextIndexNum = 0; - Cache.log - .debug("BACKUPFILES backupFiles.length: " + backupFiles.length); + Cache.trace("BACKUPFILES backupFiles.length: " + backupFiles.length); if (backupFiles.length == 0) { // No other backup files. Just need to move existing file to backupfile_1 - Cache.log.debug( + Cache.trace( "BACKUPFILES no existing backup files, setting index to 1"); nextIndexNum = 1; } @@ -313,7 +310,7 @@ public class BackupFiles if (reverseOrder) { // backup style numbering - Cache.log.debug("BACKUPFILES rolling files in reverse order"); + Cache.trace("BACKUPFILES rolling files in reverse order"); int tempMax = noMax ? -1 : max; // noMax == true means no limits @@ -345,7 +342,7 @@ public class BackupFiles // no "oldest" file to delete previousFile = backupfile_n; fileToBeDeleted = null; - Cache.log.debug("BACKUPFILES No oldest file to delete"); + Cache.trace("BACKUPFILES No oldest file to delete"); continue; } @@ -356,9 +353,9 @@ public class BackupFiles File replacementFile = backupfile_n; long fileToBeDeletedLMT = fileToBeDeleted.lastModified(); long replacementFileLMT = replacementFile.lastModified(); - Cache.log.debug("BACKUPFILES fileToBeDeleted is " + Cache.trace("BACKUPFILES fileToBeDeleted is " + fileToBeDeleted.getAbsolutePath()); - Cache.log.debug("BACKUPFILES replacementFile is " + Cache.trace("BACKUPFILES replacementFile is " + backupfile_n.getAbsolutePath()); try @@ -372,7 +369,7 @@ public class BackupFiles .format(fileToBeDeletedLMT); String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile " + Cache.warn("WARNING! I am set to delete backupfile " + fileToBeDeleted.getName() + " has modification time " + fileToBeDeletedLMTString @@ -383,7 +380,7 @@ public class BackupFiles boolean delete = confirmNewerDeleteFile(fileToBeDeleted, replacementFile, true); - Cache.log.debug("BACKUPFILES " + Cache.trace("BACKUPFILES " + (delete ? "confirmed" : "not") + " deleting file " + fileToBeDeleted.getAbsolutePath() + " which is newer than " @@ -396,7 +393,7 @@ public class BackupFiles } else { - Cache.log.debug("BACKUPFILES moving " + Cache.debug("BACKUPFILES moving " + fileToBeDeleted.getAbsolutePath() + " to " + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); @@ -404,7 +401,7 @@ public class BackupFiles } else { - Cache.log.debug("BACKUPFILES going to move " + Cache.debug("BACKUPFILES going to move " + fileToBeDeleted.getAbsolutePath() + " to " + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); @@ -413,10 +410,10 @@ public class BackupFiles } catch (Exception e) { - Cache.log.error( + Cache.error( "Error occurred, probably making new temp file for '" + fileToBeDeleted.getName() + "'"); - Cache.log.error(e.getStackTrace()); + Cache.error(Cache.getStackTraceString(e)); } // reset @@ -431,7 +428,9 @@ public class BackupFiles { if (previousFile != null) { - ret = ret && moveFileToFile(backupfile_n, previousFile); + // using boolean '&' instead of '&&' as don't want moveFileToFile + // attempt to be conditional (short-circuit) + ret = ret & moveFileToFile(backupfile_n, previousFile); } } @@ -456,12 +455,12 @@ public class BackupFiles } bfsb.append(backupFiles[i].getName()); } - Cache.log.debug("BACKUPFILES backupFiles: " + bfsb.toString()); + Cache.trace("BACKUPFILES backupFiles: " + bfsb.toString()); // noMax == true means keep all backup files if ((!noMax) && bfTreeMap.size() >= max) { - Cache.log.debug("BACKUPFILES noMax: " + noMax + ", " + "max: " + Cache.trace("BACKUPFILES noMax: " + noMax + ", " + "max: " + max + ", " + "bfTreeMap.size(): " + bfTreeMap.size()); // need to delete some files to keep number of backups to designated // max. @@ -471,7 +470,7 @@ public class BackupFiles int numToDelete = suffix.indexOf(NUM_PLACEHOLDER) > -1 ? bfTreeMap.size() - max + 1 : 0; - Cache.log.debug("BACKUPFILES numToDelete: " + numToDelete); + Cache.trace("BACKUPFILES numToDelete: " + numToDelete); // the "replacement" file is the latest backup file being kept (it's // not replacing though) File replacementFile = numToDelete < backupFiles.length @@ -484,8 +483,7 @@ public class BackupFiles File fileToBeDeleted = backupFiles[i]; boolean delete = true; - Cache.log.debug( - "BACKUPFILES fileToBeDeleted: " + fileToBeDeleted); + Cache.trace("BACKUPFILES fileToBeDeleted: " + fileToBeDeleted); boolean newer = false; if (replacementFile != null) @@ -501,7 +499,7 @@ public class BackupFiles String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile '" + Cache.warn("WARNING! I am set to delete backupfile '" + fileToBeDeleted.getName() + "' has modification time " + fileToBeDeletedLMTString @@ -516,14 +514,14 @@ public class BackupFiles { // User has confirmed delete -- no need to add it to the list fileToBeDeleted.delete(); - Cache.log.debug("BACKUPFILES deleting fileToBeDeleted: " + Cache.debug("BACKUPFILES deleting fileToBeDeleted: " + fileToBeDeleted); delete = false; } else { // keeping file, nothing to do! - Cache.log.debug("BACKUPFILES keeping fileToBeDeleted: " + Cache.debug("BACKUPFILES keeping fileToBeDeleted: " + fileToBeDeleted); } } @@ -531,7 +529,7 @@ public class BackupFiles if (delete) { addDeleteFile(fileToBeDeleted); - Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): " + Cache.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): " + fileToBeDeleted); } @@ -547,15 +545,16 @@ public class BackupFiles String latestBackupFilename = dir + File.separatorChar + BackupFilenameParts.getBackupFilename(nextIndexNum, basename, suffix, digits); - Cache.log.debug("BACKUPFILES Moving old file [" + file + Cache.trace("BACKUPFILES Moving old file [" + file + "] to latestBackupFilename [" + latestBackupFilename + "]"); - ret |= moveFileToFile(file, new File(latestBackupFilename)); - Cache.log.debug( - "BACKUPFILES moving " + file + " to " + latestBackupFilename - + " was " + (ret ? "" : "NOT ") + "successful"); + // using boolean '&' instead of '&&' as don't want moveFileToFile attempt to + // be conditional (short-circuit) + ret = ret & moveFileToFile(file, new File(latestBackupFilename)); + Cache.debug("BACKUPFILES moving " + file + " to " + latestBackupFilename + + " was " + (ret ? "" : "NOT ") + "successful"); if (tidyUp) { - Cache.log.debug("BACKUPFILES tidying up files"); + Cache.debug("BACKUPFILES tidying up files"); tidyUpFiles(); } @@ -611,7 +610,7 @@ public class BackupFiles saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile()); } catch (Exception e) { - Cache.log.error( + Cache.error( "Error when confirming to keep backup file newer than other backup files."); e.printStackTrace(); } @@ -721,10 +720,10 @@ public class BackupFiles for (int i = 0; i < deleteFiles.size(); i++) { File fileToDelete = deleteFiles.get(i); - Cache.log.debug( - "BACKUPFILES deleting fileToDelete:" + fileToDelete); + Cache.trace("BACKUPFILES about to delete fileToDelete:" + + fileToDelete); fileToDelete.delete(); - Cache.log.warn("deleting '" + fileToDelete.getName() + "'"); + Cache.warn("deleted '" + fileToDelete.getName() + "'"); } } @@ -796,6 +795,12 @@ public class BackupFiles "label.backupfiles_confirm_save_new_saved_file_not_ok")); // "The new saved file might not be okay." } + if (messageSB.length() > 0) + { + messageSB.append("\n"); + } + messageSB + .append(MessageManager.getString("label.continue_operation")); int confirmButton = Platform.isHeadless() ? JvOptionPane.OK_OPTION : JvOptionPane.showConfirmDialog(Desktop.desktop, @@ -827,8 +832,7 @@ public class BackupFiles dirFile = file.getParentFile(); } catch (Exception e) { - Cache.log.error( - "Could not get canonical path for file '" + file + "'"); + Cache.error("Could not get canonical path for file '" + file + "'"); return new TreeMap<>(); } @@ -869,14 +873,14 @@ public class BackupFiles int pos = deleteFiles.indexOf(fileToBeDeleted); if (pos > -1) { - Cache.log.debug("BACKUPFILES not adding file " + Cache.debug("BACKUPFILES not adding file " + fileToBeDeleted.getAbsolutePath() + " to the delete list (already at index" + pos + ")"); return true; } else { - Cache.log.debug("BACKUPFILES adding file " + Cache.debug("BACKUPFILES adding file " + fileToBeDeleted.getAbsolutePath() + " to the delete list"); deleteFiles.add(fileToBeDeleted); } @@ -885,30 +889,31 @@ public class BackupFiles public static boolean moveFileToFile(File oldFile, File newFile) { + Cache.initLogger(); boolean ret = false; Path oldPath = Paths.get(oldFile.getAbsolutePath()); Path newPath = Paths.get(newFile.getAbsolutePath()); try { // delete destination file - not usually necessary but Just In Case... - Cache.log.debug("BACKUPFILES deleting " + newFile.getAbsolutePath()); + Cache.trace("BACKUPFILES deleting " + newFile.getAbsolutePath()); newFile.delete(); - Cache.log.debug("BACKUPFILES moving " + oldFile.getAbsolutePath() + Cache.trace("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"); + Cache.trace("BACKUPFILES move seems to have succeeded"); } catch (IOException e) { - Cache.log.warn("Could not move file '" + oldPath.toString() + "' to '" + Cache.warn("Could not move file '" + oldPath.toString() + "' to '" + newPath.toString() + "'"); - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Cache.error(e.getMessage()); + Cache.debug(Cache.getStackTraceString(e)); ret = false; } catch (Exception e) { - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Cache.error(e.getMessage()); + Cache.debug(Cache.getStackTraceString(e)); ret = false; } return ret;