Skip to content

Commit

Permalink
Use Set to spot duplicated log handler warnings. (#306)
Browse files Browse the repository at this point in the history
Motivation:

Feels like a better fit for the problem when a dictionary of bools.

Modifications:

Convert storage to set.

Result:

Fewer lines of code, less waste.
  • Loading branch information
PeterAdams-A authored Jun 10, 2024
1 parent 2351723 commit 0f170b4
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1347,17 +1347,13 @@ extension Logger.MetadataValue: ExpressibleByArrayLiteral {
/// Contains state to manage all kinds of "warn only once" warnings which the logging system may want to issue.
private final class WarnOnceBox {
private let lock: Lock = Lock()
private var warnOnceLogHandlerNotSupportedMetadataProviderPerType: [ObjectIdentifier: Bool] = [:]
private var warnOnceLogHandlerNotSupportedMetadataProviderPerType = Set<ObjectIdentifier>()

func warnOnceLogHandlerNotSupportedMetadataProvider<Handler: LogHandler>(type: Handler.Type) -> Bool {
self.lock.withLock {
let id = ObjectIdentifier(type)
if warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] ?? false {
return false // don't warn, it was already warned about
} else {
warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] = true
return true // warn about this handler type, it is the first time we encountered it
}
let (inserted, _) = warnOnceLogHandlerNotSupportedMetadataProviderPerType.insert(id)
return inserted // warn about this handler type, it is the first time we encountered it
}
}
}
Expand Down

0 comments on commit 0f170b4

Please sign in to comment.