Commits

Jens Alfke committed 256370e

* MYTask: Added -commandLine, overhauled logging.
* MYDirectoryWatcher: Overhauled logging.
* MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.)

Comments (0)

Files changed (4)

MYDirectoryWatcher.m

 //
 
 #import "MYDirectoryWatcher.h"
+#import "Test.h"
+#import "Logging.h"
 #import <CoreServices/CoreServices.h>
 
 
 
 - (id) initWithDirectory: (NSString*)path target: (id)target action: (SEL)action
 {
-    NSParameterAssert(path);
+    Assert(path!=nil);
     self = [super init];
     if (self != nil) {
         _path = path.copy;
             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;
 }
         FSEventStreamInvalidate(_stream);
         FSEventStreamRelease(_stream);
         _stream = NULL;
-        Log(@"MYDirectoryWatcher: Stopped on %@ (lastEvent=%llu)",_path,_lastEventID);
+        LogTo(MYDirectoryWatcher, @"Stopped on %@ (lastEvent=%llu)",_path,_lastEventID);
     }
 }
 
         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;
             
     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];
 #import "Test.h"
 #import "CollectionUtils.h"
 #import <Foundation/Foundation.h>
+
+#if USE_SECURITY_API
 #import <Security/SecBase.h>
+#endif
 
 
 NSString* const MYErrorDomain = @"MYErrorDomain";
         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
 
 @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;
 
 - (id) initWithCommand: (NSString*)command
              arguments: (NSArray*)arguments
 {
-    NSParameterAssert(command);
+    Assert(command);
     self = [super init];
     if (self != nil) {
         _command = command;
 }
 
 
+- (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;
     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];
 /** 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;
 
 - (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];
     @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];
     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"];
                 [self didChangeValueForKey: @"output"];
             }
         } else {
-            LogTo(Task,@"Closed output");
+            LogTo(HgTaskVerbose, @"Closed output");
             _outHandle = nil;
             if( [self _shouldFinishUp] )
                 [self _finishUp];
         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];
 - (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];
     [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];
         // 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;