From 4f53c2e5c8ba443e2097cb7a88c63891f92df3d2 Mon Sep 17 00:00:00 2001 From: christophhagen Date: Sat, 23 May 2020 14:54:27 +0200 Subject: [PATCH] Update logging --- Sources/App/Log.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/App/Log.swift b/Sources/App/Log.swift index e57a653..96e2b7e 100644 --- a/Sources/App/Log.swift +++ b/Sources/App/Log.swift @@ -18,17 +18,23 @@ 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("\n" + 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 "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) {