How do I debug with NSLog(@“Inside of the iPhone Simulator”)?

后端 未结 6 1835
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 05:09

I\'m used to programming and having log messages be viewable. I know you used to be able to use NSLog() to trace out messages when debugging Cocoa applications.

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 05:25

    In my project I have a customised solution based on DebugOutput.m This adds the file & line number to the debug output, making it easier to identify where that output text is coming from, while still keeping it brief.

    I've augmented the standard solution with a debug mask, so that I can switch debugging on and off for particular areas of functionality in my app. In Debug.h, I have

    typedef enum {
    kDebugMaskAp- = 1,
    kDebugMaskXMLParser = 1 << 1,
    kDebugMaskNetwork = 1 << 2,
    kDebugMaskAnalytics = 1 << 3,
    kDebugMaskCache = 1 << 4,
    } debugBitMask;
    
    #define debugForComponent(mask,format,...) if( currentDebugMask() & mask) [[DebugOutput sharedDebug]  output:__FILE__ lineNumber:__LINE__ input:(format), ##__VA_ARGS__]
    

    And in Debug.m

    -(void)output:(char*)fileName lineNumber:(int)lineNumber input:(NSString*)input, ...
    {
      va_list argList;
      NSString *filePath, *formatStr;
    
      // Build the path string
      filePath = [[NSString alloc] initWithBytes:fileName length:strlen(fileName) encoding:NSUTF8StringEncoding];
    
      // Process arguments, resulting in a format string
      va_start(argList, input);
      formatStr = [[NSString alloc] initWithFormat:input arguments:argList];
      va_end(argList);
    
      // Call NSLog, prepending the filename and line number
      NSLog(@"File:%s Line:%d %@",[((DEBUG_SHOW_FULLPATH) ? filePath : [filePath lastPathComponent]) UTF8String], lineNumber, formatStr);
    
      [filePath release];
      [formatStr release];
    }
    

    In the application, calls look something like this:

    debugForComponent(kDebugMaskApp,@"Request failed - error %@", [error localizedDescription]);
    

提交回复
热议问题