Skip to content

Commit 0e22653

Browse files
authored
Merge pull request #2030 from ahoppen/plugin-logging
Add logging for requests handled by the SourceKit plugin
2 parents 2a05d94 + 12b38fe commit 0e22653

File tree

7 files changed

+56
-9
lines changed

7 files changed

+56
-9
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/SKDRequestDictionaryReader.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,34 @@
1313
import Csourcekitd
1414
import SourceKitD
1515

16+
#if canImport(Darwin)
17+
import Darwin
18+
#elseif canImport(Glibc)
19+
import Glibc
20+
#elseif canImport(Musl)
21+
import Musl
22+
#elseif canImport(CRT)
23+
import CRT
24+
#elseif canImport(Bionic)
25+
import Bionic
26+
#endif
27+
1628
/// Provide getters to get values of a sourcekitd request dictionary.
1729
///
1830
/// This is not part of the `SourceKitD` module because it uses `SourceKitD.servicePluginAPI` which must not be accessed
1931
/// outside of the service plugin.
20-
final class SKDRequestDictionaryReader: Sendable {
32+
final class SKDRequestDictionaryReader: Sendable, CustomStringConvertible {
2133
private nonisolated(unsafe) let dict: sourcekitd_api_object_t
2234
let sourcekitd: SourceKitD
2335

36+
var description: String {
37+
guard let description = sourcekitd.api.request_description_copy(dict) else {
38+
return "getting request description failed"
39+
}
40+
defer { free(description) }
41+
return String(cString: description)
42+
}
43+
2444
/// Creates an `SKDRequestDictionary` that essentially provides a view into the given opaque
2545
/// `sourcekitd_api_object_t`.
2646
init?(_ request: sourcekitd_api_object_t, sourcekitd: SourceKitD) {

Sources/SwiftSourceKitPlugin/SKDResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ final class SKDResponse: CustomStringConvertible, Sendable {
7474
}
7575

7676
public var description: String {
77-
let cstr = sourcekitd.api.request_description_copy(value)!
77+
let cstr = sourcekitd.api.response_description_copy(value)!
7878
defer { free(cstr) }
7979
return String(cString: cstr)
8080
}

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)