# HG changeset patch # User Jens Alfke # Date 1241370811 25200 # Node ID 256370e8935a99012d2245b1aab858128acf563d # Parent 252c13061ee5743ca4240d53e6f10114a85153be * MYTask: Added -commandLine, overhauled logging. * MYDirectoryWatcher: Overhauled logging. * MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.) diff -r 252c13061ee5 -r 256370e8935a MYDirectoryWatcher.m --- a/MYDirectoryWatcher.m Wed Apr 22 16:46:38 2009 -0700 +++ b/MYDirectoryWatcher.m Sun May 03 10:13:31 2009 -0700 @@ -6,6 +6,8 @@ // #import "MYDirectoryWatcher.h" +#import "Test.h" +#import "Logging.h" #import @@ -29,7 +31,7 @@ - (id) initWithDirectory: (NSString*)path target: (id)target action: (SEL)action { - NSParameterAssert(path); + Assert(path!=nil); self = [super init]; if (self != nil) { _path = path.copy; @@ -76,7 +78,7 @@ return NO; } _historyDone = (_lastEventID == kFSEventStreamEventIdSinceNow); - Log(@"MYDirectoryWatcher: Started on %@ (latency=%g, lastEvent=%llu)",_path,_latency,_lastEventID); + LogTo(MYDirectoryWatcher, @"Started on %@ (latency=%g, lastEvent=%llu)",_path,_latency,_lastEventID); } return YES; } @@ -88,7 +90,7 @@ FSEventStreamInvalidate(_stream); FSEventStreamRelease(_stream); _stream = NULL; - Log(@"MYDirectoryWatcher: Stopped on %@ (lastEvent=%llu)",_path,_lastEventID); + LogTo(MYDirectoryWatcher, @"Stopped on %@ (lastEvent=%llu)",_path,_lastEventID); } } @@ -119,14 +121,14 @@ FSEventStreamEventId eventID = eventIDs[i]; if( flags & (kFSEventStreamEventFlagMount | kFSEventStreamEventFlagUnmount) ) { if( flags & kFSEventStreamEventFlagMount ) - Log(@"MYDirectoryWatcher: Volume mounted: %@",path); + LogTo(MYDirectoryWatcher, @"Volume mounted: %@",path); else - Log(@"MYDirectoryWatcher: Volume unmounted: %@",path); + LogTo(MYDirectoryWatcher, @"Volume unmounted: %@",path); } else if( flags & kFSEventStreamEventFlagHistoryDone ) { - Log(@"MYDirectoryWatcher: Event #%llu History done",eventID); + LogTo(MYDirectoryWatcher, @"Event #%llu History done",eventID); _historyDone = YES; } else { - Log(@"MYDirectoryWatcher: Event #%llu flags=%02x path=%@",eventID,flags,path); + LogTo(MYDirectoryWatcher, @"Event #%llu flags=%02x path=%@",eventID,flags,path); if( _historyDone ) flags |= kFSEventStreamEventFlagHistoryDone; @@ -192,7 +194,7 @@ NSString *base = watcher.path; if( ! [path hasPrefix: base] ) return nil; - int length = base.length; + unsigned length = base.length; while( length < path.length && [path characterAtIndex: length]=='/' ) length++; return [path substringFromIndex: length]; diff -r 252c13061ee5 -r 256370e8935a MYErrorUtils.m --- a/MYErrorUtils.m Wed Apr 22 16:46:38 2009 -0700 +++ b/MYErrorUtils.m Sun May 03 10:13:31 2009 -0700 @@ -10,7 +10,10 @@ #import "Test.h" #import "CollectionUtils.h" #import + +#if USE_SECURITY_API #import +#endif NSString* const MYErrorDomain = @"MYErrorDomain"; @@ -134,12 +137,14 @@ if (name && *name) result = [NSString stringWithCString: name encoding: NSMacOSRomanStringEncoding]; else { +#if USE_SECURITY_API result = (id) SecCopyErrorMessageString(code,NULL); if (result) { [(id)CFMakeCollectable(result) autorelease]; if ([result hasPrefix: @"OSStatus "]) result = nil; // just a generic message } +#endif } } #endif diff -r 252c13061ee5 -r 256370e8935a MYTask.h --- a/MYTask.h Wed Apr 22 16:46:38 2009 -0700 +++ b/MYTask.h Sun May 03 10:13:31 2009 -0700 @@ -51,6 +51,9 @@ @property (copy) NSString* currentDirectoryPath; +/** Prettified description of command string. Doesn't do full shell-style quoting, though. */ +- (NSString*) commandLine; + - (BOOL) run; - (BOOL) run: (NSError**)outError; diff -r 252c13061ee5 -r 256370e8935a MYTask.m --- a/MYTask.m Wed Apr 22 16:46:38 2009 -0700 +++ b/MYTask.m Sun May 03 10:13:31 2009 -0700 @@ -30,7 +30,7 @@ - (id) initWithCommand: (NSString*)command arguments: (NSArray*)arguments { - NSParameterAssert(command); + Assert(command); self = [super init]; if (self != nil) { _command = command; @@ -107,6 +107,18 @@ } +- (NSString*) commandLine { + NSMutableString *desc = [NSMutableString stringWithString: _command]; + for (NSString *arg in _arguments) { + [desc appendString: @" "]; + if ([arg rangeOfString: @" "].length > 0) + arg = [NSString stringWithFormat: @"'%@'", arg]; + [desc appendString: arg]; + } + return desc; +} + + - (void) ignoreOutput { _ignoreOutput = YES; @@ -119,7 +131,7 @@ va_start(args,fmt); NSString *message = [[NSString alloc] initWithFormat: fmt arguments: args]; - Log(@"MYTask Error: %@",message); + LogTo(MYTask, @"Error: %@",message); NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObject: message forKey: NSLocalizedDescriptionKey]; _error = [NSError errorWithDomain: MYTaskErrorDomain code: kMYTaskError userInfo: info]; @@ -155,7 +167,7 @@ /** Subclasses can override this. */ - (NSTask*) createTask { - NSAssert(!_task,@"createTask called twice"); + Assert(!_task,@"createTask called twice"); NSTask *task = [[NSTask alloc] init]; task.launchPath = _command; task.arguments = _arguments; @@ -167,14 +179,14 @@ - (BOOL) start { - NSAssert(!_task, @"Task has already been run"); + Assert(!_task, @"Task has already been run"); if( _error ) return NO; _task = [self createTask]; - NSAssert(_task,@"createTask returned nil"); + Assert(_task,@"createTask returned nil"); - Log(@"Task: %@ %@",_task.launchPath,[_task.arguments componentsJoinedByString: @" "]); + LogTo(MYTask,@"$ %@", self.commandLine); _task.standardOutput = [self _openPipeAndHandle: &_outHandle notifying: @selector(_gotOutput:)]; _outputData = [[NSMutableData alloc] init]; @@ -188,7 +200,7 @@ @try{ [_task launch]; }@catch( id x ) { - Log(@"Task failed to launch: %@",x); + Warn(@"Task failed to launch: %@",x); _resultCode = 666; [self _close]; return [self makeError: @"Exception launching %@: %@",_task.launchPath,x]; @@ -221,7 +233,7 @@ if( n.object == _outHandle ) { if( data.length > 0 ) { [_outHandle readInBackgroundAndNotifyForModes: _modes]; - LogTo(Task,@"Got %u bytes of output",data.length); + LogTo(HgTaskVerbose, @"Got %u bytes of output",data.length); if( _outputData ) { [self willChangeValueForKey: @"output"]; [self willChangeValueForKey: @"outputData"]; @@ -231,7 +243,7 @@ [self didChangeValueForKey: @"output"]; } } else { - LogTo(Task,@"Closed output"); + LogTo(HgTaskVerbose, @"Closed output"); _outHandle = nil; if( [self _shouldFinishUp] ) [self _finishUp]; @@ -245,12 +257,12 @@ NSData *data = [n.userInfo objectForKey: NSFileHandleNotificationDataItem]; if( data.length > 0 ) { [_errHandle readInBackgroundAndNotifyForModes: _modes]; - LogTo(Task,@"Got %u bytes of stderr",data.length); + LogTo(HgTaskVerbose, @"Got %u bytes of stderr",data.length); [self willChangeValueForKey: @"errorData"]; [_errorData appendData: data]; [self didChangeValueForKey: @"errorData"]; } else { - LogTo(Task,@"Closed stderr"); + LogTo(HgTaskVerbose, @"Closed stderr"); _errHandle = nil; if( [self _shouldFinishUp] ) [self _finishUp]; @@ -261,7 +273,7 @@ - (void) _exited: (NSNotification*)n { _resultCode = _task.terminationStatus; - LogTo(Task,@"Exited with result=%i",_resultCode); + LogTo(HgTaskVerbose, @"Exited with result=%i",_resultCode); _taskRunning = NO; if( [self _shouldFinishUp] ) [self _finishUp]; @@ -275,14 +287,14 @@ [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_finishUp) object: nil]; [self _close]; - LogTo(Task,@"Finished!"); + LogTo(HgTaskVerbose, @"Finished!"); if( _resultCode != 0 ) { // Handle errors: NSString *errStr = nil; if( _errorData.length > 0 ) errStr = [[NSString alloc] initWithData: _errorData encoding: NSUTF8StringEncoding]; - Log(@" *** task returned %i: %@",_resultCode,errStr); + LogTo(MYTask, @" *** task returned %i: %@",_resultCode,errStr); if( errStr.length == 0 ) errStr = [NSString stringWithFormat: @"Command returned status %i",_resultCode]; NSString *desc = [NSString stringWithFormat: @"%@ command error", _task.launchPath.lastPathComponent]; @@ -351,7 +363,7 @@ // If output isn't valid UTF-8, fall back to CP1252, aka WinLatin1, a superset of ISO-Latin-1. if( ! _output ) { _output = [[NSString alloc] initWithData: _outputData encoding: NSWindowsCP1252StringEncoding]; - Log(@"Warning: Output of '%@' was not valid UTF-8; interpreting as CP1252",self); + Warn(@"MYTask: Output of '%@' was not valid UTF-8; interpreting as CP1252",self); } } return _output;