From 1c657b99b9fb206fc22f530662f1178381485c40 Mon Sep 17 00:00:00 2001 From: Peter Adams Date: Mon, 10 Jun 2024 13:24:15 +0100 Subject: [PATCH] Use Set to spot duplicated log handler warnings. 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. --- Sources/Logging/Logging.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index fdb1f138..7f4f5245 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -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() func warnOnceLogHandlerNotSupportedMetadataProvider(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 } } }