Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Sources/TimeIt/TimeIt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ public class TimeIt {
}
}

public static func access(log: String, completion: @escaping ([String]) -> Void) {
queue.async {
if let logEntries = storage[log], let start = logEntries.first?.stamp {
let logDescriptions = describe(logEntries: logEntries, started: start, titled: log)
completion(logDescriptions)
}
}
}

public static func complete(log: String, completion: @escaping ([String]) -> Void) {
queue.async {
if let logEntries = storage.removeValue(forKey: log), let start = logEntries.first?.stamp {
let logs = logEntries.map { logEntry -> String in
let interval = String(format: "%.4f", logEntry.stamp.timeIntervalSince(start))
return "[\(log)] \(formatter.string(from: logEntry.stamp)) \(interval)secs \(logEntry.entry)"
}
let logs = describe(logEntries: logEntries, started: start, titled: log)
completion(logs)
}
}
}

private static func describe(logEntries: [LogEntry], started start: Date, titled title: String) -> [String] {
let logs = logEntries.map { logEntry -> String in
let interval = String(format: "%.4f", logEntry.stamp.timeIntervalSince(start))
return "[\(title)] \(formatter.string(from: logEntry.stamp)) \(interval)secs \(logEntry.entry)"
}
return logs
}
}
37 changes: 31 additions & 6 deletions Tests/TimeItTests/TimeItTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@ import TimeIt

final class TimeItTests: XCTestCase {

func testTimeIt() {
static var allTests = [
("testTimeItAccesses", testTimeItAccesses),
("testTimeItCompletes", testTimeItCompletes),
]

func testTimeItAccesses() {
TimeIt.log("test", entry: "first")
TimeIt.log("test", entry: "second")

let e = expectation(description: "logs in correct order")

TimeIt.complete(log: "test") { logs in
TimeIt.access(log: "test") { logs in
guard logs[0].contains("first") else { XCTFail("first log message not correct"); return }
guard logs[1].contains("second") else { XCTFail("second log message not correct"); return }
e.fulfill()
}

TimeIt.log("test", entry: "third")

TimeIt.access(log: "test") { logs in
guard logs[2].contains("third") else { XCTFail("third log message not correct"); return }
e.fulfill()
}
waitForExpectations(timeout: 0.5)
}

static var allTests = [
("testExample", testTimeIt),
]
func testTimeItCompletes() {
TimeIt.log("test", entry: "first")
TimeIt.log("test", entry: "second")

let e = expectation(description: "logs in correct order")

TimeIt.complete(log: "test") { logs in
guard logs[0].contains("first") else { XCTFail("first log message not correct"); return }
guard logs[1].contains("second") else { XCTFail("second log message not correct"); return }
}

TimeIt.log("test", entry: "second")

TimeIt.complete(log: "test") { logs in
guard logs[0].contains("third") else { e.fulfill(); return }
}
waitForExpectations(timeout: 0.5)
}
}