Posted Tuesday 17th January ‘12.

Alternatives to NSLog

In Xcode you can use NSLog to display information from your app without it distrupting the actual UI of the app.

This works well but is limiting in a way, and someone on freenode today linked me to this post on stackoverflow.

Below you can see some code you would add into your .pch file:

#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#ifdef DEBUG
#   define ULog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#else
#   define ULog(...)
#endif

Using the above code you can now call some new functions within your app, these being:

  • ALog() will display the standard NSLog but containing function and line number.
  • DLog() will output like NSLog only when the DEBUG variable is set
  • ULog() will show the UIAlertView only when the DEBUG variable is set

Each one of these will also include the line number and function that created it, for example, ALog(@"Hello world"); would display as -[LibraryController awakeFromNib] [Line 364] Hello world.

I hope this helps someone out!.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
By submitting this form, you accept the Mollom privacy policy.