1.1 --- a/MYTask.m Sat Mar 28 09:36:46 2009 -0700
1.2 +++ b/MYTask.m Sun May 03 10:13:31 2009 -0700
1.3 @@ -30,7 +30,7 @@
1.4 - (id) initWithCommand: (NSString*)command
1.5 arguments: (NSArray*)arguments
1.6 {
1.7 - NSParameterAssert(command);
1.8 + Assert(command);
1.9 self = [super init];
1.10 if (self != nil) {
1.11 _command = command;
1.12 @@ -107,6 +107,18 @@
1.13 }
1.14
1.15
1.16 +- (NSString*) commandLine {
1.17 + NSMutableString *desc = [NSMutableString stringWithString: _command];
1.18 + for (NSString *arg in _arguments) {
1.19 + [desc appendString: @" "];
1.20 + if ([arg rangeOfString: @" "].length > 0)
1.21 + arg = [NSString stringWithFormat: @"'%@'", arg];
1.22 + [desc appendString: arg];
1.23 + }
1.24 + return desc;
1.25 +}
1.26 +
1.27 +
1.28 - (void) ignoreOutput
1.29 {
1.30 _ignoreOutput = YES;
1.31 @@ -119,7 +131,7 @@
1.32 va_start(args,fmt);
1.33
1.34 NSString *message = [[NSString alloc] initWithFormat: fmt arguments: args];
1.35 - Log(@"MYTask Error: %@",message);
1.36 + LogTo(MYTask, @"Error: %@",message);
1.37 NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObject: message
1.38 forKey: NSLocalizedDescriptionKey];
1.39 _error = [NSError errorWithDomain: MYTaskErrorDomain code: kMYTaskError userInfo: info];
1.40 @@ -155,7 +167,7 @@
1.41 /** Subclasses can override this. */
1.42 - (NSTask*) createTask
1.43 {
1.44 - NSAssert(!_task,@"createTask called twice");
1.45 + Assert(!_task,@"createTask called twice");
1.46 NSTask *task = [[NSTask alloc] init];
1.47 task.launchPath = _command;
1.48 task.arguments = _arguments;
1.49 @@ -167,14 +179,14 @@
1.50
1.51 - (BOOL) start
1.52 {
1.53 - NSAssert(!_task, @"Task has already been run");
1.54 + Assert(!_task, @"Task has already been run");
1.55 if( _error )
1.56 return NO;
1.57
1.58 _task = [self createTask];
1.59 - NSAssert(_task,@"createTask returned nil");
1.60 + Assert(_task,@"createTask returned nil");
1.61
1.62 - Log(@"Task: %@ %@",_task.launchPath,[_task.arguments componentsJoinedByString: @" "]);
1.63 + LogTo(MYTask,@"$ %@", self.commandLine);
1.64
1.65 _task.standardOutput = [self _openPipeAndHandle: &_outHandle notifying: @selector(_gotOutput:)];
1.66 _outputData = [[NSMutableData alloc] init];
1.67 @@ -188,7 +200,7 @@
1.68 @try{
1.69 [_task launch];
1.70 }@catch( id x ) {
1.71 - Log(@"Task failed to launch: %@",x);
1.72 + Warn(@"Task failed to launch: %@",x);
1.73 _resultCode = 666;
1.74 [self _close];
1.75 return [self makeError: @"Exception launching %@: %@",_task.launchPath,x];
1.76 @@ -221,7 +233,7 @@
1.77 if( n.object == _outHandle ) {
1.78 if( data.length > 0 ) {
1.79 [_outHandle readInBackgroundAndNotifyForModes: _modes];
1.80 - LogTo(Task,@"Got %u bytes of output",data.length);
1.81 + LogTo(HgTaskVerbose, @"Got %u bytes of output",data.length);
1.82 if( _outputData ) {
1.83 [self willChangeValueForKey: @"output"];
1.84 [self willChangeValueForKey: @"outputData"];
1.85 @@ -231,7 +243,7 @@
1.86 [self didChangeValueForKey: @"output"];
1.87 }
1.88 } else {
1.89 - LogTo(Task,@"Closed output");
1.90 + LogTo(HgTaskVerbose, @"Closed output");
1.91 _outHandle = nil;
1.92 if( [self _shouldFinishUp] )
1.93 [self _finishUp];
1.94 @@ -245,12 +257,12 @@
1.95 NSData *data = [n.userInfo objectForKey: NSFileHandleNotificationDataItem];
1.96 if( data.length > 0 ) {
1.97 [_errHandle readInBackgroundAndNotifyForModes: _modes];
1.98 - LogTo(Task,@"Got %u bytes of stderr",data.length);
1.99 + LogTo(HgTaskVerbose, @"Got %u bytes of stderr",data.length);
1.100 [self willChangeValueForKey: @"errorData"];
1.101 [_errorData appendData: data];
1.102 [self didChangeValueForKey: @"errorData"];
1.103 } else {
1.104 - LogTo(Task,@"Closed stderr");
1.105 + LogTo(HgTaskVerbose, @"Closed stderr");
1.106 _errHandle = nil;
1.107 if( [self _shouldFinishUp] )
1.108 [self _finishUp];
1.109 @@ -261,7 +273,7 @@
1.110 - (void) _exited: (NSNotification*)n
1.111 {
1.112 _resultCode = _task.terminationStatus;
1.113 - LogTo(Task,@"Exited with result=%i",_resultCode);
1.114 + LogTo(HgTaskVerbose, @"Exited with result=%i",_resultCode);
1.115 _taskRunning = NO;
1.116 if( [self _shouldFinishUp] )
1.117 [self _finishUp];
1.118 @@ -275,14 +287,14 @@
1.119 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_finishUp) object: nil];
1.120 [self _close];
1.121
1.122 - LogTo(Task,@"Finished!");
1.123 + LogTo(HgTaskVerbose, @"Finished!");
1.124
1.125 if( _resultCode != 0 ) {
1.126 // Handle errors:
1.127 NSString *errStr = nil;
1.128 if( _errorData.length > 0 )
1.129 errStr = [[NSString alloc] initWithData: _errorData encoding: NSUTF8StringEncoding];
1.130 - Log(@" *** task returned %i: %@",_resultCode,errStr);
1.131 + LogTo(MYTask, @" *** task returned %i: %@",_resultCode,errStr);
1.132 if( errStr.length == 0 )
1.133 errStr = [NSString stringWithFormat: @"Command returned status %i",_resultCode];
1.134 NSString *desc = [NSString stringWithFormat: @"%@ command error", _task.launchPath.lastPathComponent];
1.135 @@ -351,7 +363,7 @@
1.136 // If output isn't valid UTF-8, fall back to CP1252, aka WinLatin1, a superset of ISO-Latin-1.
1.137 if( ! _output ) {
1.138 _output = [[NSString alloc] initWithData: _outputData encoding: NSWindowsCP1252StringEncoding];
1.139 - Log(@"Warning: Output of '%@' was not valid UTF-8; interpreting as CP1252",self);
1.140 + Warn(@"MYTask: Output of '%@' was not valid UTF-8; interpreting as CP1252",self);
1.141 }
1.142 }
1.143 return _output;