Update SwiftyBeaver for MemoryDestination

See for reference:

- https://github.com/pia-foss/tunnel-apple/pull/15
- https://github.com/SwiftyBeaver/SwiftyBeaver/pull/299
This commit is contained in:
Davide De Rosa 2018-09-23 11:02:20 +02:00
parent b2dba4822a
commit 58726a67d7
2 changed files with 13 additions and 16 deletions

View File

@ -1,6 +1,6 @@
PODS: PODS:
- OpenSSL-Apple (1.1.0h) - OpenSSL-Apple (1.1.0h)
- SwiftyBeaver (1.6.0) - SwiftyBeaver (1.6.1)
DEPENDENCIES: DEPENDENCIES:
- OpenSSL-Apple (~> 1.1.0h) - OpenSSL-Apple (~> 1.1.0h)
@ -13,7 +13,7 @@ SPEC REPOS:
SPEC CHECKSUMS: SPEC CHECKSUMS:
OpenSSL-Apple: cd153d705ef350eb834ae7ff5f21f792b51ed208 OpenSSL-Apple: cd153d705ef350eb834ae7ff5f21f792b51ed208
SwiftyBeaver: e45759613e50b522b0e6f53b1f0f14389b45ca34 SwiftyBeaver: ccfcdf85a04d429f1633f668650b0ce8020bda3a
PODFILE CHECKSUM: db783bdfb06f72df39d3c99df20aafdb51e0f7c6 PODFILE CHECKSUM: db783bdfb06f72df39d3c99df20aafdb51e0f7c6

View File

@ -39,8 +39,6 @@ import Foundation
import SwiftyBeaver import SwiftyBeaver
class MemoryDestination: BaseDestination, CustomStringConvertible { class MemoryDestination: BaseDestination, CustomStringConvertible {
private let queue = DispatchQueue(label: "MemoryDestination")
private var buffer: [String] = [] private var buffer: [String] = []
var maxLines: Int? var maxLines: Int?
@ -51,38 +49,37 @@ class MemoryDestination: BaseDestination, CustomStringConvertible {
} }
func start(with existing: [String]) { func start(with existing: [String]) {
queue.sync { execute(synchronously: true) {
buffer = existing self.buffer = existing
} }
} }
func flush(to: UserDefaults, with key: String) { func flush(to: UserDefaults, with key: String) {
queue.sync { execute(synchronously: true) {
to.set(buffer, forKey: key) to.set(self.buffer, forKey: key)
} }
to.synchronize() to.synchronize()
} }
var description: String { var description: String {
return queue.sync { return executeSynchronously {
return buffer.joined(separator: "\n") return self.buffer.joined(separator: "\n")
} }
} }
// MARK: BaseDestination // MARK: BaseDestination
// XXX: executed in SwiftyBeaver queue. DO NOT invoke execute* here (sync in sync would crash otherwise)
override func send(_ level: SwiftyBeaver.Level, msg: String, thread: String, file: String, function: String, line: Int, context: Any?) -> String? { 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 { guard let message = super.send(level, msg: msg, thread: thread, file: file, function: function, line: line) else {
return nil return nil
} }
queue.sync {
buffer.append(message) buffer.append(message)
if let maxLines = maxLines { if let maxLines = maxLines {
while (buffer.count > maxLines) { while buffer.count > maxLines {
buffer.removeFirst() buffer.removeFirst()
} }
} }
}
return message return message
} }
} }