Skip to content

Commit 676a81c

Browse files
authored
Merge pull request swiftlang#30 from benlangmuir/logup
Improve logging of errors getting build settings
2 parents 6fc7651 + 87914fc commit 676a81c

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

Sources/SKCore/CompilationDatabase.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ public protocol CompilationDatabase {
5858
/// Loads the compilation database located in `directory`, if any.
5959
public func tryLoadCompilationDatabase(
6060
directory: AbsolutePath,
61-
fileSystem: FileSystem = localFileSystem
61+
_ fileSystem: FileSystem = localFileSystem
6262
) -> CompilationDatabase? {
63-
64-
return orLog("could not open compilation database for \(directory.asString)", level: .error) {
65-
try JSONCompilationDatabase(directory: directory, fileSystem: fileSystem)
66-
}
6763
// TODO: Support fixed compilation database (compile_flags.txt).
64+
return try? JSONCompilationDatabase(directory: directory, fileSystem)
6865
}
6966

7067
/// The JSON clang-compatible compilation database.
@@ -128,7 +125,7 @@ extension JSONCompilationDatabase: Codable {
128125
}
129126

130127
extension JSONCompilationDatabase {
131-
public init(directory: AbsolutePath, fileSystem: FileSystem = localFileSystem) throws {
128+
public init(directory: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws {
132129
let path = directory.appending(component: "compile_commands.json")
133130
let bytes = try fileSystem.readFileContents(path)
134131
try bytes.withUnsafeData { data in

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SKSupport
1314
import Basic
1415
import LanguageServerProtocol
1516

@@ -27,7 +28,7 @@ public final class CompilationDatabaseBuildSystem: BuildSettingsProvider {
2728
public init(projectRoot: AbsolutePath? = nil, fileSystem: FileSystem = localFileSystem) {
2829
self.fileSystem = fileSystem
2930
if let path = projectRoot {
30-
self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem: fileSystem)
31+
self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem)
3132
}
3233
}
3334

@@ -53,12 +54,17 @@ public final class CompilationDatabaseBuildSystem: BuildSettingsProvider {
5354
var dir = path
5455
while !dir.isRoot {
5556
dir = dir.parentDirectory
56-
if let db = tryLoadCompilationDatabase(directory: dir, fileSystem: fileSystem) {
57+
if let db = tryLoadCompilationDatabase(directory: dir, fileSystem) {
5758
compdb = db
5859
break
5960
}
6061
}
6162
}
63+
64+
if compdb == nil {
65+
log("could not open compilation database for \(path.asString)", level: .warning)
66+
}
67+
6268
return compdb
6369
}
6470
}

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ public final class SwiftPMWorkspace {
129129

130130
// FIXME: the rest of this should be done asynchronously.
131131

132-
// FIXME: connect to logging?
133-
let diags = DiagnosticsEngine()
132+
let diags = DiagnosticsEngine(handlers: [{ diag in
133+
log(diag.localizedDescription, level: diag.behavior.asLogLevel)
134+
}])
134135

135136
self.packageGraph = self.workspace.loadPackageGraph(root: PackageGraphRootInput(packages: [packageRoot]), diagnostics: diags)
136137

@@ -399,3 +400,13 @@ public final class BuildSettingProviderWorkspaceDelegate: WorkspaceDelegate {
399400
public func managedDependenciesDidUpdate(_ dependencies: AnySequence<ManagedDependency>) {
400401
}
401402
}
403+
404+
extension Basic.Diagnostic.Behavior {
405+
var asLogLevel: LogLevel {
406+
switch self {
407+
case .error: return .error
408+
case .warning: return .warning
409+
default: return .info
410+
}
411+
}
412+
}

Tests/SKCoreTests/CompilationDatabaseTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ final class CompilationDatabaseTests: XCTestCase {
169169
func testJSONCompilationDatabaseFromDirectory() {
170170
let fs = InMemoryFileSystem()
171171
try! fs.createDirectory(AbsolutePath("/a"))
172-
XCTAssertNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fileSystem: fs))
172+
XCTAssertNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fs))
173173

174174
try! fs.writeFileContents(AbsolutePath("/a/compile_commands.json"), bytes: """
175175
[
@@ -181,7 +181,7 @@ final class CompilationDatabaseTests: XCTestCase {
181181
]
182182
""")
183183

184-
XCTAssertNotNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fileSystem: fs))
184+
XCTAssertNotNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fs))
185185
}
186186

187187
func testCompilationDatabaseBuildSystem() {

0 commit comments

Comments
 (0)