Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Created April 22, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlguy99/9e4fa6ad566f51bdcbc33e9c247f2053 to your computer and use it in GitHub Desktop.
Save perlguy99/9e4fa6ad566f51bdcbc33e9c247f2053 to your computer and use it in GitHub Desktop.
TimeLogger for Combine Debugging
// 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