openclaw/apps/macos/Sources/Clawdis/LogLocator.swift

31 lines
1.1 KiB
Swift
Raw Permalink Normal View History

2025-12-10 00:03:37 +00:00
import Foundation
enum LogLocator {
private static let logDir = URL(fileURLWithPath: "/tmp/clawdis")
2025-12-10 00:04:57 +00:00
private static let stdoutLog = logDir.appendingPathComponent("clawdis-stdout.log")
2025-12-10 00:03:37 +00:00
2025-12-10 01:00:53 +00:00
private static func modificationDate(for url: URL) -> Date {
(try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast
}
2025-12-10 00:04:57 +00:00
/// Returns the newest log file under /tmp/clawdis/ (rolling or stdout), or nil if none exist.
2025-12-10 00:03:37 +00:00
static func bestLogFile() -> URL? {
let fm = FileManager.default
2025-12-10 00:04:57 +00:00
let files = (try? fm.contentsOfDirectory(
2025-12-10 01:00:53 +00:00
at: self.logDir,
2025-12-10 00:03:37 +00:00
includingPropertiesForKeys: [.contentModificationDateKey],
options: [.skipsHiddenFiles])) ?? []
2025-12-10 00:04:57 +00:00
return files
.filter { $0.lastPathComponent.hasPrefix("clawdis") && $0.pathExtension == "log" }
2025-12-10 01:00:53 +00:00
.max { lhs, rhs in
self.modificationDate(for: lhs) < self.modificationDate(for: rhs)
2025-12-10 00:03:37 +00:00
}
}
2025-12-10 00:04:57 +00:00
/// Path to use for launchd stdout/err.
static var launchdLogPath: String {
stdoutLog.path
2025-12-10 00:03:37 +00:00
}
}