From fbb6162b0f6b37b2c9d8655ea6da81c41ff55af6 Mon Sep 17 00:00:00 2001 From: Rudi Schlatte Date: Tue, 4 Jun 2024 10:02:27 +0200 Subject: [PATCH] Make log file names on Windows less illegal Also document requirements on the `name` parameter of `Main.logFile`. Change-Id: I7d2e15595f0fe5667ddf15911cd036ffd1bc87cf --- .../eu/nebulouscloud/optimiser/controller/Main.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java index bca4957..a230308 100644 --- a/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java +++ b/optimiser-controller/src/main/java/eu/nebulouscloud/optimiser/controller/Main.java @@ -223,15 +223,16 @@ public class Main implements Callable { * Log a file into the given log directory. Does nothing if {@link * Main#logDirectory} is not set. * - * @param name The filename. Note that the file that is written will have - * a longer name including a timestamp, so this argument does not need to - * be unique. + * @param name A string that can be used as part of a filename, does not + * need to be unique. Should not contain characters that are illegal in + * file names, e.g., avoid colons (:) or slashes (/). * @param contents The content of the file to be written. Will be - * converted to String via `toString`. + * converted to a String via `toString`. */ public static void logFile(String name, Object contents) { if (Main.logDirectory == null) return; - String prefix = LocalDateTime.now().toString(); + String prefix = LocalDateTime.now().toString() + .replace(":", "-"); // make Windows less unhappy Path path = logDirectory.resolve(prefix + "--" + name); try (FileWriter out = new FileWriter(path.toFile())) { out.write(contents.toString());