import Foundation private let df: DateFormatter = { let df = DateFormatter() df.dateStyle = .short df.timeStyle = .short df.locale = Locale(identifier: "de") return df }() func log(_ message: String, file: String = #file, line: Int = #line) { let date = df.string(from: Date()) let m = "[\(date)][\(file.components(separatedBy: "/").last ?? file):\(line)] \(message)" print(m) Log.write(m + "\n") } enum Log { static func set(logFile: String) throws { let url = URL(fileURLWithPath: logFile) let date = df.string(from: Date()) if !FileManager.default.fileExists(atPath: logFile) { try "[\(date)] New log created.\n".write(to: url, atomically: false, encoding: .utf8) } guard let f = FileHandle(forWritingAtPath: logFile) else { try "[\(date)] Failed to start log.\n".write(to: url, atomically: false, encoding: .utf8) return } f.seekToEndOfFile() f.write("[\(date)] Logging started.\n".data(using: .utf8)!) file = f } static func write(_ message: String) { guard let f = file else { return } f.write(message.data(using: .utf8)!) } private static var file: FileHandle? }