@@ -205,10 +205,20 @@ public final class Process {
205
205
public typealias OutputClosure = ( [ UInt8 ] ) -> Void
206
206
207
207
/// 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
209
214
210
215
/// 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 ) ?
212
222
213
223
/// The current environment.
214
224
@available ( * , deprecated, message: " use ProcessEnv.vars instead " )
@@ -286,24 +296,50 @@ public final class Process {
286
296
/// will be inherited.
287
297
/// - workingDirectory: The path to the directory under which to run the process.
288
298
/// - 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.
290
299
/// - startNewProcessGroup: If true, a new progress group is created for the child making it
291
300
/// continue running even if the parent is killed or interrupted. Default value is true.
301
+ /// - loggingHandler: Handler for logging messages
302
+ ///
292
303
@available ( macOS 10 . 15 , * )
293
304
public init (
294
305
arguments: [ String ] ,
295
306
environment: [ String : String ] = ProcessEnv . vars,
296
307
workingDirectory: AbsolutePath ,
297
308
outputRedirection: OutputRedirection = . collect,
298
- verbose : Bool = Process . verbose ,
299
- startNewProcessGroup : Bool = true
309
+ startNewProcessGroup : Bool = true ,
310
+ loggingHandler : ( ( String ) -> Void ) ? = Process . loggingHandler
300
311
) {
301
312
self . arguments = arguments
302
313
self . environment = environment
303
314
self . workingDirectory = workingDirectory
304
315
self . outputRedirection = outputRedirection
305
- self . verbose = verbose
306
316
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
+ )
307
343
}
308
344
309
345
/// Create a new process instance.
@@ -320,15 +356,36 @@ public final class Process {
320
356
arguments: [ String ] ,
321
357
environment: [ String : String ] = ProcessEnv . vars,
322
358
outputRedirection: OutputRedirection = . collect,
323
- verbose : Bool = Process . verbose ,
324
- startNewProcessGroup : Bool = true
359
+ startNewProcessGroup : Bool = true ,
360
+ loggingHandler : ( ( String ) -> Void ) ? = Process . loggingHandler
325
361
) {
326
362
self . arguments = arguments
327
363
self . environment = environment
328
364
self . workingDirectory = nil
329
365
self . outputRedirection = outputRedirection
330
- self . verbose = verbose
331
366
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
+ )
332
389
}
333
390
334
391
/// Returns the path of the the given program if found in the search paths.
@@ -393,9 +450,10 @@ public final class Process {
393
450
}
394
451
395
452
// 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()
399
457
}
400
458
401
459
// Look for executable.
0 commit comments