Skip to content

Commit b2c31a7

Browse files
committed
use verbosity flags to drive diagnostics logging level
motivation: allow users to specify verbosity which would impact how much diagnostic information is outputed changes: * add "very-verbose" CLI flag * drive diagnostics logging level based on verbosity flags: verbose == .info, very-verbose == .debug * filter diagnostic printing based on log level * pass logLevel where appropriate instead of isVerbose * add test
1 parent 79f834c commit b2c31a7

11 files changed

+214
-89
lines changed

Sources/Basics/Observability.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,28 @@ public struct Diagnostic: CustomStringConvertible, Equatable {
292292
Self(severity: .debug, message: message.description, metadata: metadata)
293293
}
294294

295-
public enum Severity: Equatable {
295+
public enum Severity: Comparable {
296296
case error
297297
case warning
298298
case info
299299
case debug
300+
301+
internal var naturalIntegralValue: Int {
302+
switch self {
303+
case .debug:
304+
return 0
305+
case .info:
306+
return 1
307+
case .warning:
308+
return 2
309+
case .error:
310+
return 3
311+
}
312+
}
313+
314+
public static func < (lhs: Self, rhs: Self) -> Bool {
315+
return lhs.naturalIntegralValue < rhs.naturalIntegralValue
316+
}
300317
}
301318
}
302319

Sources/Build/BuildOperation.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
5252
/// The output stream for the build delegate.
5353
private let outputStream: OutputByteStream
5454

55+
/// The verbosity level to print out at
56+
private let logLevel: Basics.Diagnostic.Severity
57+
5558
/// File system to operate on
5659
private let fileSystem: TSCBasic.FileSystem
5760

@@ -68,6 +71,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
6871
packageGraphLoader: @escaping () throws -> PackageGraph,
6972
pluginInvoker: @escaping (PackageGraph) throws -> [ResolvedTarget: [PluginInvocationResult]],
7073
outputStream: OutputByteStream,
74+
logLevel: Basics.Diagnostic.Severity,
7175
fileSystem: TSCBasic.FileSystem,
7276
observabilityScope: ObservabilityScope
7377
) {
@@ -76,6 +80,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
7680
self.packageGraphLoader = packageGraphLoader
7781
self.pluginInvoker = pluginInvoker
7882
self.outputStream = outputStream
83+
self.logLevel = logLevel
7984
self.fileSystem = fileSystem
8085
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Operation")
8186
}
@@ -127,7 +132,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
127132
public func build(subset: BuildSubset) throws {
128133
// Create the build system.
129134
let buildDescription = try self.getBuildDescription()
130-
let buildSystem = try createBuildSystem(with: buildDescription)
135+
let buildSystem = try self.createBuildSystem(buildDescription: buildDescription)
131136
self.buildSystem = buildSystem
132137

133138
// Perform the build.
@@ -224,7 +229,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
224229

225230
/// Build the package structure target.
226231
private func buildPackageStructure() throws {
227-
let buildSystem = try self.createBuildSystem(with: nil)
232+
let buildSystem = try self.createBuildSystem(buildDescription: .none)
228233
self.buildSystem = buildSystem
229234

230235
// Build the package structure target which will re-generate the llbuild manifest, if necessary.
@@ -237,16 +242,13 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
237242
///
238243
/// The build description should only be omitted when creating the build system for
239244
/// building the package structure target.
240-
private func createBuildSystem(
241-
with buildDescription: BuildDescription?
242-
) throws -> SPMLLBuild.BuildSystem {
245+
private func createBuildSystem(buildDescription: BuildDescription?) throws -> SPMLLBuild.BuildSystem {
243246
// Figure out which progress bar we have to use during the build.
244-
let isVerbose = verbosity != .concise
245-
let progressAnimation: ProgressAnimationProtocol = isVerbose
247+
let progressAnimation: ProgressAnimationProtocol = self.logLevel.isVerbose
246248
? MultiLineNinjaProgressAnimation(stream: self.outputStream)
247249
: NinjaProgressAnimation(stream: self.outputStream)
248250

249-
let bctx = BuildExecutionContext(
251+
let buildExecutionContext = BuildExecutionContext(
250252
buildParameters,
251253
buildDescription: buildDescription,
252254
packageStructureDelegate: self,
@@ -256,14 +258,14 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
256258
// Create the build delegate.
257259
let buildSystemDelegate = BuildOperationBuildSystemDelegateHandler(
258260
buildSystem: self,
259-
bctx: bctx,
261+
buildExecutionContext: buildExecutionContext,
260262
outputStream: self.outputStream,
261263
progressAnimation: progressAnimation,
264+
logLevel: self.logLevel,
262265
observabilityScope: self.observabilityScope,
263266
delegate: self.delegate
264267
)
265268
self.buildSystemDelegate = buildSystemDelegate
266-
buildSystemDelegate.isVerbose = isVerbose
267269

268270
let databasePath = buildParameters.dataPath.appending(component: "build.db").pathString
269271
let buildSystem = SPMLLBuild.BuildSystem(
@@ -272,7 +274,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
272274
delegate: buildSystemDelegate,
273275
schedulerLanes: buildParameters.jobs
274276
)
275-
buildSystemDelegate.onCommmandFailure = {
277+
278+
// TODO: this seems fragile, perhaps we replace commandFailureHandler by adding relevant calls in the delegates chain
279+
buildSystemDelegate.commandFailureHandler = {
276280
buildSystem.cancel()
277281
self.delegate?.buildSystemDidCancel(self)
278282
}
@@ -380,3 +384,9 @@ extension BuildSubset {
380384
}
381385
}
382386
}
387+
388+
extension Basics.Diagnostic.Severity {
389+
var isVerbose: Bool {
390+
return self <= .info
391+
}
392+
}

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ final class CopyCommand: CustomLLBuildCommand {
363363

364364
/// Convenient llbuild build system delegate implementation
365365
final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate, SwiftCompilerOutputParserDelegate {
366-
var outputStream: ThreadSafeOutputByteStream
367-
var progressAnimation: ProgressAnimationProtocol
368-
var onCommmandFailure: (() -> Void)?
369-
var isVerbose: Bool = false
370-
weak var delegate: SPMBuildCore.BuildSystemDelegate?
366+
private let outputStream: ThreadSafeOutputByteStream
367+
private let progressAnimation: ProgressAnimationProtocol
368+
var commandFailureHandler: (() -> Void)?
369+
private let logLevel: Basics.Diagnostic.Severity
370+
private weak var delegate: SPMBuildCore.BuildSystemDelegate?
371371
private let buildSystem: SPMBuildCore.BuildSystem
372372
private let queue = DispatchQueue(label: "org.swift.swiftpm.build-delegate")
373373
private var taskTracker = CommandTaskTracker()
@@ -382,22 +382,24 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
382382

383383
init(
384384
buildSystem: SPMBuildCore.BuildSystem,
385-
bctx: BuildExecutionContext,
385+
buildExecutionContext: BuildExecutionContext,
386386
outputStream: OutputByteStream,
387387
progressAnimation: ProgressAnimationProtocol,
388+
logLevel: Basics.Diagnostic.Severity,
388389
observabilityScope: ObservabilityScope,
389390
delegate: SPMBuildCore.BuildSystemDelegate?
390391
) {
392+
self.buildSystem = buildSystem
393+
self.buildExecutionContext = buildExecutionContext
391394
// FIXME: Implement a class convenience initializer that does this once they are supported
392395
// https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924
393396
self.outputStream = outputStream as? ThreadSafeOutputByteStream ?? ThreadSafeOutputByteStream(outputStream)
394397
self.progressAnimation = progressAnimation
395-
self.buildExecutionContext = bctx
396-
self.delegate = delegate
397-
self.buildSystem = buildSystem
398+
self.logLevel = logLevel
398399
self.observabilityScope = observabilityScope
400+
self.delegate = delegate
399401

400-
let swiftParsers = bctx.buildDescription?.swiftCommands.mapValues { tool in
402+
let swiftParsers = buildExecutionContext.buildDescription?.swiftCommands.mapValues { tool in
401403
SwiftCompilerOutputParser(targetName: tool.moduleName, delegate: self)
402404
} ?? [:]
403405
self.swiftParsers = swiftParsers
@@ -429,7 +431,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
429431
}
430432

431433
func hadCommandFailure() {
432-
onCommmandFailure?()
434+
self.commandFailureHandler?()
433435
}
434436

435437
func handleDiagnostic(_ diagnostic: SPMLLBuild.Diagnostic) {
@@ -446,7 +448,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
446448
}
447449

448450
func commandStatusChanged(_ command: SPMLLBuild.Command, kind: CommandStatusKind) {
449-
guard !isVerbose else { return }
451+
guard !self.logLevel.isVerbose else { return }
450452
guard command.shouldShowStatus else { return }
451453
guard !swiftParsers.keys.contains(command.name) else { return }
452454

@@ -467,7 +469,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
467469

468470
queue.async {
469471
self.delegate?.buildSystem(self.buildSystem, didStartCommand: BuildSystemCommand(command))
470-
if self.isVerbose {
472+
if self.logLevel.isVerbose {
471473
self.outputStream <<< command.verboseDescription <<< "\n"
472474
self.outputStream.flush()
473475
}
@@ -485,7 +487,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
485487
queue.async {
486488
self.delegate?.buildSystem(self.buildSystem, didFinishCommand: BuildSystemCommand(command))
487489

488-
if !self.isVerbose {
490+
if !self.logLevel.isVerbose {
489491
let targetName = self.swiftParsers[command.name]?.targetName
490492
self.taskTracker.commandFinished(command, result: result, targetName: targetName)
491493
self.updateProgress()
@@ -575,7 +577,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
575577

576578
func swiftCompilerOutputParser(_ parser: SwiftCompilerOutputParser, didParse message: SwiftCompilerMessage) {
577579
queue.async {
578-
if self.isVerbose {
580+
if self.logLevel.isVerbose {
579581
if let text = message.verboseProgressText {
580582
self.outputStream <<< text <<< "\n"
581583
self.outputStream.flush()
@@ -587,7 +589,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
587589

588590
if let output = message.standardOutput {
589591
// first we want to print the output so users have it handy
590-
if !self.isVerbose {
592+
if !self.logLevel.isVerbose {
591593
self.progressAnimation.clear()
592594
}
593595

@@ -609,7 +611,7 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
609611
func swiftCompilerOutputParser(_ parser: SwiftCompilerOutputParser, didFailWith error: Error) {
610612
let message = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription
611613
self.observabilityScope.emit(.swiftCompilerOutputParsingError(message))
612-
onCommmandFailure?()
614+
self.commandFailureHandler?()
613615
}
614616

615617
func buildComplete(success: Bool) {

Sources/Commands/APIDigester.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ struct APIDigesterBaselineDumper {
5858
for modulesToDiff: Set<String>,
5959
at baselineDir: AbsolutePath?,
6060
force: Bool,
61-
outputStream: OutputByteStream
61+
outputStream: OutputByteStream,
62+
logLevel: Diagnostic.Severity
6263
) throws -> AbsolutePath {
6364
var modulesToDiff = modulesToDiff
6465
let apiDiffDir = inputBuildParameters.apiDiff
@@ -126,6 +127,7 @@ struct APIDigesterBaselineDumper {
126127
packageGraphLoader: { graph },
127128
pluginInvoker: { _ in [:] },
128129
outputStream: outputStream,
130+
logLevel: logLevel,
129131
fileSystem: localFileSystem,
130132
observabilityScope: observabilityScope
131133
)

0 commit comments

Comments
 (0)