Skip to content

Commit

Permalink
more informative message when deleting files
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Nov 6, 2023
1 parent c428711 commit f25df08
Showing 1 changed file with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
* Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
* reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
* This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
* v1.0 as published by the Eclipse Foundation
*
* or (per the licensee's choosing)
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
* under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
*/
package ch.qos.logback.core.rolling.helper;

Expand Down Expand Up @@ -86,8 +84,7 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
File[] matchingFileArray = getFilesInPeriod(instantOfPeriodToClean);

for (File f : matchingFileArray) {
addInfo("deleting " + f);
f.delete();
checkAndDeleteFile(f);
}

if (parentClean && matchingFileArray.length > 0) {
Expand All @@ -96,6 +93,23 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
}
}

private boolean checkAndDeleteFile(File f) {
addInfo("deleting " + f);
if (f == null) {
addWarn("Cannot delete empty file");
return false;
} else if (!f.exists()) {
addWarn("Cannot delete non existent file");
return false;
}

boolean result = f.delete();
if (!result) {
addWarn("Failed to delete file " + f.toString());
}
return result;
}

void capTotalSize(Instant now) {
long totalSize = 0;
long totalRemoved = 0;
Expand All @@ -107,8 +121,10 @@ void capTotalSize(Instant now) {
long size = f.length();
if (totalSize + size > totalSizeCap) {
addInfo("Deleting [" + f + "]" + " of size " + new FileSize(size));
// assume that deletion attempt will succeed.
totalRemoved += size;
f.delete();

checkAndDeleteFile(f);
}
totalSize += size;
}
Expand Down Expand Up @@ -141,7 +157,7 @@ int computeElapsedPeriodsSinceLastClean(long nowInMillis) {

/**
* Computes whether the fileNamePattern may create sub-folders.
*
*
* @param fileNamePattern
* @return
*/
Expand Down Expand Up @@ -183,9 +199,8 @@ void removeFolderIfEmpty(File dir) {
}

/**
* Will remove the directory passed as parameter if empty. After that, if the
* parent is also becomes empty, remove the parent dir as well but at most 3
* times.
* Will remove the directory passed as parameter if empty. After that, if the parent is also becomes empty, remove
* the parent dir as well but at most 3 times.
*
* @param dir
* @param depth
Expand All @@ -197,7 +212,7 @@ private void removeFolderIfEmpty(File dir, int depth) {
}
if (dir.isDirectory() && FileFilterUtil.isEmptyDirectory(dir)) {
addInfo("deleting folder [" + dir + "]");
dir.delete();
checkAndDeleteFile(dir);
removeFolderIfEmpty(dir.getParentFile(), depth + 1);
}
}
Expand Down

0 comments on commit f25df08

Please sign in to comment.