From 599b9ca7dc8148cf495be2fa344e5dc209b7e017 Mon Sep 17 00:00:00 2001 From: Stefan Wieland Date: Thu, 28 Nov 2019 16:09:27 +0100 Subject: [PATCH] cleanup --- Example/AppDelegate.swift | 44 ++++++++++++++------------- Logbook/Source/Sink/FileLogSink.swift | 22 +++++--------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Example/AppDelegate.swift b/Example/AppDelegate.swift index 89be2af..dfac8ef 100644 --- a/Example/AppDelegate.swift +++ b/Example/AppDelegate.swift @@ -14,14 +14,18 @@ let log = Logbook.self @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { + private lazy var dateFormatter: DateFormatter = { + let formatter = DateFormatter() + formatter.dateStyle = .none + formatter.timeStyle = .short + return formatter + }() + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - let dateFormatter = DateFormatter() - dateFormatter.dateStyle = .none - dateFormatter.timeStyle = .short - // Create sink - let console = ConsoleLogSink(level: .min(.debug), categories: [.networking]) + let console = ConsoleLogSink(level: .min(.debug)) // change date format console.dateFormatter = dateFormatter @@ -35,29 +39,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Override default LogCategory LogCategory.default = LogCategory("default", prefix: "💿") - log.debug("default log", application, launchOptions) - log.debug("didFinishLaunchingWithOptions", category: .startup) - - log.error("something went wrong") - - // log all with min level warning - Logbook.add(sink: ConsoleLogSink(level: .min(.debug))) - // log only level error with category .networking - Logbook.add(sink: ConsoleLogSink(level: .fix(.error), categories: [.networking])) - - if let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { - let fileSink = FileLogSink(level: .min(.debug), categories: [.fileTest], baseDirectory: path) - Logbook.add(sink: fileSink) - } - Logbook.add(sink: OSLogSink(level: .min(.debug), isPublic: true)) + // testmessage logToFile() return true } - func logToFile() { + private func addOSLog() { + let ossink = OSLogSink(level: .min(.debug), isPublic: false) + Logbook.add(sink: ossink) + } + + private func addFileLog() { + guard let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return } + + let fileSink = FileLogSink(level: .min(.debug), categories: [.fileTest], baseDirectory: path, maxFileSize: 5) + Logbook.add(sink: fileSink) + } + + private func logToFile() { DispatchQueue.main.asyncAfter (deadline: .now() + .milliseconds(500)) { log.error("Test String", category: .fileTest) self.logToFile() diff --git a/Logbook/Source/Sink/FileLogSink.swift b/Logbook/Source/Sink/FileLogSink.swift index 7255582..97a98ba 100644 --- a/Logbook/Source/Sink/FileLogSink.swift +++ b/Logbook/Source/Sink/FileLogSink.swift @@ -43,8 +43,6 @@ public final class FileLogSink: LogSink { let handle = try FileHandle(forUpdating: logFileURL) return handle } catch { - print("Failed to create FileHandle for URL \(logFileURL.absoluteString)") - print(error) return nil } }() @@ -94,21 +92,21 @@ extension FileLogSink { private func writeCacheToFile() { guard !lineCache.isEmpty else { return } - let bytes = bytesToRemove() - if bytes > 0 { - removeBytesFromFile(bytes) - } - let string = lineCache.reduce(into: "", { $0 += $1 }) - lineCache.removeAll() - // append to end of existing file guard let fileHandle = fileHandle, let data = string.data(using: .utf8) else { return } + lineCache.removeAll() - + // append to end of existing file fileHandle.seekToEndOfFile() fileHandle.write(data) + + // reduce file size if needed + let bytes = bytesToRemove() + if bytes > 0 { + removeBytesFromFile(bytes) + } } private func bytesToRemove() -> UInt64 { @@ -118,14 +116,10 @@ extension FileLogSink { let maxFileSize = maxFileSizeInKb * 1024 let toRemove: UInt64 = (fileSize > maxFileSize) ? fileSize - maxFileSize: 0 - - print("FileSize: \(fileSize)") - print("toRemove: \(toRemove)") return toRemove } catch { - print("Error: \(error)") return 0 } }