import Foundation /// Appends to ~/Library/Logs/CampusNAS.log. /// /// NSLog output from an ad-hoc signed bundle does not reliably reach the /// unified log, which made real errors invisible while debugging notification /// delivery. A plain file is boring and always readable. enum Log { static let fileURL = URL(fileURLWithPath: NSHomeDirectory()) .appendingPathComponent("Library/Logs/CampusNAS.log") private static let queue = DispatchQueue(label: "campusnas.log") private static let formatter: DateFormatter = { let f = DateFormatter() f.dateFormat = "yyyy-MM-dd HH:mm:ss" return f }() static func write(_ message: String) { NSLog("campusnas-monitor: %@", message) let line = "\(formatter.string(from: Date())) \(message)\n" queue.async { guard let data = line.data(using: .utf8) else { return } if let handle = try? FileHandle(forWritingTo: fileURL) { defer { try? handle.close() } _ = try? handle.seekToEnd() try? handle.write(contentsOf: data) } else { try? data.write(to: fileURL) } } } }