Update logging

This commit is contained in:
christophhagen 2020-05-23 14:54:27 +02:00
parent 9aaa98a7a2
commit 4f53c2e5c8

View File

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