Skip to content

Commit 008d414

Browse files
committed
deprecate global and process verbosity
motivation: fine grained control over verbosity setting and handling of logging changes: * deprecate global verbosity flag * deprecate Process verbosity attribute in favor of an optional logging handler (closure)
1 parent d318eaa commit 008d414

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

Sources/TSCBasic/Process.swift

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,20 @@ public final class Process {
205205
public typealias OutputClosure = ([UInt8]) -> Void
206206

207207
/// Global default setting for verbose.
208-
public static var verbose = false
208+
@available(*, deprecated)
209+
public static var verbose: Bool {
210+
Self.loggingHandler != nil
211+
}
212+
213+
public static var loggingHandler: ((String) -> Void)? = nil
209214

210215
/// If true, prints the subprocess arguments before launching it.
211-
public let verbose: Bool
216+
@available(*, deprecated)
217+
public var verbose: Bool {
218+
self.loggingHandler != nil
219+
}
220+
221+
private let loggingHandler: ((String) -> Void)?
212222

213223
/// The current environment.
214224
@available(*, deprecated, message: "use ProcessEnv.vars instead")
@@ -286,24 +296,50 @@ public final class Process {
286296
/// will be inherited.
287297
/// - workingDirectory: The path to the directory under which to run the process.
288298
/// - outputRedirection: How process redirects its output. Default value is .collect.
289-
/// - verbose: If true, launch() will print the arguments of the subprocess before launching it.
290299
/// - startNewProcessGroup: If true, a new progress group is created for the child making it
291300
/// continue running even if the parent is killed or interrupted. Default value is true.
301+
/// - loggingHandler: Handler for logging messages
302+
///
292303
@available(macOS 10.15, *)
293304
public init(
294305
arguments: [String],
295306
environment: [String: String] = ProcessEnv.vars,
296307
workingDirectory: AbsolutePath,
297308
outputRedirection: OutputRedirection = .collect,
298-
verbose: Bool = Process.verbose,
299-
startNewProcessGroup: Bool = true
309+
startNewProcessGroup: Bool = true,
310+
loggingHandler: ((String) -> Void)? = Process.loggingHandler
300311
) {
301312
self.arguments = arguments
302313
self.environment = environment
303314
self.workingDirectory = workingDirectory
304315
self.outputRedirection = outputRedirection
305-
self.verbose = verbose
306316
self.startNewProcessGroup = startNewProcessGroup
317+
self.loggingHandler = loggingHandler
318+
}
319+
320+
// deprecated 2/2022
321+
@_disfavoredOverload
322+
@available(*, deprecated, message: "use version without verbosity flag")
323+
@available(macOS 10.15, *)
324+
public convenience init(
325+
arguments: [String],
326+
environment: [String: String] = ProcessEnv.vars,
327+
workingDirectory: AbsolutePath,
328+
outputRedirection: OutputRedirection = .collect,
329+
verbose: Bool,
330+
startNewProcessGroup: Bool = true
331+
) {
332+
self.init(
333+
arguments: arguments,
334+
environment: environment,
335+
workingDirectory: workingDirectory,
336+
outputRedirection: outputRedirection,
337+
startNewProcessGroup: startNewProcessGroup,
338+
loggingHandler: verbose ? { message in
339+
stdoutStream <<< message <<< "\n"
340+
stdoutStream.flush()
341+
} : nil
342+
)
307343
}
308344

309345
/// Create a new process instance.
@@ -320,15 +356,36 @@ public final class Process {
320356
arguments: [String],
321357
environment: [String: String] = ProcessEnv.vars,
322358
outputRedirection: OutputRedirection = .collect,
323-
verbose: Bool = Process.verbose,
324-
startNewProcessGroup: Bool = true
359+
startNewProcessGroup: Bool = true,
360+
loggingHandler: ((String) -> Void)? = Process.loggingHandler
325361
) {
326362
self.arguments = arguments
327363
self.environment = environment
328364
self.workingDirectory = nil
329365
self.outputRedirection = outputRedirection
330-
self.verbose = verbose
331366
self.startNewProcessGroup = startNewProcessGroup
367+
self.loggingHandler = loggingHandler
368+
}
369+
370+
@_disfavoredOverload
371+
@available(*, deprecated, message: "user version without verbosity flag")
372+
public convenience init(
373+
arguments: [String],
374+
environment: [String: String] = ProcessEnv.vars,
375+
outputRedirection: OutputRedirection = .collect,
376+
verbose: Bool = Process.verbose,
377+
startNewProcessGroup: Bool = true
378+
) {
379+
self.init(
380+
arguments: arguments,
381+
environment: environment,
382+
outputRedirection: outputRedirection,
383+
startNewProcessGroup: startNewProcessGroup,
384+
loggingHandler: verbose ? { message in
385+
stdoutStream <<< message <<< "\n"
386+
stdoutStream.flush()
387+
} : nil
388+
)
332389
}
333390

334391
/// Returns the path of the the given program if found in the search paths.
@@ -393,9 +450,10 @@ public final class Process {
393450
}
394451

395452
// Print the arguments if we are verbose.
396-
if self.verbose {
397-
stdoutStream <<< arguments.map({ $0.spm_shellEscaped() }).joined(separator: " ") <<< "\n"
398-
stdoutStream.flush()
453+
if let loggingHandler = self.loggingHandler {
454+
loggingHandler(arguments.map({ $0.spm_shellEscaped() }).joined(separator: " "))
455+
//stdoutStream <<< arguments.map({ $0.spm_shellEscaped() }).joined(separator: " ") <<< "\n"
456+
//stdoutStream.flush()
399457
}
400458

401459
// Look for executable.

Sources/TSCUtility/Verbosity.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
// verbose 2/2022
12+
@available(*, deprecated)
1113
public enum Verbosity: Int {
1214
case concise
1315
case verbose
@@ -37,4 +39,6 @@ public enum Verbosity: Int {
3739
}
3840
}
3941

42+
// verbose 2/2022
43+
@available(*, deprecated)
4044
public var verbosity = Verbosity.concise

0 commit comments

Comments
 (0)