Created
April 22, 2020 15:11
-
-
Save perlguy99/9e4fa6ad566f51bdcbc33e9c247f2053 to your computer and use it in GitHub Desktop.
TimeLogger for Combine Debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: This snipped comes from Ray Wenderlich's "Combine" book. | |
// https://store.raywenderlich.com/products/combine-asynchronous-programming-with-swift | |
class TimeLogger: TextOutputStream { | |
private var previous = Date() | |
private let formatter = NumberFormatter() | |
init() { | |
formatter.maximumFractionDigits = 5 | |
formatter.minimumFractionDigits = 5 | |
} | |
func write(_ string: String) { | |
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewLines) | |
guard !trimmed.isEmpty else { return } | |
let now = Date() | |
print("+\(formatter.string(for: now.timeIntervalSince(previous))!)s: \(string)") | |
previous = now | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment