Update logging calls in FileAccess

This commit is contained in:
JeremyStar™ 2024-12-04 01:00:06 +01:00
parent f7c130d66e
commit a7c02cc9c2
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -250,14 +250,14 @@ public final class FileAccess {
for (Path path : scheduledDeletion) for (Path path : scheduledDeletion)
try (Stream<Path> stream = Files.walk(path)) { try (Stream<Path> stream = Files.walk(path)) {
Logger.diag("Deleting file or directory \"" + path + "\""); Logger.diag("Deleting file or directory '" + path + "'");
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
stream.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); stream.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
if (Files.exists(path)) if (Files.exists(path))
Logger.error("Deleting file or directory \"" + path + "\" failed"); Logger.error("Deleting file or directory '" + path + "' failed");
} catch (Exception exception) { } catch (Exception exception) {
Logger.error("File or directory \"" + path + "\" could not be deleted\n" + Miscellaneous.throwableHeader(exception) + "\n" + Miscellaneous.stacktraceAsString(exception, true)); Logger.error("File or directory '" + path + "' could not be deleted\n" + Miscellaneous.throwableHeader(exception) + "\n" + Miscellaneous.stacktraceAsString(exception, true));
} }
} }
} }
@ -406,18 +406,18 @@ public final class FileAccess {
* directories in this directory. * directories in this directory.
* *
* @return array of file and directory names * @return array of file and directory names
* @throws UnsupportedOperationException if the file is not a directory * @throws UnsupportedOperationException if this file isn't a directory
* @throws IOException on an IO error * @throws IOException on an IO error
* @since v1-alpha8 * @since v1-alpha8
*/ */
public @NotNull String @NotNull [] listContents() throws UnsupportedOperationException, IOException { public @NotNull String @NotNull [] listContents() throws UnsupportedOperationException, IOException {
if (getType() != Type.DIRECTORY) if (getType() != Type.DIRECTORY)
throw new UnsupportedOperationException("The file \"" + path + "\" is not a directory"); throw new UnsupportedOperationException("The file '" + path + "' is not a directory");
String[] list = file.list(); String[] list = file.list();
if (list == null) if (list == null)
throw new IOException("list is null"); throw new IOException("list is null (isn't a directory although it should be one)");
else else
return list; return list;
} }
@ -432,7 +432,7 @@ public final class FileAccess {
*/ */
public @NotNull String getLinkDestination() throws IOException { public @NotNull String getLinkDestination() throws IOException {
if (!isSymbolicLink()) if (!isSymbolicLink())
throw new UnsupportedOperationException("The file \"" + path + "\" is not a symbolic link"); throw new UnsupportedOperationException("The file '" + path + "' is not a symbolic link");
return unformatPath(Files.readSymbolicLink(path)); return unformatPath(Files.readSymbolicLink(path));
} }
@ -516,10 +516,10 @@ public final class FileAccess {
throw new IllegalArgumentException("Invalid permission format: " + permissions); throw new IllegalArgumentException("Invalid permission format: " + permissions);
try { try {
Logger.diag("Setting POSIX file permissions for \"" + path + "\" to '" + permissions + "'"); Logger.diag("Setting POSIX file permissions for '" + path + "' to '" + permissions + "'");
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(permissions)); Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(permissions));
} catch (UnsupportedOperationException exception) { } catch (UnsupportedOperationException exception) {
Logger.diag("Setting POSIX file permissions for \"" + path + "\" to '" + permissions.substring(0, 2) + "'"); Logger.diag("Setting POSIX file permissions for '" + path + "' to '" + permissions.substring(0, 2) + "'");
char @NotNull [] chars = permissions.toCharArray(); char @NotNull [] chars = permissions.toCharArray();
for (int permission = 0; permission < 3; permission++) { for (int permission = 0; permission < 3; permission++) {
@ -622,7 +622,7 @@ public final class FileAccess {
Path pathResolved = this.path.resolve(formatPath(path)); Path pathResolved = this.path.resolve(formatPath(path));
if (!Files.exists(pathResolved)) if (!Files.exists(pathResolved))
throw new FileNotFoundException("Traversal failed as relative path \"" + path + "\" does not exist from absolute path \"" + path + "\""); throw new FileNotFoundException("Traversal failed as relative path '" + path + "' does not exist from absolute path '" + path + "'");
return new FileAccess(pathResolved); return new FileAccess(pathResolved);
} }
@ -640,7 +640,7 @@ public final class FileAccess {
@SuppressWarnings({"UnusedReturnValue", "ResultOfMethodCallIgnored"}) @SuppressWarnings({"UnusedReturnValue", "ResultOfMethodCallIgnored"})
public @NotNull FileAccess createFile() throws IOException { public @NotNull FileAccess createFile() throws IOException {
if (!exists()) { if (!exists()) {
Logger.diag("Creating a file at \"" + path + "\""); Logger.diag("Creating a file at '" + path + "'");
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
file.createNewFile(); file.createNewFile();
} }
@ -658,9 +658,9 @@ public final class FileAccess {
*/ */
public @NotNull FileAccess createDirectory() throws IOException { public @NotNull FileAccess createDirectory() throws IOException {
if (!exists()) { if (!exists()) {
Logger.diag("Creating a directory at \"" + path + "\""); Logger.diag("Creating a directory at '" + path + "'");
if (!file.mkdirs()) if (!file.mkdirs())
throw new IOException("Creating directory \"" + path + "\" recursively failed"); throw new IOException("Creating directory '" + path + "' recursively failed");
} }
return this; return this;
@ -679,7 +679,7 @@ public final class FileAccess {
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
public @NotNull FileAccess createLink(boolean hard, @NotNull String destination) throws IOException { public @NotNull FileAccess createLink(boolean hard, @NotNull String destination) throws IOException {
if (!exists()) { if (!exists()) {
Logger.diag("Creating a " + (hard ? "hard" : "symbolic") + " link at \"" + path + "\""); Logger.diag("Creating a " + (hard ? "hard" : "symbolic") + " link at '" + path + "'");
if (hard) if (hard)
Files.createLink(path, formatPath(destination)); Files.createLink(path, formatPath(destination));
@ -725,7 +725,7 @@ public final class FileAccess {
*/ */
public @NotNull FileAccess delete() { public @NotNull FileAccess delete() {
if (exists()) { if (exists()) {
Logger.diag("Deleting \"" + path + "\""); Logger.diag("Deleting '" + path + "'");
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
file.delete(); file.delete();
} }
@ -741,7 +741,7 @@ public final class FileAccess {
* @since v1-alpha8 * @since v1-alpha8
*/ */
public @NotNull FileAccess deleteOnShutdown() { public @NotNull FileAccess deleteOnShutdown() {
Logger.diag("Marking \"" + path + "\" for deletion at engine shutdown"); Logger.diag("Marking '" + path + "' for deletion at engine shutdown");
// Append path to scheduledDeletion array // Append path to scheduledDeletion array
List<@NotNull Path> scheduledDeletionList = new ArrayList<>(Arrays.stream(scheduledDeletion).toList()); List<@NotNull Path> scheduledDeletionList = new ArrayList<>(Arrays.stream(scheduledDeletion).toList());
@ -800,7 +800,7 @@ public final class FileAccess {
if (getType() != Type.FILE) if (getType() != Type.FILE)
return new byte[0]; return new byte[0];
Logger.diag("Reading file \"" + path + "\" (bytes)"); Logger.diag("Reading file '" + path + "' (bytes)");
return Files.readAllBytes(path); return Files.readAllBytes(path);
} }
@ -819,7 +819,7 @@ public final class FileAccess {
if (getType() != Type.FILE) if (getType() != Type.FILE)
return new ArrayList<>(); return new ArrayList<>();
Logger.diag("Reading file \"" + path + "\" (lines)"); Logger.diag("Reading file '" + path + "' (lines)");
return Files.readAllLines(path); return Files.readAllLines(path);
} }
@ -856,7 +856,7 @@ public final class FileAccess {
if (getType() != Type.FILE) if (getType() != Type.FILE)
return ""; return "";
Logger.diag("Reading file \"" + path + "\" (string)"); Logger.diag("Reading file '" + path + "' (string)");
return Files.readString(path, charset); return Files.readString(path, charset);
} }
@ -875,10 +875,10 @@ public final class FileAccess {
if (getType() == Type.VOID) if (getType() == Type.VOID)
createFile(); createFile();
else if (getType() != Type.FILE) else if (getType() != Type.FILE)
throw new UnsupportedOperationException("File \"" + path + "\" is not of type Type.VOID or Type.FILE"); throw new UnsupportedOperationException("File '" + path + "' is not of type Type.VOID or Type.FILE");
createFile(); createFile();
Logger.diag("Writing file \"" + path + "\" (bytes, " + (async ? "async" : "dsync") + ")"); Logger.diag("Writing file '" + path + "' (bytes, " + (async ? "async" : "dsync") + ")");
Files.write(path, bytes, StandardOpenOption.WRITE, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC); Files.write(path, bytes, StandardOpenOption.WRITE, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC);
return this; return this;
} }
@ -912,9 +912,9 @@ public final class FileAccess {
if (getType() == Type.VOID) if (getType() == Type.VOID)
createFile(); createFile();
else if (getType() != Type.FILE) else if (getType() != Type.FILE)
throw new UnsupportedOperationException("File \"" + path + "\" is not of type Type.VOID or Type.FILE"); throw new UnsupportedOperationException("File '" + path + "' is not of type Type.VOID or Type.FILE");
Logger.diag("Writing file \"" + path + "\" (string, " + (async ? "async" : "dsync") + ")"); Logger.diag("Writing file '" + path + "' (string, " + (async ? "async" : "dsync") + ")");
Files.writeString(path, string, charset, StandardOpenOption.WRITE, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC); Files.writeString(path, string, charset, StandardOpenOption.WRITE, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC);
return this; return this;
} }
@ -934,9 +934,9 @@ public final class FileAccess {
if (getType() == Type.VOID) if (getType() == Type.VOID)
createFile(); createFile();
else if (getType() != Type.FILE) else if (getType() != Type.FILE)
throw new UnsupportedOperationException("File \"" + path + "\" is not of type Type.VOID or Type.FILE"); throw new UnsupportedOperationException("File '" + path + "' is not of type Type.VOID or Type.FILE");
Logger.diag("Appending file \"" + path + "\" (bytes, " + (async ? "async" : "dsync") + ")"); Logger.diag("Appending file '" + path + "' (bytes, " + (async ? "async" : "dsync") + ")");
Files.write(path, bytes, StandardOpenOption.APPEND, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC); Files.write(path, bytes, StandardOpenOption.APPEND, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC);
return this; return this;
} }
@ -970,13 +970,15 @@ public final class FileAccess {
if (getType() == Type.VOID) if (getType() == Type.VOID)
createFile(); createFile();
else if (getType() != Type.FILE) else if (getType() != Type.FILE)
throw new UnsupportedOperationException("File \"" + path + "\" is not of type Type.VOID or Type.FILE"); throw new UnsupportedOperationException("File '" + path + "' is not of type Type.VOID or Type.FILE");
Logger.diag("Appending file \"" + path + "\" (string, " + (async ? "async" : "dsync") + ")"); Logger.diag("Appending file '" + path + "' (string, " + (async ? "async" : "dsync") + ")");
Files.writeString(path, string, charset, StandardOpenOption.APPEND, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC); Files.writeString(path, string, charset, StandardOpenOption.APPEND, async ? StandardOpenOption.DSYNC : StandardOpenOption.SYNC);
return this; return this;
} }
// -----> Inner classes
/** /**
* Represents various file types. * Represents various file types.
* *