Files
openclaw/apps/macos/Sources/OpenClaw/LogLocator.swift

60 lines
1.9 KiB
Swift
Raw Normal View History

2025-12-10 00:03:37 +00:00
import Foundation
enum LogLocator {
private static var logDir: URL {
2026-01-30 03:15:10 +01:00
if let override = ProcessInfo.processInfo.environment["OPENCLAW_LOG_DIR"],
!override.isEmpty
{
return URL(fileURLWithPath: override)
}
return URL(fileURLWithPath: "/tmp/openclaw")
}
private static var stdoutLog: URL {
2026-01-30 03:15:10 +01:00
logDir.appendingPathComponent("openclaw-stdout.log")
}
private static var gatewayLog: URL {
2026-01-30 03:15:10 +01:00
logDir.appendingPathComponent("openclaw-gateway.log")
}
2025-12-10 00:03:37 +00:00
private static func ensureLogDirExists() {
try? FileManager().createDirectory(at: self.logDir, withIntermediateDirectories: true)
}
2025-12-10 01:00:53 +00:00
private static func modificationDate(for url: URL) -> Date {
(try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast
}
2026-01-30 03:15:10 +01:00
/// Returns the newest log file under /tmp/openclaw/ (rolling or stdout), or nil if none exist.
2025-12-10 00:03:37 +00:00
static func bestLogFile() -> URL? {
self.ensureLogDirExists()
let fm = FileManager()
2025-12-10 00:04:57 +00:00
let files = (try? fm.contentsOfDirectory(
2026-01-15 14:40:57 +00:00
at: self.logDir,
includingPropertiesForKeys: [.contentModificationDateKey],
options: [.skipsHiddenFiles])) ?? []
2025-12-10 00:03:37 +00:00
2026-01-30 03:15:10 +01:00
let prefixes = ["openclaw"]
2025-12-10 00:04:57 +00:00
return files
2026-01-30 03:15:10 +01:00
.filter { file in
prefixes.contains { file.lastPathComponent.hasPrefix($0) } && file.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 {
self.ensureLogDirExists()
return stdoutLog.path
2025-12-10 00:03:37 +00:00
}
2026-01-11 10:15:37 +00:00
/// Path to use for the Gateway launchd job stdout/err.
static var launchdGatewayLogPath: String {
self.ensureLogDirExists()
return gatewayLog.path
}
2025-12-10 00:03:37 +00:00
}