What unique uses of NSLog have you used?
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 will want to wrap this in your own method or function for ease of use. I use a preprocessor and also only allow it for my own use and special assemblies that I send to beta testers. This, by the way, is copied from my answer to this question.
#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 does DebugLog
the same thing as it does NSLog
, but displays the filename and line number at which it was called:
2009-05-23 17:23:40.920 myproject[92523:10b] <AppCon.m:(8)> My debug message...
a source to share
I am using some macros with NSLog to quickly debug the contents of cocoa NSPoint
, NSSize
and NSRect
structs:
#define LogPoint(POINT) CMLog(@"%s: (%0.0f, %0.0f)",\
#POINT, POINT.x, POINT.y)
#define LogSize(SIZE) CMLog(@"%s: %0.0f x %0.0f",\
#SIZE, SIZE.width, SIZE.height)
#define LogRect(RECT) CMLog(@"%s: (%0.0f, %0.0f) %0.0f x %0.0f",\
#RECT, RECT.origin.x, RECT.origin.y,\
RECT.size.width, RECT.size.height)
They can be used as follows:
LogPoint(somePoint); LogSize(someSize); LogRect(someRect);
And they produce the following output:
somePoint: (100, 200)
someSize: 12 x 440
someRect: (120, 240) 326 x 74
a source to share
This is not an NSLog function, however it is very handy for use with NSLog: you can use% @placeholder on objects and their descriptions will be displayed:
NSLog (@"The object is %@", someKindOfObjectWhichYouWantToDisplay);
This way you can, for example, see what kind of object you get for example. This works by sending a "description" to the object selector, which of course can be implemented in your own objects.
a source to share
Here's how you can separate some parts of the debug logs to make them more readable:
// MyDebugStuff.h:
void MyLog_Indent();
void MyLog_Outdent();
void MyLog(NSString * format, ...);
// MyDebugStuff.m:
int logIndentLevel = 0;
void MyLog_Indent() { logIndentLevel++; }
void MyLog_Outdent() { if (logIndentLevel > 0) { logIndentLevel--; } }
void MyLog(NSString * format, ...)
{
va_list args;
va_start(args, format);
NSString * indentString = [[NSString stringWithString:@""]
stringByPaddingToLength:(2*LogIndentLevel)
withString:@" "
startingAtIndex:0];
NSLogv([NSString stringWithFormat:@"%@%@", indentString, format], args);
va_end(args);
}
It can be used like this:
MyLog(@"Hello, world");
MyLog_Indent();
MyLog(@"Step 1");
MyLog(@"Step 2");
MyLog_Indent();
MyLog(@"Step 2a");
MyLog(@"Step 2b");
MyLog_Outdent();
MyLog(@"Step 3");
MyLog_Outdent();
MyLog(@"Goodbye, cruel world!");
And will produce:
Hello, world
Step 1
Step 2
Step 2a
Step 2b
Step 3
Goodbye, cruel world!
a source to share