From e5ab744e583a24534e58cb99de2e5e5d73579754 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 19 Feb 2025 14:15:44 -0500 Subject: [PATCH] [insteon] Truncate debug log files when starting monitor (#18295) Signed-off-by: Jeremy Setton --- .../internal/command/DebugCommand.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/command/DebugCommand.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/command/DebugCommand.java index fd7065aa33..81f0a4f0fa 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/command/DebugCommand.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/command/DebugCommand.java @@ -271,11 +271,19 @@ public class DebugCommand extends InsteonCommand implements PortListener { return getBindingDataFilePath(MSG_EVENTS_FILE_PREFIX + "-" + name + ".log"); } - private void clearMonitorFiles(@Nullable DeviceAddress address) { - String prefix = address == null ? MSG_EVENTS_FILE_PREFIX - : getMsgEventsFilePath(address).getFileName().toString(); + private void clearMonitorFiles() { + getBindingDataFilePaths(MSG_EVENTS_FILE_PREFIX).map(Path::toFile).forEach(File::delete); + } - getBindingDataFilePaths(prefix).map(Path::toFile).forEach(File::delete); + private void truncateMonitorFile(DeviceAddress address) { + try { + Path path = getMsgEventsFilePath(address); + + Files.createDirectories(path.getParent()); + Files.writeString(path, "", StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } catch (IOException e) { + logger.warn("failed to truncate message event file", e); + } } private void logMessageEvent(Msg msg) { @@ -315,7 +323,7 @@ public class DebugCommand extends InsteonCommand implements PortListener { monitoredAddresses.clear(); console.println("Started monitoring all devices."); console.println("Message events logged in " + getBindingDataDirPath()); - clearMonitorFiles(null); + clearMonitorFiles(); } else { DeviceAddress address = InsteonAddress.isValid(arg) ? new InsteonAddress(arg) : X10Address.isValid(arg) ? new X10Address(arg) : null; @@ -324,7 +332,7 @@ public class DebugCommand extends InsteonCommand implements PortListener { } else if (monitoredAddresses.add(address)) { console.println("Started monitoring the device " + address + "."); console.println("Message events logged in " + getMsgEventsFilePath(address)); - clearMonitorFiles(address); + truncateMonitorFile(address); } else { console.println("Already monitoring the device " + address + "."); }