276d2bb6-619d-4888-ba11-8a6.../Sources/engine/ClipperDebugLog.swift

42 lines
1.4 KiB
Swift
Raw Normal View History

//
// ClipperDebugLog.swift
// plugin
//
// Mirrors the FundDebugLog pattern plugin-bundle NSLog calls don't
// reliably surface in `log show` from the host process. This helper
// appends timestamped lines to /tmp/clipper-plugin.log so we can
// `tail -f` during debugging.
//
import Foundation
enum ClipperDebugLog {
static let path = "/tmp/clipper-plugin.log"
private static let queue = DispatchQueue(label: "clipper.debug.log")
private static let dateFmt: DateFormatter = {
let f = DateFormatter()
f.locale = Locale(identifier: "en_US_POSIX")
f.timeZone = TimeZone.current
f.dateFormat = "HH:mm:ss.SSS"
return f
}()
static func write(_ message: String) {
let stamp = dateFmt.string(from: Date())
let line = "[\(stamp)] \(message)\n"
queue.async {
if let data = line.data(using: .utf8) {
if FileManager.default.fileExists(atPath: path) {
if let handle = try? FileHandle(forWritingTo: URL(fileURLWithPath: path)) {
try? handle.seekToEnd()
try? handle.write(contentsOf: data)
try? handle.close()
}
} else {
try? data.write(to: URL(fileURLWithPath: path))
}
}
}
}
}