Search code examples
objective-ccocoanslog

What are some unique uses of NSLog you have used?


Do you have any unique or special uses of NSLog you use for debugging?


Solution

  • I like to use this format for debugging.

    NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    

    Of course, you'll want to wrap this in your own method or function for ease of use. I use the preprocessor and also only enable it for my own use and special builds I send out to beta testers. This is copied from my answer to this question, by the way.

    #define DEBUG_MODE
    
    #ifdef DEBUG_MODE
        #define DebugLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    #else
        #define DebugLog( s, ... ) 
    #endif
    

    This makes DebugLog act just like NSLog, but displays the filename and line-number where it was called:

    2009-05-23 17:23:40.920 myproject[92523:10b] <AppCon.m:(8)> My debug message...