Make InMemoryLogging available on all platforms (#394)

Make InMemoryLogging available on all platforms

### Motivation:

Users might want to adopt the in memory logger on lower platform
versions.

### Modifications:

Remove availability annotations by using swift-logs own Lock instead of
Mutex

### Result:

In memory logger can be used on all platforms that swift-log supports
This commit is contained in:
hamzahrmalik authored and GitHub committed 2025-12-04 11:52:31 +00:00
1 parent 63eaca6f4d
commit b1fa4ef41f
3 files changed
+14 -20

No files matched your search

@@ -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 }
}
}
+6 -6
View File
@@ -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<T>(_ body: () throws -> T) rethrows -> T {
package func withLock<T>(_ 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)
}
}
@@ -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