macOS: ensure /tmp/clawdbot exists for launchd logs
This commit is contained in:
committed by
Peter Steinberger
parent
5b23f847d6
commit
aaae327563
@@ -5,12 +5,17 @@ enum LogLocator {
|
|||||||
private static let stdoutLog = logDir.appendingPathComponent("clawdbot-stdout.log")
|
private static let stdoutLog = logDir.appendingPathComponent("clawdbot-stdout.log")
|
||||||
private static let gatewayLog = logDir.appendingPathComponent("clawdbot-gateway.log")
|
private static let gatewayLog = logDir.appendingPathComponent("clawdbot-gateway.log")
|
||||||
|
|
||||||
|
private static func ensureLogDirExists() {
|
||||||
|
try? FileManager.default.createDirectory(at: self.logDir, withIntermediateDirectories: true)
|
||||||
|
}
|
||||||
|
|
||||||
private static func modificationDate(for url: URL) -> Date {
|
private static func modificationDate(for url: URL) -> Date {
|
||||||
(try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast
|
(try? url.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the newest log file under /tmp/clawdbot/ (rolling or stdout), or nil if none exist.
|
/// Returns the newest log file under /tmp/clawdbot/ (rolling or stdout), or nil if none exist.
|
||||||
static func bestLogFile() -> URL? {
|
static func bestLogFile() -> URL? {
|
||||||
|
self.ensureLogDirExists()
|
||||||
let fm = FileManager.default
|
let fm = FileManager.default
|
||||||
let files = (try? fm.contentsOfDirectory(
|
let files = (try? fm.contentsOfDirectory(
|
||||||
at: self.logDir,
|
at: self.logDir,
|
||||||
@@ -26,11 +31,13 @@ enum LogLocator {
|
|||||||
|
|
||||||
/// Path to use for launchd stdout/err.
|
/// Path to use for launchd stdout/err.
|
||||||
static var launchdLogPath: String {
|
static var launchdLogPath: String {
|
||||||
stdoutLog.path
|
self.ensureLogDirExists()
|
||||||
|
return stdoutLog.path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Path to use for the Gateway launchd job stdout/err.
|
/// Path to use for the Gateway launchd job stdout/err.
|
||||||
static var launchdGatewayLogPath: String {
|
static var launchdGatewayLogPath: String {
|
||||||
gatewayLog.path
|
self.ensureLogDirExists()
|
||||||
|
return gatewayLog.path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift
Normal file
21
apps/macos/Tests/ClawdbotIPCTests/LogLocatorTests.swift
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
@testable import Clawdbot
|
||||||
|
|
||||||
|
@Suite struct LogLocatorTests {
|
||||||
|
@Test func launchdGatewayLogPathEnsuresTmpDirExists() throws {
|
||||||
|
let dirPath = "/tmp/clawdbot"
|
||||||
|
let fm = FileManager.default
|
||||||
|
|
||||||
|
// Simulate a clean machine state where /tmp/clawdbot does not exist.
|
||||||
|
if fm.fileExists(atPath: dirPath) {
|
||||||
|
try? fm.removeItem(atPath: dirPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = LogLocator.launchdGatewayLogPath
|
||||||
|
|
||||||
|
var isDir: ObjCBool = false
|
||||||
|
#expect(fm.fileExists(atPath: dirPath, isDirectory: &isDir))
|
||||||
|
#expect(isDir.boolValue == true)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user