1313NSString *const PBTaskTerminationStatusKey = @" PBTaskTerminationStatusKey" ;
1414NSString *const PBTaskTerminationOutputKey = @" PBTaskTerminationOutputKey" ;
1515
16+ const BOOL PBTaskDebugEnable = NO ;
17+
18+ #define PBTaskLog (...) \
19+ do { \
20+ if (PBTaskDebugEnable) NSLog (__VA_ARGS__); \
21+ } while (0 )
22+
1623@interface PBTask ()
1724
1825@property (retain ) NSTask *task;
@@ -63,12 +70,20 @@ - (instancetype)initWithLaunchPath:(NSString *)launchPath arguments:(NSArray *)a
6370 _standardOutputData = [NSMutableData data ];
6471 __weak PBTask *weakSelf = self;
6572 pipe.fileHandleForReading .readabilityHandler = ^(NSFileHandle *handle) {
73+ PBTaskLog (@" task %p : can read %d " , weakSelf, handle.fileDescriptor );
74+
6675 [(NSMutableData *)weakSelf.standardOutputData appendData: handle.availableData];
6776 };
6877
78+ PBTaskLog (@" task %p : init" , self);
79+
6980 return self;
7081}
7182
83+ - (void )dealloc {
84+ PBTaskLog (@" task %p : dealloc" , self);
85+ }
86+
7287
7388- (void )performTaskOnQueue : (dispatch_queue_t )queue terminationHandler : (void (^)(NSError * _Nullable))terminationHandler {
7489 NSParameterAssert (terminationHandler != nil );
@@ -77,6 +92,7 @@ - (void)performTaskOnQueue:(dispatch_queue_t)queue terminationHandler:(void (^)(
7792 self.task .terminationHandler = ^(NSTask *task) {
7893 NSError *error = nil ;
7994 if (task.terminationReason == NSTaskTerminationReasonUncaughtSignal) {
95+ PBTaskLog (@" task %p : caught signal" , weakSelf);
8096
8197 NSString *desc = @" Task killed" ;
8298 NSArray *taskArguments = [@[task.launchPath] arrayByAddingObjectsFromArray: task.arguments];
@@ -90,6 +106,8 @@ - (void)performTaskOnQueue:(dispatch_queue_t)queue terminationHandler:(void (^)(
90106 } else if (task.terminationReason == NSTaskTerminationReasonExit && task.terminationStatus != 0 ) {
91107 // Since we're on an error path, grab the output now and stash it in the returned error
92108
109+ PBTaskLog (@" task %p : exit != 0" , weakSelf);
110+
93111 [(NSMutableData *)weakSelf.standardOutputData appendData: [[task.standardOutput fileHandleForReading ] readDataToEndOfFile ]];
94112 NSString *outputString = [[NSString alloc ] initWithData: weakSelf.standardOutputData encoding: NSUTF8StringEncoding];
95113 weakSelf.standardOutputData = nil ;
@@ -104,7 +122,8 @@ - (void)performTaskOnQueue:(dispatch_queue_t)queue terminationHandler:(void (^)(
104122 PBTaskTerminationOutputKey: outputString,
105123 };
106124 error = [NSError errorWithDomain: PBTaskErrorDomain code: PBTaskNonZeroExitCodeError userInfo: userInfo];
107-
125+ } else {
126+ PBTaskLog (@" task %p : exit success" , weakSelf);
108127 }
109128
110129 dispatch_async (queue, ^{
@@ -118,12 +137,15 @@ - (void)performTaskOnQueue:(dispatch_queue_t)queue terminationHandler:(void (^)(
118137 self.task .standardInput = inputPipe;
119138
120139 inputPipe.fileHandleForWriting .writeabilityHandler = ^(NSFileHandle *handle) {
140+ PBTaskLog (@" task %p : can write %d " , weakSelf, handle.fileDescriptor );
141+
121142 [handle writeData: weakSelf.standardInputData];
122143 [handle closeFile ];
123144 };
124145 }
125146
126147 @try {
148+ PBTaskLog (@" task %p : launching" , self);
127149 [self .task launch ];
128150 }
129151 @catch (NSException *exception) {
@@ -171,6 +193,7 @@ - (BOOL)launchTask:(NSError **)error {
171193 }];
172194
173195 dispatch_time_t timeout = dispatch_time (DISPATCH_TIME_NOW , 30 * NSEC_PER_SEC );
196+ PBTaskLog (@" task %p : waiting for completion" , self);
174197 if (dispatch_semaphore_wait (sem, timeout) != 0 ) {
175198 // Timeout !
176199 // Unset the termination handler before calling, so we don't trigger it
0 commit comments