46 lines
1.6 KiB
Swift
46 lines
1.6 KiB
Swift
import Foundation
|
|
import OSLog
|
|
|
|
/// Shared logger for Bugger. View output in Xcode console or Console.app
|
|
/// (filter by subsystem: com.xorbitlab.bugger).
|
|
enum BuggerLog {
|
|
private static let logger = Logger(subsystem: "com.xorbitlab.bugger", category: "general")
|
|
private static let queue = DispatchQueue(label: "com.xorbitlab.bugger.log")
|
|
private static let logURL: URL = {
|
|
FileManager.default.homeDirectoryForCurrentUser
|
|
.appendingPathComponent("Library/Logs/Bugger.log")
|
|
}()
|
|
|
|
static func info(_ message: String) {
|
|
logger.info("\(message, privacy: .public)")
|
|
write("INFO", message)
|
|
}
|
|
|
|
static func error(_ message: String) {
|
|
logger.error("\(message, privacy: .public)")
|
|
write("ERROR", message)
|
|
}
|
|
|
|
static func debug(_ message: String) {
|
|
logger.debug("\(message, privacy: .public)")
|
|
write("DEBUG", message)
|
|
}
|
|
|
|
/// Unified logging for this ad-hoc app has been empty, so keep a file too.
|
|
private static func write(_ level: String, _ message: String) {
|
|
queue.async {
|
|
let stamp = ISO8601DateFormatter().string(from: Date())
|
|
let line = "\(stamp) \(level) \(message)\n"
|
|
guard let data = line.data(using: .utf8) else { return }
|
|
if FileManager.default.fileExists(atPath: logURL.path),
|
|
let handle = try? FileHandle(forWritingTo: logURL) {
|
|
defer { try? handle.close() }
|
|
_ = try? handle.seekToEnd()
|
|
try? handle.write(contentsOf: data)
|
|
} else {
|
|
try? data.write(to: logURL)
|
|
}
|
|
}
|
|
}
|
|
}
|