Use Set to spot duplicated log handler warnings. (#306)

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.
This commit is contained in:
Peter Adams authored and GitHub committed 2024-06-10 14:17:43 +01:00
1 parent 23517234d5
commit 0f170b4764
1 file changed
+3 -7
+3 -7
View File
@@ -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
}
}
}