Skip to content

Commit

Permalink
Scope: disabled category fix
Browse files Browse the repository at this point in the history
  • Loading branch information
khvorost-dish committed Oct 18, 2023
1 parent c88ed11 commit 8081237
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Sources/DLog/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func synchronized<T : AnyObject, U>(_ obj: T, closure: () -> U) -> U {
}

@propertyWrapper
class Atomic<T> {
public class Atomic<T> {
private var value: T

init(wrappedValue value: T) {
public init(wrappedValue value: T) {
self.value = value
}

var wrappedValue: T {
public var wrappedValue: T {
get {
synchronized(self) { value }
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/DLog/DLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ public class DLog: LogProtocol {

func enter(scope: LogScope) {
guard let out = output else { return }
out.scopeEnter(scope: scope)
ScopeStack.shared.append(scope) {
out.scopeEnter(scope: scope)
}
}

func leave(scope: LogScope) {
guard let out = output else { return }
out.scopeLeave(scope: scope)
ScopeStack.shared.remove(scope) {
out.scopeLeave(scope: scope)
}
}

// Interval
Expand Down
19 changes: 8 additions & 11 deletions Sources/DLog/LogScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ class ScopeStack {
/// Scope provides a mechanism for grouping log messages.
///
public class LogScope: LogProtocol {
var time = Date()
@Atomic var time = Date()

var os_state = os_activity_scope_state_s()

/// A global level in the stack.
@objc
public internal(set) var level: Int = 0

/// A time duration.
public private(set) var duration: TimeInterval = 0
@Atomic public private(set) var duration: TimeInterval = 0

/// Scope name.
@objc
Expand All @@ -101,11 +102,9 @@ public class LogScope: LogProtocol {
///
@objc
public func enter() {
ScopeStack.shared.append(self) {
time = Date()
duration = 0
logger.enter(scope: self)
}
time = Date()
duration = 0
logger.enter(scope: self)
}

/// Finish a scope.
Expand All @@ -123,9 +122,7 @@ public class LogScope: LogProtocol {
///
@objc
public func leave() {
ScopeStack.shared.remove(self) {
duration = -time.timeIntervalSinceNow
logger.leave(scope: self)
}
duration = -time.timeIntervalSinceNow
logger.leave(scope: self)
}
}
13 changes: 13 additions & 0 deletions Tests/DLogTests/DLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,19 @@ final class ScopeTests: XCTestCase {
scope.leave()
XCTAssert(0.25 <= scope.duration)
}

func test_disabled_category() {
let logger = DLog()
let category = DLog.disabled

category.scope("scope1") { scope1 in
scope1.scope("scope2") { scope2 in
logger.scope("scope3") { scope3 in
XCTAssert(scope3.debug("log")?.match(#"\#(CategoryTag) \#(DebugTag) \#(Location) log"#) == true)
}
}
}
}
}

final class TraceTests: XCTestCase {
Expand Down

0 comments on commit 8081237

Please sign in to comment.