Make log file names on Windows less illegal

Also document requirements on the `name` parameter of `Main.logFile`.

Change-Id: I7d2e15595f0fe5667ddf15911cd036ffd1bc87cf
This commit is contained in:
Rudi Schlatte 2024-06-04 10:02:27 +02:00
parent 8992468386
commit fbb6162b0f

View File

@ -223,15 +223,16 @@ public class Main implements Callable<Integer> {
* Log a file into the given log directory. Does nothing if {@link * Log a file into the given log directory. Does nothing if {@link
* Main#logDirectory} is not set. * Main#logDirectory} is not set.
* *
* @param name The filename. Note that the file that is written will have * @param name A string that can be used as part of a filename, does not
* a longer name including a timestamp, so this argument does not need to * need to be unique. Should not contain characters that are illegal in
* be unique. * file names, e.g., avoid colons (:) or slashes (/).
* @param contents The content of the file to be written. Will be * @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) { public static void logFile(String name, Object contents) {
if (Main.logDirectory == null) return; 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); Path path = logDirectory.resolve(prefix + "--" + name);
try (FileWriter out = new FileWriter(path.toFile())) { try (FileWriter out = new FileWriter(path.toFile())) {
out.write(contents.toString()); out.write(contents.toString());