Skip to content

Commit 1f23ce9

Browse files
committed
Improve logging
1 parent 918c91b commit 1f23ce9

File tree

5 files changed

+81
-51
lines changed

5 files changed

+81
-51
lines changed

Sources/SwiftBuildSupport/PackagePIFBuilder+Helpers.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,55 @@ extension ProjectModel.BuildSettings.Declaration {
10351035
}
10361036
}
10371037

1038+
// MARK: - ObservabilityScope Helpers
1039+
1040+
extension ObservabilityScope {
1041+
/// Logs an informational PIF message (intended for developers, not end users).
1042+
func logPIF(
1043+
_ severity: Diagnostic.Severity = .debug,
1044+
indent: UInt = 0,
1045+
_ message: String,
1046+
sourceFile: StaticString = #fileID,
1047+
sourceLine: UInt = #line
1048+
) {
1049+
var metadata = ObservabilityMetadata()
1050+
metadata.sourceLocation = SourceLocation(sourceFile, sourceLine)
1051+
1052+
let indentation = String(repeating: " ", count: Int(indent))
1053+
let message = "PIF: \(indentation)\(message)"
1054+
1055+
let diagnostic = Diagnostic(severity: severity, message: message, metadata: metadata)
1056+
self.emit(diagnostic)
1057+
}
1058+
}
1059+
1060+
extension ObservabilityMetadata {
1061+
public var sourceLocation: SourceLocation? {
1062+
get {
1063+
self[SourceLocationKey.self]
1064+
}
1065+
set {
1066+
self[SourceLocationKey.self] = newValue
1067+
}
1068+
}
1069+
1070+
private enum SourceLocationKey: Key {
1071+
typealias Value = SourceLocation
1072+
}
1073+
}
1074+
1075+
public struct SourceLocation: Sendable {
1076+
public let file: StaticString
1077+
public let line: UInt
1078+
1079+
public init(_ file: StaticString, _ line: UInt) {
1080+
precondition(file.description.hasContent)
1081+
1082+
self.file = file
1083+
self.line = line
1084+
}
1085+
}
1086+
10381087
// MARK: - General Helpers
10391088

10401089
extension SourceControlURL {

Sources/SwiftBuildSupport/PackagePIFBuilder.swift

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,14 @@ public final class PackagePIFBuilder {
5959
/// Scope for logging informational debug messages (intended for developers, not end users).
6060
let observabilityScope: ObservabilityScope
6161

62-
/// Logs an informational debug message (intended for developers, not end users).
62+
/// Logs an informational message (intended for developers, not end users).
6363
func log(
6464
_ severity: Diagnostic.Severity,
6565
_ message: String,
6666
sourceFile: StaticString = #fileID,
6767
sourceLine: UInt = #line
6868
) {
69-
var metadata = ObservabilityMetadata()
70-
metadata.sourceLocation = SourceLocation(sourceFile, sourceLine)
71-
72-
let diagnostic = Diagnostic(severity: severity, message: message, metadata: metadata)
73-
self.observabilityScope.emit(diagnostic)
69+
self.observabilityScope.logPIF(severity, message, sourceFile: sourceFile, sourceLine: sourceLine)
7470
}
7571

7672
unowned let delegate: BuildDelegate
@@ -373,10 +369,11 @@ public final class PackagePIFBuilder {
373369
/// Build the PIF.
374370
@discardableResult
375371
public func build() throws -> [ModuleOrProduct] {
376-
self.log(.info, "Building PIF for package \(self.package.identity)")
377-
378-
var builder = PackagePIFProjectBuilder(createForPackage: package, builder: self)
379-
self.addProjectBuildSettings(&builder)
372+
self.log(
373+
.info,
374+
"Building PIF project for package '\(self.package.identity)' " +
375+
"(\(package.products.count) products, \(package.modules.count) modules)"
376+
)
380377

381378
var projectBuilder = PackagePIFProjectBuilder(createForPackage: package, builder: self)
382379
self.addProjectBuildSettings(&projectBuilder)
@@ -664,29 +661,4 @@ extension PackagePIFBuilder.LinkedPackageBinary {
664661
}
665662
}
666663

667-
extension ObservabilityMetadata {
668-
public var sourceLocation: SourceLocation? {
669-
get {
670-
self[SourceLocationKey.self]
671-
}
672-
set {
673-
self[SourceLocationKey.self] = newValue
674-
}
675-
}
676-
677-
private enum SourceLocationKey: Key {
678-
typealias Value = SourceLocation
679-
}
680-
}
681-
682-
public struct SourceLocation: Sendable {
683-
public let file: StaticString
684-
public let line: UInt
685-
686-
public init(_ file: StaticString, _ line: UInt) {
687-
self.file = file
688-
self.line = line
689-
}
690-
}
691-
692664
#endif

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ extension PackagePIFProjectBuilder {
4747
}
4848
do {
4949
let pluginTarget = self.project[keyPath: pluginTargetKeyPath]
50-
log(.debug, "Created \(pluginTarget.productType) '\(pluginTarget.id)' with name '\(pluginTarget.name)'")
50+
log(
51+
.debug,
52+
"Created target '\(pluginTarget.id)' of type " +
53+
"\(pluginTarget.productType) and name '\(pluginTarget.name)'"
54+
)
5155
}
5256

5357
var buildSettings: ProjectModel.BuildSettings = self.package.underlying.packageBaseBuildSettings
@@ -285,8 +289,8 @@ extension PackagePIFProjectBuilder {
285289
let sourceModule = self.project[keyPath: sourceModuleTargetKeyPath]
286290
log(
287291
.debug,
288-
"Created \(sourceModuleTarget.productType) '\(sourceModuleTarget.id)' " +
289-
"with name '\(sourceModuleTarget.name)' and product name '\(sourceModuleTarget.productName)'"
292+
"Created target '\(sourceModule.id)' of type '\(sourceModule.productType)' " +
293+
"with name '\(sourceModule.name)' and product name '\(sourceModule.productName)'"
290294
)
291295
}
292296

@@ -517,7 +521,7 @@ extension PackagePIFProjectBuilder {
517521
log(
518522
.debug,
519523
indent: 1,
520-
"Added '\(String(describing: impartedSettings[.OTHER_LDFLAGS]))' to imparted OTHER_LDFLAGS"
524+
"Added '\(impartedSettings[.OTHER_LDFLAGS]!)' to imparted OTHER_LDFLAGS"
521525
)
522526

523527
// This should be only for dynamic targets, but that isn't possible today.
@@ -527,7 +531,7 @@ extension PackagePIFProjectBuilder {
527531
log(
528532
.debug,
529533
indent: 1,
530-
"Added '\(String(describing: impartedSettings[.FRAMEWORK_SEARCH_PATHS]))' to imparted FRAMEWORK_SEARCH_PATHS"
534+
"Added '\(impartedSettings[.FRAMEWORK_SEARCH_PATHS]!)' to imparted FRAMEWORK_SEARCH_PATHS"
531535
)
532536

533537
// Set the appropriate language versions.
@@ -818,7 +822,7 @@ extension PackagePIFProjectBuilder {
818822
let systemLibraryTarget = self.project[keyPath: systemLibraryTargetKeyPath]
819823
log(
820824
.debug,
821-
"Created \(type(of: systemLibraryTarget)) '\(systemLibraryTarget.id)' with name '\(systemLibraryTarget.name)'"
825+
"Created aggregate target '\(systemLibraryTarget.id)' with name '\(systemLibraryTarget.name)'"
822826
)
823827
}
824828

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Products.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ extension PackagePIFProjectBuilder {
8484
let mainModuleTarget = self.project[keyPath: mainModuleTargetKeyPath]
8585
log(
8686
.debug,
87-
"Created \(mainModuleTarget.productType)) '\(mainModuleTarget.id)' " +
88-
"with name '\(mainModuleTarget.name)' and product name '\(mainModuleTarget.productName)'"
87+
"Created target '\(mainModuleTarget.id)' of type '\(mainModuleTarget.productType)' " +
88+
"with name '\(mainModuleTarget.name)' and product name '\(mainModuleTarget.productName)'"
8989
)
9090
}
9191

9292
// We're currently *not* handling other module targets (and SwiftPM should never return them) for
9393
// a main-module product but, for diagnostic purposes, we warn about any that we do come across.
9494
if product.otherModules.hasContent {
9595
let otherModuleNames = product.otherModules.map(\.name).joined(separator: ",")
96-
log(.debug, ".. warning: ignored unexpected other module targets \(otherModuleNames)")
96+
log(.debug, indent: 1, "Warning: ignored unexpected other module targets \(otherModuleNames)")
9797
}
9898

9999
// Deal with any generated source files or resource files.
@@ -626,7 +626,7 @@ extension PackagePIFProjectBuilder {
626626
log(
627627
.debug,
628628
"Created target '\(librayTarget.id)' of type '\(librayTarget.productType)' with " +
629-
"name '\(librayTarget.name)' and product name '\(librayTarget.productName)'"
629+
"name '\(librayTarget.name)' and product name '\(librayTarget.productName)'"
630630
)
631631
}
632632

@@ -889,7 +889,7 @@ extension PackagePIFProjectBuilder {
889889
log(
890890
.debug,
891891
"Created target '\(systemLibraryTarget.id)' of type '\(systemLibraryTarget.productType)' " +
892-
"with name '\(systemLibraryTarget.name)' and product name '\(systemLibraryTarget.productName)'"
892+
"with name '\(systemLibraryTarget.name)' and product name '\(systemLibraryTarget.productName)'"
893893
)
894894
}
895895

Sources/SwiftBuildSupport/PackagePIFProjectBuilder.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@ struct PackagePIFProjectBuilder {
7171

7272
func log(
7373
_ severity: Diagnostic.Severity,
74-
indent: Int = 0,
74+
indent: UInt = 0,
7575
_ message: String,
7676
sourceFile: StaticString = #fileID,
7777
sourceLine: UInt = #line
7878
) {
79-
let levelPrefix = String(repeating: " ", count: indent)
80-
self.pifBuilder.log(severity, levelPrefix + message, sourceFile: sourceFile, sourceLine: sourceLine)
79+
self.pifBuilder.observabilityScope.logPIF(
80+
severity,
81+
indent: indent,
82+
message,
83+
sourceFile: sourceFile,
84+
sourceLine: sourceLine
85+
)
8186
}
8287

8388
init(createForPackage package: PackageGraph.ResolvedPackage, builder: PackagePIFBuilder) {
@@ -197,8 +202,8 @@ struct PackagePIFProjectBuilder {
197202
self.log(
198203
.debug,
199204
indent: 1,
200-
"Created \(type(of: resourcesTarget)) '\(resourcesTarget.id)' of type '\(resourcesTarget.productType)' " +
201-
"with name '\(resourcesTarget.name)' and product name '\(resourcesTarget.productName)'"
205+
"Created target '\(resourcesTarget.id)' of type '\(resourcesTarget.productType)' " +
206+
"with name '\(resourcesTarget.name)' and product name '\(resourcesTarget.productName)'"
202207
)
203208

204209
var settings: ProjectModel.BuildSettings = self.package.underlying.packageBaseBuildSettings

0 commit comments

Comments
 (0)