diff --git a/README.md b/README.md index 8776157..bdfc19d 100644 --- a/README.md +++ b/README.md @@ -76,9 +76,9 @@ swift run monitord --retention 7d --dir /tmp/logs # rotating CSV logger ``` `monitord` is the logger: it samples every metric on the same clock and writes -rotating, human-readable CSV — one file per day, hostname in the filename and as -a column, timestamps in ISO8601 and epoch millis, temperatures in both °C and -°F. Run it as a launchd `LaunchAgent` to log for days. +rotating, human-readable CSV — one file per run, hostname in the filename and +as a column, timestamps in ISO8601 and epoch millis, temperatures in both °C +and °F. Run it as a launchd `LaunchAgent` to log for days. With no options it logs at 1s with 1d retention to `~/Library/Logs/monitor`. The release zip ships a standalone `monitord` binary alongside `monitor.app`, so diff --git a/Sources/MonitorCore/LogRetention.swift b/Sources/MonitorCore/LogRetention.swift index 589539f..df8f6f3 100644 --- a/Sources/MonitorCore/LogRetention.swift +++ b/Sources/MonitorCore/LogRetention.swift @@ -42,16 +42,17 @@ public enum LogRetention: String, CaseIterable, Sendable { return calendar.date(from: components)?.timeIntervalSince1970 ?? timestamp } - /// The day a file covers, read back from its name. The date is the last - /// component of the name, so a hostname that itself contains dashes or dots - /// cannot confuse the parse. + /// The day a file covers, read back from its name. The date and time are + /// the last components of the name, so a hostname that itself contains + /// dashes or dots cannot confuse the parse. The time is ignored: the period + /// is the start of the day, so retention still deletes whole days. public static func period(from filename: String) -> TimeInterval? { let base = filename.hasSuffix(".csv") ? String(filename.dropLast(4)) : filename let formatter = DateFormatter() formatter.timeZone = .current - formatter.dateFormat = "yyyy_MM_dd" - guard base.count >= 10 else { return nil } - guard let date = formatter.date(from: String(base.suffix(10))) else { return nil } - return date.timeIntervalSince1970 + formatter.dateFormat = "yyyy_MM_dd_HH_mm_ss" + guard base.count >= 19 else { return nil } + guard let date = formatter.date(from: String(base.suffix(19))) else { return nil } + return period(for: date.timeIntervalSince1970) } } diff --git a/Sources/MonitorLog/CSVLogSink.swift b/Sources/MonitorLog/CSVLogSink.swift index c978b39..7b17017 100644 --- a/Sources/MonitorLog/CSVLogSink.swift +++ b/Sources/MonitorLog/CSVLogSink.swift @@ -3,10 +3,10 @@ import MonitorCore /// Writes sampled batches to rotating CSV files. /// -/// One file per day, named `sensors...csv` so files from several -/// machines sharing a directory do not clobber. The header is written when a -/// file is first created; a file reopened after a restart appends without -/// repeating it. +/// One file per run, named `sensors.._