mirror of
https://github.com/immich-app/immich.git
synced 2026-04-25 02:29:51 -04:00
34 lines
1.1 KiB
Swift
34 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
enum FileLogger {
|
|
private static let queue = DispatchQueue(label: "app.alextran.immich.FileLogger")
|
|
private static let isoFormatter: ISO8601DateFormatter = {
|
|
let f = ISO8601DateFormatter()
|
|
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
return f
|
|
}()
|
|
|
|
private static var logFileURL: URL? {
|
|
guard let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
|
|
else { return nil }
|
|
return docs.appendingPathComponent("background_log.txt")
|
|
}
|
|
|
|
static func log(_ message: String) {
|
|
let line = "[\(isoFormatter.string(from: Date()))] \(message)\n"
|
|
print(line, terminator: "")
|
|
queue.async {
|
|
guard let url = logFileURL, let data = line.data(using: .utf8) else { return }
|
|
if FileManager.default.fileExists(atPath: url.path) {
|
|
if let handle = try? FileHandle(forWritingTo: url) {
|
|
defer { try? handle.close() }
|
|
try? handle.seekToEnd()
|
|
try? handle.write(contentsOf: data)
|
|
}
|
|
} else {
|
|
try? data.write(to: url, options: .atomic)
|
|
}
|
|
}
|
|
}
|
|
}
|