Skip to content

Commit 7376cca

Browse files
committed
Make the default logger subsystem configurable
1 parent 92fbe48 commit 7376cca

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var targets: [Target] = [
7272
name: "LanguageServerProtocolTests",
7373
dependencies: [
7474
"LanguageServerProtocol",
75+
"SKLogging",
7576
"ToolsProtocolsTestSupport",
7677
],
7778
swiftSettings: globalSwiftSettings
@@ -95,6 +96,7 @@ var targets: [Target] = [
9596
name: "LanguageServerProtocolTransportTests",
9697
dependencies: [
9798
"LanguageServerProtocolTransport",
99+
"SKLogging",
98100
"ToolsProtocolsTestSupport",
99101
],
100102
swiftSettings: globalSwiftSettings
@@ -119,7 +121,6 @@ var targets: [Target] = [
119121
],
120122
exclude: ["CMakeLists.txt"],
121123
swiftSettings: globalSwiftSettings + lspLoggingSwiftSettings + [
122-
.define("SKLOGGING_FOR_PLUGIN"),
123124
.unsafeFlags([
124125
"-module-alias", "SwiftExtensions=_SwiftExtensionsForPlugin",
125126
]),

Sources/SKLogging/LoggingScope.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
@_spi(SourceKitLSP) import SwiftExtensions
15+
16+
public final class LoggingScope {
17+
18+
/// The name of the default logging subsystem if no task-local value is set.
19+
fileprivate static let defaultSubsystem: ThreadSafeBox<String?> = .init(initialValue: nil)
1420

15-
@_spi(SourceKitLSP) public final class LoggingScope {
1621
/// The name of the current logging subsystem or `nil` if no logging scope is set.
1722
@TaskLocal fileprivate static var _subsystem: String?
1823

@@ -21,17 +26,23 @@ import Foundation
2126

2227
/// The name of the current logging subsystem.
2328
@_spi(SourceKitLSP) public static var subsystem: String {
24-
#if SKLOGGING_FOR_PLUGIN
25-
return _subsystem ?? "org.swift.sourcekit-lsp.plugin"
26-
#else
27-
return _subsystem ?? "org.swift.sourcekit-lsp"
28-
#endif
29+
if let _subsystem {
30+
return _subsystem
31+
} else if let defaultSubsystem = defaultSubsystem.value {
32+
return defaultSubsystem
33+
} else {
34+
fatalError("SKLogging: default subsystem was not configured before first use")
35+
}
2936
}
3037

3138
/// The name of the current logging scope.
3239
@_spi(SourceKitLSP) public static var scope: String {
3340
return _scope ?? "default"
3441
}
42+
43+
public static func configureDefaultLoggingSubsystem(_ subsystem: String) {
44+
LoggingScope.defaultSubsystem.withLock { $0 = subsystem }
45+
}
3546
}
3647

3748
/// Logs all messages created from the operation to the given subsystem.

Tests/LanguageServerProtocolTests/ConnectionTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
import LanguageServerProtocol
1414
import ToolsProtocolsTestSupport
15+
import SKLogging
1516
import XCTest
1617

1718
class ConnectionTests: XCTestCase {
1819

1920
var connection: TestLocalConnection! = nil
2021

2122
override func setUp() {
23+
LoggingScope.configureDefaultLoggingSubsystem("org.swift.swift-tools-protocols-tests")
2224
connection = TestLocalConnection(allowUnexpectedNotification: false)
2325
}
2426

Tests/LanguageServerProtocolTransportTests/ConnectionPerfTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
import LanguageServerProtocolTransport
1414
import ToolsProtocolsTestSupport
15+
import SKLogging
1516
import XCTest
1617

1718
class ConnectionPerfTests: PerfTestCase {
1819

1920
var connection: TestJSONRPCConnection! = nil
2021

2122
override func setUp() {
23+
LoggingScope.configureDefaultLoggingSubsystem("org.swift.swift-tools-protocols-tests")
2224
connection = TestJSONRPCConnection()
2325
}
2426

Tests/SKLoggingTests/LoggingTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ private func assertLogging(
7070
}
7171

7272
final class LoggingTests: XCTestCase {
73+
74+
override func setUp() async throws {
75+
LoggingScope.configureDefaultLoggingSubsystem("org.swift.swift-tools-protocols-tests")
76+
}
77+
7378
func testLoggingFormat() async throws {
7479
let expectation = self.expectation(description: "message logged")
7580
// nonisolated(unsafe) because we only have a single call to `logger.log` and that cannot race.
@@ -85,7 +90,7 @@ final class LoggingTests: XCTestCase {
8590
logger.log(level: .error, "my message")
8691
try await fulfillmentOfOrThrow(expectation)
8792
XCTAssert(
88-
message.starts(with: "[org.swift.sourcekit-lsp:test] error"),
93+
message.starts(with: "[org.swift.swift-tools-protocols-tests:test] error"),
8994
"Message did not have expected header. Received \n\(message)"
9095
)
9196
XCTAssert(message.hasSuffix("\nmy message\n---"), "Message did not have expected body. Received \n\(message)")

Tests/SwiftExtensionsTests/AsyncUtilsTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import Android
2222
#endif
2323

2424
final class AsyncUtilsTests: XCTestCase {
25+
override func setUp() async throws {
26+
LoggingScope.configureDefaultLoggingSubsystem("org.swift.swift-tools-protocols-tests")
27+
}
28+
2529
func testWithTimeout() async throws {
2630
let expectation = self.expectation(description: "withTimeout body finished")
2731
await assertThrowsError(

0 commit comments

Comments
 (0)