diff --git a/Sources/ExampleUsage/GlobalLoggerBasedSystem.swift b/Sources/ExampleUsage/GlobalLoggerBasedSystem.swift index 06b8c6d..bf7191c 100644 --- a/Sources/ExampleUsage/GlobalLoggerBasedSystem.swift +++ b/Sources/ExampleUsage/GlobalLoggerBasedSystem.swift @@ -6,7 +6,21 @@ import ExampleImplementation import Foundation import Logging -var logger = Logging.make("GlobalLogger") +// since this example mutates the global logger, we need to lock around it +var _logger = Logging.make("GlobalLogger") +var lock = NSLock() +var logger: Logger { + get { + return lock.withLock { + _logger + } + } + set { + lock.withLock { + _logger = newValue + } + } +} // this is a contrived example of a system that shares one global logger enum GlobalLoggerBasedSystem { @@ -80,3 +94,13 @@ enum GlobalLoggerBasedSystem { } } } + +private extension NSLock { + func withLock(_ body: () -> T) -> T { + self.lock() + defer { + self.unlock() + } + return body() + } +}