diff --git a/Sources/InMemoryLogging/InMemoryLogHandler.swift b/Sources/InMemoryLogging/InMemoryLogHandler.swift index c320042..99645c6 100644 --- a/Sources/InMemoryLogging/InMemoryLogHandler.swift +++ b/Sources/InMemoryLogging/InMemoryLogHandler.swift @@ -13,7 +13,6 @@ //===----------------------------------------------------------------------===// import Logging -import Synchronization /// A custom log handler which just collects logs into memory. /// You can then retrieve an array of those log entries. @@ -35,7 +34,6 @@ import Synchronization /// let logEntries = logger.entries /// ``` /// -@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) public struct InMemoryLogHandler: LogHandler { public var metadata: Logger.Metadata = [:] public var metadataProvider: Logger.MetadataProvider? @@ -58,12 +56,13 @@ public struct InMemoryLogHandler: LogHandler { } } - private final class LogStore: Sendable { - private let logs: Mutex<[Entry]> = .init([]) + private final class LogStore: @unchecked Sendable { + private var _entries: [Entry] = [] + private let lock = Lock() fileprivate func append(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata) { - self.logs.withLock { - $0.append( + self.lock.withLockVoid { + self._entries.append( Entry( level: level, message: message, @@ -74,13 +73,13 @@ public struct InMemoryLogHandler: LogHandler { } fileprivate func clear() { - self.logs.withLock { - $0.removeAll() + self.lock.withLockVoid { + _entries.removeAll() } } var entries: [Entry] { - self.logs.withLock { $0 } + self.lock.withLock { self._entries } } } diff --git a/Sources/Logging/Locks.swift b/Sources/Logging/Locks.swift index 1cc81fe..cfc1789 100644 --- a/Sources/Logging/Locks.swift +++ b/Sources/Logging/Locks.swift @@ -48,7 +48,7 @@ import Musl /// of lock is safe to use with `libpthread`-based threading models, such as the /// one used by NIO. On Windows, the lock is based on the substantially similar /// `SRWLOCK` type. -internal final class Lock: @unchecked Sendable { +package final class Lock: @unchecked Sendable { #if canImport(WASILibc) // WASILibc is single threaded, provides no locks #elseif os(Windows) @@ -60,7 +60,7 @@ internal final class Lock: @unchecked Sendable { #endif /// Create a new lock. - public init() { + package init() { #if canImport(WASILibc) // WASILibc is single threaded, provides no locks #elseif os(Windows) @@ -92,7 +92,7 @@ internal final class Lock: @unchecked Sendable { /// /// Whenever possible, consider using `withLock` instead of this method and /// `unlock`, to simplify lock handling. - public func lock() { + package func lock() { #if canImport(WASILibc) // WASILibc is single threaded, provides no locks #elseif os(Windows) @@ -107,7 +107,7 @@ internal final class Lock: @unchecked Sendable { /// /// Whenever possible, consider using `withLock` instead of this method and /// `lock`, to simplify lock handling. - public func unlock() { + package func unlock() { #if canImport(WASILibc) // WASILibc is single threaded, provides no locks #elseif os(Windows) @@ -129,7 +129,7 @@ extension Lock { /// - Parameter body: The block to execute while holding the lock. /// - Returns: The value returned by the block. @inlinable - internal func withLock(_ body: () throws -> T) rethrows -> T { + package func withLock(_ body: () throws -> T) rethrows -> T { self.lock() defer { self.unlock() @@ -139,7 +139,7 @@ extension Lock { // specialise Void return (for performance) @inlinable - internal func withLockVoid(_ body: () throws -> Void) rethrows { + package func withLockVoid(_ body: () throws -> Void) rethrows { try self.withLock(body) } } diff --git a/Tests/InMemoryLoggingTests/InMemoryLogHandlerTests.swift b/Tests/InMemoryLoggingTests/InMemoryLogHandlerTests.swift index 57d8fde..fd600cc 100644 --- a/Tests/InMemoryLoggingTests/InMemoryLogHandlerTests.swift +++ b/Tests/InMemoryLoggingTests/InMemoryLogHandlerTests.swift @@ -18,7 +18,6 @@ import Testing struct InMemoryLogHandlerTests { @Test - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) func collectsLogs() { let (logHandler, logger) = self.makeTestLogger() logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]]) @@ -35,7 +34,6 @@ struct InMemoryLogHandlerTests { } @Test - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) func metadataFromLoggerEndsUpInEntry() { var (logHandler, logger) = self.makeTestLogger() logger[metadataKey: "test"] = "value" @@ -55,7 +53,6 @@ struct InMemoryLogHandlerTests { } @Test - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) func moreSpecificMetadataOverridesGlobal() { let testProvider = Logger.MetadataProvider { ["a": "1", "b": "1", "c": "1"] @@ -73,7 +70,6 @@ struct InMemoryLogHandlerTests { } @Test - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) func clear() { let (logHandler, logger) = self.makeTestLogger() logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]]) @@ -88,7 +84,6 @@ struct InMemoryLogHandlerTests { ) } - @available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *) private func makeTestLogger(metadataProvider: Logger.MetadataProvider? = nil) -> (InMemoryLogHandler, Logger) { var logHandler = InMemoryLogHandler() logHandler.metadataProvider = metadataProvider