Skip to content

Commit

Permalink
Time interval to time with abbreviations e.g. 1m 1.234s
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Apr 14, 2022
1 parent bcb0435 commit 9e2fa62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions Sources/DLog/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,25 @@ public class Text : LogOutput {
private static let dateComponentsFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .abbreviated
return formatter
}()

static func stringFromTime(interval: TimeInterval) -> String {
let ms = Int(interval.truncatingRemainder(dividingBy: 1) * 1000)
return dateComponentsFormatter.string(from: interval)! + ".\(ms)"
let time = dateComponentsFormatter.string(from: interval)!

let fractation = String(format: "%.3f", interval).split(separator: ".")[1]
if fractation != "000" {
let ms = ".\(fractation)s"
if let index = time.firstIndex(of: "s") {
let range = index..<time.index(after: index)
return time.replacingCharacters(in: range, with: ms)
}
else {
return "\(time) 0\(ms)"
}
}
return time
}

private func logPrefix(items: [(LogOptions, () -> String)], options: LogOptions) -> String {
Expand Down
4 changes: 2 additions & 2 deletions Tests/DLogTests/DLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ let FaultTag = #"\[FAULT\]"#
let IntervalTag = #"\[INTERVAL\]"#

let Location = "<DLogTests.swift:[0-9]+>"
let SECS = #"[0-9]+\.[0-9]{3}"#
let SECS = #"[0-9]+\.[0-9]{3}s"#
let Interval = #"\{ duration: \#(SECS), average: \#(SECS) \}"#

fileprivate func testAll(_ logger: LogProtocol, categoryTag: String = CategoryTag) {
Expand All @@ -158,7 +158,7 @@ fileprivate func testAll(_ logger: LogProtocol, categoryTag: String = CategoryTa

XCTAssert(logger.fault("fault")?.match(#"\#(categoryTag)\#(padding)\#(FaultTag) \#(Location) fault"#) == true)

XCTAssert(read_stdout { logger.scope("scope") { _ in delay() } }?.match(#"\#(categoryTag)\#(padding)└ \[scope\] \(0\.[0-9]{3}\)"#) == true)
XCTAssert(read_stdout { logger.scope("scope") { _ in delay() } }?.match(#"\#(categoryTag)\#(padding)└ \[scope\] \(\#(SECS)\)"#) == true)
XCTAssert(read_stdout { logger.interval("signpost") { delay() } }?.match(#"\#(categoryTag)\#(padding)\[INTERVAL\] \#(Location) signpost: \#(Interval)"#) == true)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/DLogTestsObjC/DLogTestsObjC.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ - (void)test_Scope {
testAll(scope, nil);
[scope leave];
});
XCTAssertTrue([text match:@"\\[Scope 2\\] \\(\\d\\.\\d\\)"]);
XCTAssertTrue([text match:@"\\[Scope 2\\] \\(0\\.\\d+s\\)"]);
}

- (void)test_Interval {
Expand Down

0 comments on commit 9e2fa62

Please sign in to comment.