Skip to content

Commit 97add3f

Browse files
authored
Merge branch 'main' into disable-log-level-traits
2 parents 3100360 + cdaa5ac commit 97add3f

File tree

8 files changed

+57
-28
lines changed

8 files changed

+57
-28
lines changed

.github/workflows/main.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ jobs:
4949
name: Cxx interop
5050
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
5151

52-
static-sdk:
53-
name: Static SDK
52+
static-linux:
53+
name: Static Linux Swift SDK
5454
# Workaround https://github.com/nektos/act/issues/1875
5555
uses: apple/swift-nio/.github/workflows/static_sdk.yml@main
5656

@@ -62,6 +62,15 @@ jobs:
6262
build_scheme: swift-log-Package
6363
swift_test_enabled: true
6464

65+
wasm:
66+
name: Wasm Swift SDK
67+
uses: apple/swift-nio/.github/workflows/wasm_swift_sdk.yml@main
68+
69+
android:
70+
name: Android Swift SDK
71+
# Workaround https://github.com/nektos/act/issues/1875
72+
uses: apple/swift-nio/.github/workflows/android_swift_sdk.yml@main
73+
6574
release-builds:
6675
name: Release builds
6776
uses: apple/swift-nio/.github/workflows/release_builds.yml@main

.github/workflows/pull_request.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ jobs:
5555
name: Cxx interop
5656
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
5757

58-
static-sdk:
59-
name: Static SDK
58+
static-linux:
59+
name: Static Linux Swift SDK
6060
# Workaround https://github.com/nektos/act/issues/1875
6161
uses: apple/swift-nio/.github/workflows/static_sdk.yml@main
6262

@@ -68,6 +68,10 @@ jobs:
6868
build_scheme: swift-log-Package
6969
swift_test_enabled: true
7070

71+
android:
72+
name: Android Swift SDK
73+
# Workaround https://github.com/nektos/act/issues/1875
74+
uses: apple/swift-nio/.github/workflows/android_swift_sdk.yml@main
7175

7276
release-builds:
7377
name: Release builds
@@ -78,3 +82,7 @@ jobs:
7882
windows_6_2_enabled: true
7983
windows_nightly_next_enabled: true
8084
windows_nightly_main_enabled: true
85+
86+
wasm:
87+
name: Wasm Swift SDK
88+
uses: apple/swift-nio/.github/workflows/wasm_swift_sdk.yml@main

Sources/InMemoryLogging/InMemoryLogHandler.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Logging
16-
import Synchronization
1716

1817
/// A custom log handler which just collects logs into memory.
1918
/// You can then retrieve an array of those log entries.
@@ -35,7 +34,6 @@ import Synchronization
3534
/// let logEntries = logger.entries
3635
/// ```
3736
///
38-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
3937
public struct InMemoryLogHandler: LogHandler {
4038
public var metadata: Logger.Metadata = [:]
4139
public var metadataProvider: Logger.MetadataProvider?
@@ -58,12 +56,13 @@ public struct InMemoryLogHandler: LogHandler {
5856
}
5957
}
6058

61-
private final class LogStore: Sendable {
62-
private let logs: Mutex<[Entry]> = .init([])
59+
private final class LogStore: @unchecked Sendable {
60+
private var _entries: [Entry] = []
61+
private let lock = Lock()
6362

6463
fileprivate func append(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata) {
65-
self.logs.withLock {
66-
$0.append(
64+
self.lock.withLockVoid {
65+
self._entries.append(
6766
Entry(
6867
level: level,
6968
message: message,
@@ -74,13 +73,13 @@ public struct InMemoryLogHandler: LogHandler {
7473
}
7574

7675
fileprivate func clear() {
77-
self.logs.withLock {
78-
$0.removeAll()
76+
self.lock.withLockVoid {
77+
_entries.removeAll()
7978
}
8079
}
8180

8281
var entries: [Entry] {
83-
self.logs.withLock { $0 }
82+
self.lock.withLock { self._entries }
8483
}
8584
}
8685

proposals/0001-metadata-providers.md renamed to Sources/Logging/Docs.docc/Proposals/SLG-0001-metadata-providers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Metadata Providers
22

3-
Authors: [Moritz Lang](https://github.com/slashmo), [Konrad 'ktoso' Malawski](https://github.com/ktoso)
3+
While global metadata attributes may be manually set on a `LogHandler` level, there's currently no way of reliably providing contextual, automatically propagated, metadata when logging with swift-log.
44

5-
## Introduction
5+
## Authors
66

7-
While global metadata attributes may be manually set on a `LogHandler` level, there's currently no way of reliably providing contextual, automatically propagated, metadata when logging with swift-log.
7+
[Moritz Lang](https://github.com/slashmo), [Konrad 'ktoso' Malawski](https://github.com/ktoso)
88

99
## Motivation
1010

Sources/Logging/Locks.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import Musl
4848
/// of lock is safe to use with `libpthread`-based threading models, such as the
4949
/// one used by NIO. On Windows, the lock is based on the substantially similar
5050
/// `SRWLOCK` type.
51-
internal final class Lock: @unchecked Sendable {
51+
package final class Lock: @unchecked Sendable {
5252
#if canImport(WASILibc)
5353
// WASILibc is single threaded, provides no locks
5454
#elseif os(Windows)
@@ -60,7 +60,7 @@ internal final class Lock: @unchecked Sendable {
6060
#endif
6161

6262
/// Create a new lock.
63-
public init() {
63+
package init() {
6464
#if canImport(WASILibc)
6565
// WASILibc is single threaded, provides no locks
6666
#elseif os(Windows)
@@ -92,7 +92,7 @@ internal final class Lock: @unchecked Sendable {
9292
///
9393
/// Whenever possible, consider using `withLock` instead of this method and
9494
/// `unlock`, to simplify lock handling.
95-
public func lock() {
95+
package func lock() {
9696
#if canImport(WASILibc)
9797
// WASILibc is single threaded, provides no locks
9898
#elseif os(Windows)
@@ -107,7 +107,7 @@ internal final class Lock: @unchecked Sendable {
107107
///
108108
/// Whenever possible, consider using `withLock` instead of this method and
109109
/// `lock`, to simplify lock handling.
110-
public func unlock() {
110+
package func unlock() {
111111
#if canImport(WASILibc)
112112
// WASILibc is single threaded, provides no locks
113113
#elseif os(Windows)
@@ -129,7 +129,7 @@ extension Lock {
129129
/// - Parameter body: The block to execute while holding the lock.
130130
/// - Returns: The value returned by the block.
131131
@inlinable
132-
internal func withLock<T>(_ body: () throws -> T) rethrows -> T {
132+
package func withLock<T>(_ body: () throws -> T) rethrows -> T {
133133
self.lock()
134134
defer {
135135
self.unlock()
@@ -139,7 +139,7 @@ extension Lock {
139139

140140
// specialise Void return (for performance)
141141
@inlinable
142-
internal func withLockVoid(_ body: () throws -> Void) rethrows {
142+
package func withLockVoid(_ body: () throws -> Void) rethrows {
143143
try self.withLock(body)
144144
}
145145
}

Sources/Logging/Logging.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import CRT
1919
#elseif canImport(Glibc)
2020
@preconcurrency import Glibc
2121
#elseif canImport(Android)
22-
import Android
22+
@preconcurrency import Android
2323
#elseif canImport(Musl)
2424
import Musl
2525
#elseif canImport(WASILibc)
@@ -1102,6 +1102,19 @@ extension Logger.Level: Comparable {
11021102
}
11031103
}
11041104

1105+
extension Logger.Level: CustomStringConvertible, LosslessStringConvertible {
1106+
/// A textual representation of the log level.
1107+
public var description: String {
1108+
self.rawValue
1109+
}
1110+
1111+
/// Creates a log level from its textual representation.
1112+
/// - Parameter description: A textual representation of the log level, case insensitive.
1113+
public init?(_ description: String) {
1114+
self.init(rawValue: description.lowercased())
1115+
}
1116+
}
1117+
11051118
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
11061119
// https://bugs.swift.org/browse/SR-9687
11071120
// Then we could write it as follows and it would work under Swift 5 and not only 4 as it does currently:

Tests/InMemoryLoggingTests/InMemoryLogHandlerTests.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Testing
1818

1919
struct InMemoryLogHandlerTests {
2020
@Test
21-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
2221
func collectsLogs() {
2322
let (logHandler, logger) = self.makeTestLogger()
2423
logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]])
@@ -35,7 +34,6 @@ struct InMemoryLogHandlerTests {
3534
}
3635

3736
@Test
38-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
3937
func metadataFromLoggerEndsUpInEntry() {
4038
var (logHandler, logger) = self.makeTestLogger()
4139
logger[metadataKey: "test"] = "value"
@@ -55,7 +53,6 @@ struct InMemoryLogHandlerTests {
5553
}
5654

5755
@Test
58-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
5956
func moreSpecificMetadataOverridesGlobal() {
6057
let testProvider = Logger.MetadataProvider {
6158
["a": "1", "b": "1", "c": "1"]
@@ -73,7 +70,6 @@ struct InMemoryLogHandlerTests {
7370
}
7471

7572
@Test
76-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
7773
func clear() {
7874
let (logHandler, logger) = self.makeTestLogger()
7975
logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]])
@@ -88,7 +84,6 @@ struct InMemoryLogHandlerTests {
8884
)
8985
}
9086

91-
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
9287
private func makeTestLogger(metadataProvider: Logger.MetadataProvider? = nil) -> (InMemoryLogHandler, Logger) {
9388
var logHandler = InMemoryLogHandler()
9489
logHandler.metadataProvider = metadataProvider

Tests/LoggingTests/LoggingTest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ struct LoggingTest {
957957
#expect(Logger.Level.error < Logger.Level.critical)
958958
}
959959

960+
@Test(arguments: Logger.Level.allCases) func logLevelDescription(level: Logger.Level) {
961+
#expect(level.description == level.rawValue)
962+
#expect(Logger.Level(level.rawValue.uppercased()) == level)
963+
}
964+
960965
final class InterceptStream: TextOutputStream {
961966
var interceptedText: String?
962967
var strings = [String]()

0 commit comments

Comments
 (0)