Skip to content

Commit 12b38fe

Browse files
committed
Add logging for requests handled by the SourceKit plugin
1 parent 45be280 commit 12b38fe

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ var targets: [Target] = [
334334
swiftSettings: globalSwiftSettings + lspLoggingSwiftSettings + [
335335
// We can't depend on swift-crypto in the plugin because we can't module-alias it due to https://github.com/swiftlang/swift-package-manager/issues/8119
336336
.define("NO_CRYPTO_DEPENDENCY"),
337+
.define("SKLOGGING_FOR_PLUGIN"),
337338
.unsafeFlags([
338339
"-module-alias", "SwiftExtensions=SwiftExtensionsForPlugin",
339340
]),

Sources/SKLogging/LoggingScope.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ package final class LoggingScope {
2121

2222
/// The name of the current logging subsystem.
2323
package static var subsystem: String {
24+
#if SKLOGGING_FOR_PLUGIN
25+
return _subsystem ?? "org.swift.sourcekit-lsp.plugin"
26+
#else
2427
return _subsystem ?? "org.swift.sourcekit-lsp"
28+
#endif
2529
}
2630

2731
/// The name of the current logging scope.

Sources/SwiftSourceKitPlugin/Plugin.swift

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

1313
import Foundation
14+
import SKLogging
1415
import SourceKitD
1516
import SwiftExtensions
1617
import SwiftSourceKitPluginCommon
@@ -62,14 +63,31 @@ final class RequestHandler: Sendable {
6263
func produceResult(
6364
body: @escaping @Sendable () async throws -> SKDResponseDictionaryBuilder
6465
) -> HandleRequestResult {
65-
requestHandlingQueue.async {
66-
do {
67-
receiver(try await body().response)
68-
} catch {
69-
receiver(SKDResponse.from(error: error, sourcekitd: self.sourcekitd))
66+
withLoggingScope("request-\(handle?.numericValue ?? 0 % 100)") {
67+
let start = Date()
68+
logger.debug(
69+
"""
70+
Plugin received sourcekitd request (handle: \(handle?.numericValue ?? -1))
71+
\(dict.description)
72+
"""
73+
)
74+
requestHandlingQueue.async {
75+
let response: SKDResponse
76+
do {
77+
response = try await body().response
78+
} catch {
79+
response = SKDResponse.from(error: error, sourcekitd: self.sourcekitd)
80+
}
81+
logger.debug(
82+
"""
83+
Finished (took \(Date().timeIntervalSince(start))s)
84+
\(response.description)
85+
"""
86+
)
87+
receiver(response)
7088
}
89+
return .requestHandled
7190
}
72-
return .requestHandled
7391
}
7492

7593
func sourcekitdProducesResult(body: @escaping @Sendable () async -> ()) -> HandleRequestResult {
@@ -139,6 +157,7 @@ final class RequestHandler: Sendable {
139157
}
140158

141159
func cancel(_ handle: RequestHandle) {
160+
logger.debug("Cancelling request with handle \(handle.numericValue)")
142161
self.completionProvider.cancel(handle: handle)
143162
}
144163
}

Sources/SwiftSourceKitPlugin/SourceKitDWrappers.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ struct RequestHandle: Sendable {
3131
}
3232
self.handle = handle
3333
}
34+
35+
var numericValue: Int {
36+
Int(bitPattern: handle)
37+
}
3438
}

Tests/SwiftSourceKitPluginTests/SwiftSourceKitPluginTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ final class SwiftSourceKitPluginTests: XCTestCase {
272272
}
273273

274274
func testCancellation() async throws {
275-
try XCTSkipIf(true, "rdar://145905708")
276275
try await SkipUnless.sourcekitdSupportsPlugin()
277276
let sourcekitd = try await getSourceKitD()
278277
let path = scratchFilePath()

0 commit comments

Comments
 (0)