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 apps. What is the best way to "trace" messages when coding in a iPhone XCode development environment?
views:
14065answers:
5NSLog :)
The output is piped to the console window in XCode and the log files on the iPhone (which are viewable through the XCode device manager).
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]);
I simply use the replace all functionality....
I disable all my NSLog statements by replacing NSLog(@" with //***NSLog(@"
That way I can simply find it (using find in all project files) with //***NSLog(@" and re-enable them
Nothing fancy but it works :)
Here's a great bit of code I picked up somewhere on the web. It defines new functions DLog() and ALog(). DLog messages only appear when the app is compiled with a -DDEBUG flag (define DEBUG). ALog messages ALWAYS appear (even in Release mode).
// DLog is almost a drop-in replacement for NSLog
// DLog();
// DLog(@"here");
// DLog(@"value: %d", x);
// Unfortunately this doesn't work DLog(aStringVariable); you have to do this instead DLog(@"%@", aStringVariable);
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);