* MYTask: Added -commandLine, overhauled logging.
authorJens Alfke <jens@mooseyard.com>
Sun May 03 10:13:31 2009 -0700 (2009-05-03)
changeset 27256370e8935a
parent 26 252c13061ee5
child 28 206903ba35a5
* MYTask: Added -commandLine, overhauled logging.
* MYDirectoryWatcher: Overhauled logging.
* MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.)
MYDirectoryWatcher.m
MYErrorUtils.m
MYTask.h
MYTask.m
     1.1 --- a/MYDirectoryWatcher.m	Wed Apr 22 16:46:38 2009 -0700
     1.2 +++ b/MYDirectoryWatcher.m	Sun May 03 10:13:31 2009 -0700
     1.3 @@ -6,6 +6,8 @@
     1.4  //
     1.5  
     1.6  #import "MYDirectoryWatcher.h"
     1.7 +#import "Test.h"
     1.8 +#import "Logging.h"
     1.9  #import <CoreServices/CoreServices.h>
    1.10  
    1.11  
    1.12 @@ -29,7 +31,7 @@
    1.13  
    1.14  - (id) initWithDirectory: (NSString*)path target: (id)target action: (SEL)action
    1.15  {
    1.16 -    NSParameterAssert(path);
    1.17 +    Assert(path!=nil);
    1.18      self = [super init];
    1.19      if (self != nil) {
    1.20          _path = path.copy;
    1.21 @@ -76,7 +78,7 @@
    1.22              return NO;
    1.23          }
    1.24          _historyDone = (_lastEventID == kFSEventStreamEventIdSinceNow);
    1.25 -        Log(@"MYDirectoryWatcher: Started on %@ (latency=%g, lastEvent=%llu)",_path,_latency,_lastEventID);
    1.26 +        LogTo(MYDirectoryWatcher, @"Started on %@ (latency=%g, lastEvent=%llu)",_path,_latency,_lastEventID);
    1.27      }
    1.28      return YES;
    1.29  }
    1.30 @@ -88,7 +90,7 @@
    1.31          FSEventStreamInvalidate(_stream);
    1.32          FSEventStreamRelease(_stream);
    1.33          _stream = NULL;
    1.34 -        Log(@"MYDirectoryWatcher: Stopped on %@ (lastEvent=%llu)",_path,_lastEventID);
    1.35 +        LogTo(MYDirectoryWatcher, @"Stopped on %@ (lastEvent=%llu)",_path,_lastEventID);
    1.36      }
    1.37  }
    1.38  
    1.39 @@ -119,14 +121,14 @@
    1.40          FSEventStreamEventId eventID = eventIDs[i];
    1.41          if( flags & (kFSEventStreamEventFlagMount | kFSEventStreamEventFlagUnmount) ) {
    1.42              if( flags & kFSEventStreamEventFlagMount )
    1.43 -                Log(@"MYDirectoryWatcher: Volume mounted: %@",path);
    1.44 +                LogTo(MYDirectoryWatcher, @"Volume mounted: %@",path);
    1.45              else
    1.46 -                Log(@"MYDirectoryWatcher: Volume unmounted: %@",path);
    1.47 +                LogTo(MYDirectoryWatcher, @"Volume unmounted: %@",path);
    1.48          } else if( flags & kFSEventStreamEventFlagHistoryDone ) {
    1.49 -            Log(@"MYDirectoryWatcher: Event #%llu History done",eventID);
    1.50 +            LogTo(MYDirectoryWatcher, @"Event #%llu History done",eventID);
    1.51              _historyDone = YES;
    1.52          } else {
    1.53 -            Log(@"MYDirectoryWatcher: Event #%llu flags=%02x path=%@",eventID,flags,path);
    1.54 +            LogTo(MYDirectoryWatcher, @"Event #%llu flags=%02x path=%@",eventID,flags,path);
    1.55              if( _historyDone )
    1.56                  flags |= kFSEventStreamEventFlagHistoryDone;
    1.57              
    1.58 @@ -192,7 +194,7 @@
    1.59      NSString *base = watcher.path;
    1.60      if( ! [path hasPrefix: base] )
    1.61          return nil;
    1.62 -    int length = base.length;
    1.63 +    unsigned length = base.length;
    1.64      while( length < path.length && [path characterAtIndex: length]=='/' )
    1.65          length++;
    1.66      return [path substringFromIndex: length];
     2.1 --- a/MYErrorUtils.m	Wed Apr 22 16:46:38 2009 -0700
     2.2 +++ b/MYErrorUtils.m	Sun May 03 10:13:31 2009 -0700
     2.3 @@ -10,7 +10,10 @@
     2.4  #import "Test.h"
     2.5  #import "CollectionUtils.h"
     2.6  #import <Foundation/Foundation.h>
     2.7 +
     2.8 +#if USE_SECURITY_API
     2.9  #import <Security/SecBase.h>
    2.10 +#endif
    2.11  
    2.12  
    2.13  NSString* const MYErrorDomain = @"MYErrorDomain";
    2.14 @@ -134,12 +137,14 @@
    2.15          if (name && *name)
    2.16              result = [NSString stringWithCString: name encoding: NSMacOSRomanStringEncoding];
    2.17          else {
    2.18 +#if USE_SECURITY_API
    2.19              result = (id) SecCopyErrorMessageString(code,NULL);
    2.20              if (result) {
    2.21                  [(id)CFMakeCollectable(result) autorelease];
    2.22                  if ([result hasPrefix: @"OSStatus "])
    2.23                      result = nil; // just a generic message
    2.24              }
    2.25 +#endif
    2.26          }
    2.27      }
    2.28  #endif
     3.1 --- a/MYTask.h	Wed Apr 22 16:46:38 2009 -0700
     3.2 +++ b/MYTask.h	Sun May 03 10:13:31 2009 -0700
     3.3 @@ -51,6 +51,9 @@
     3.4  
     3.5  @property (copy) NSString* currentDirectoryPath;
     3.6  
     3.7 +/** Prettified description of command string. Doesn't do full shell-style quoting, though. */
     3.8 +- (NSString*) commandLine;
     3.9 +
    3.10  - (BOOL) run;
    3.11  - (BOOL) run: (NSError**)outError;
    3.12  
     4.1 --- a/MYTask.m	Wed Apr 22 16:46:38 2009 -0700
     4.2 +++ b/MYTask.m	Sun May 03 10:13:31 2009 -0700
     4.3 @@ -30,7 +30,7 @@
     4.4  - (id) initWithCommand: (NSString*)command
     4.5               arguments: (NSArray*)arguments
     4.6  {
     4.7 -    NSParameterAssert(command);
     4.8 +    Assert(command);
     4.9      self = [super init];
    4.10      if (self != nil) {
    4.11          _command = command;
    4.12 @@ -107,6 +107,18 @@
    4.13  }
    4.14  
    4.15  
    4.16 +- (NSString*) commandLine {
    4.17 +    NSMutableString *desc = [NSMutableString stringWithString: _command];
    4.18 +    for (NSString *arg in _arguments) {
    4.19 +        [desc appendString: @" "];
    4.20 +        if ([arg rangeOfString: @" "].length > 0)
    4.21 +            arg = [NSString stringWithFormat: @"'%@'", arg];
    4.22 +        [desc appendString: arg];
    4.23 +    }
    4.24 +    return desc;
    4.25 +}
    4.26 +
    4.27 +
    4.28  - (void) ignoreOutput
    4.29  {
    4.30      _ignoreOutput = YES;
    4.31 @@ -119,7 +131,7 @@
    4.32      va_start(args,fmt);
    4.33  
    4.34      NSString *message = [[NSString alloc] initWithFormat: fmt arguments: args];
    4.35 -    Log(@"MYTask Error: %@",message);
    4.36 +    LogTo(MYTask, @"Error: %@",message);
    4.37      NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObject: message
    4.38                                                                     forKey: NSLocalizedDescriptionKey];
    4.39      _error = [NSError errorWithDomain: MYTaskErrorDomain code: kMYTaskError userInfo: info];
    4.40 @@ -155,7 +167,7 @@
    4.41  /** Subclasses can override this. */
    4.42  - (NSTask*) createTask
    4.43  {
    4.44 -    NSAssert(!_task,@"createTask called twice");
    4.45 +    Assert(!_task,@"createTask called twice");
    4.46      NSTask *task = [[NSTask alloc] init];
    4.47      task.launchPath = _command;
    4.48      task.arguments = _arguments;
    4.49 @@ -167,14 +179,14 @@
    4.50  
    4.51  - (BOOL) start
    4.52  {
    4.53 -    NSAssert(!_task, @"Task has already been run");
    4.54 +    Assert(!_task, @"Task has already been run");
    4.55      if( _error )
    4.56          return NO;
    4.57      
    4.58      _task = [self createTask];
    4.59 -    NSAssert(_task,@"createTask returned nil");
    4.60 +    Assert(_task,@"createTask returned nil");
    4.61      
    4.62 -    Log(@"Task: %@ %@",_task.launchPath,[_task.arguments componentsJoinedByString: @" "]);
    4.63 +    LogTo(MYTask,@"$ %@", self.commandLine);
    4.64      
    4.65      _task.standardOutput = [self _openPipeAndHandle: &_outHandle notifying: @selector(_gotOutput:)];
    4.66      _outputData =  [[NSMutableData alloc] init];
    4.67 @@ -188,7 +200,7 @@
    4.68      @try{
    4.69          [_task launch];
    4.70      }@catch( id x ) {
    4.71 -        Log(@"Task failed to launch: %@",x);
    4.72 +        Warn(@"Task failed to launch: %@",x);
    4.73          _resultCode = 666;
    4.74          [self _close];
    4.75          return [self makeError: @"Exception launching %@: %@",_task.launchPath,x];
    4.76 @@ -221,7 +233,7 @@
    4.77      if( n.object == _outHandle ) {
    4.78          if( data.length > 0 ) {
    4.79              [_outHandle readInBackgroundAndNotifyForModes: _modes];
    4.80 -            LogTo(Task,@"Got %u bytes of output",data.length);
    4.81 +            LogTo(HgTaskVerbose, @"Got %u bytes of output",data.length);
    4.82              if( _outputData ) {
    4.83                  [self willChangeValueForKey: @"output"];
    4.84                  [self willChangeValueForKey: @"outputData"];
    4.85 @@ -231,7 +243,7 @@
    4.86                  [self didChangeValueForKey: @"output"];
    4.87              }
    4.88          } else {
    4.89 -            LogTo(Task,@"Closed output");
    4.90 +            LogTo(HgTaskVerbose, @"Closed output");
    4.91              _outHandle = nil;
    4.92              if( [self _shouldFinishUp] )
    4.93                  [self _finishUp];
    4.94 @@ -245,12 +257,12 @@
    4.95          NSData *data = [n.userInfo objectForKey: NSFileHandleNotificationDataItem];
    4.96          if( data.length > 0 ) {
    4.97              [_errHandle readInBackgroundAndNotifyForModes: _modes];
    4.98 -            LogTo(Task,@"Got %u bytes of stderr",data.length);
    4.99 +            LogTo(HgTaskVerbose, @"Got %u bytes of stderr",data.length);
   4.100              [self willChangeValueForKey: @"errorData"];
   4.101              [_errorData appendData: data];
   4.102              [self didChangeValueForKey: @"errorData"];
   4.103          } else {
   4.104 -            LogTo(Task,@"Closed stderr");
   4.105 +            LogTo(HgTaskVerbose, @"Closed stderr");
   4.106              _errHandle = nil;
   4.107              if( [self _shouldFinishUp] )
   4.108                  [self _finishUp];
   4.109 @@ -261,7 +273,7 @@
   4.110  - (void) _exited: (NSNotification*)n
   4.111  {
   4.112      _resultCode = _task.terminationStatus;
   4.113 -    LogTo(Task,@"Exited with result=%i",_resultCode);
   4.114 +    LogTo(HgTaskVerbose, @"Exited with result=%i",_resultCode);
   4.115      _taskRunning = NO;
   4.116      if( [self _shouldFinishUp] )
   4.117          [self _finishUp];
   4.118 @@ -275,14 +287,14 @@
   4.119      [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_finishUp) object: nil];
   4.120      [self _close];
   4.121  
   4.122 -    LogTo(Task,@"Finished!");
   4.123 +    LogTo(HgTaskVerbose, @"Finished!");
   4.124  
   4.125      if( _resultCode != 0 ) {
   4.126          // Handle errors:
   4.127          NSString *errStr = nil;
   4.128          if( _errorData.length > 0 )
   4.129              errStr = [[NSString alloc] initWithData: _errorData encoding: NSUTF8StringEncoding];
   4.130 -        Log(@"    *** task returned %i: %@",_resultCode,errStr);
   4.131 +        LogTo(MYTask, @"    *** task returned %i: %@",_resultCode,errStr);
   4.132          if( errStr.length == 0 )
   4.133              errStr = [NSString stringWithFormat: @"Command returned status %i",_resultCode];
   4.134          NSString *desc = [NSString stringWithFormat: @"%@ command error", _task.launchPath.lastPathComponent];
   4.135 @@ -351,7 +363,7 @@
   4.136          // If output isn't valid UTF-8, fall back to CP1252, aka WinLatin1, a superset of ISO-Latin-1.
   4.137          if( ! _output ) {
   4.138              _output = [[NSString alloc] initWithData: _outputData encoding: NSWindowsCP1252StringEncoding];
   4.139 -            Log(@"Warning: Output of '%@' was not valid UTF-8; interpreting as CP1252",self);
   4.140 +            Warn(@"MYTask: Output of '%@' was not valid UTF-8; interpreting as CP1252",self);
   4.141          }
   4.142      }
   4.143      return _output;