Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
swieland committed Nov 28, 2019
1 parent 2684f3e commit 599b9ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
44 changes: 23 additions & 21 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
22 changes: 8 additions & 14 deletions Logbook/Source/Sink/FileLogSink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down

0 comments on commit 599b9ca

Please sign in to comment.