tunnelkit/TunnelKit/Sources/AppExtension/MemoryDestination.swift

60 lines
1.5 KiB
Swift
Raw Normal View History

2018-08-23 08:19:25 +00:00
//
// MemoryDestination.swift
// TunnelKit
2018-08-23 08:19:25 +00:00
//
// Created by Davide De Rosa on 7/26/17.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import Foundation
import SwiftyBeaver
class MemoryDestination: BaseDestination, CustomStringConvertible {
private let queue = DispatchQueue(label: "MemoryDestination")
private var buffer: [String] = []
var maxLines: Int?
override init() {
super.init()
asynchronously = false
}
func start(with existing: [String]) {
queue.sync {
buffer = existing
}
}
func flush(to: UserDefaults, with key: String) {
queue.sync {
to.set(buffer, forKey: key)
}
to.synchronize()
}
var description: String {
return queue.sync {
return buffer.joined(separator: "\n")
}
}
// MARK: BaseDestination
override func send(_ level: SwiftyBeaver.Level, msg: String, thread: String, file: String, function: String, line: Int, context: Any?) -> String? {
guard let message = super.send(level, msg: msg, thread: thread, file: file, function: function, line: line) else {
return nil
}
queue.sync {
buffer.append(message)
if let maxLines = maxLines {
while (buffer.count > maxLines) {
buffer.removeFirst()
}
}
}
return message
}
}