Display Debug Information In A Popup Window (UIAlertView)

I previously shared the code I use to replace NSLog, as I really don’t care for the date/time stamp that NSLog outputs to the console.

Every now and again rather than looking at the console, it can be helpful to have a popup (UIAlertView) with the same information that one would include in NSLog. Let’s see how to accomplish this…

Assume you have the following code:

NSString *str = @"iOS Developer Tips";
int x = 101;
 
NSLog(@"str: %@ x: %d", str, x);

The output will look as follows:

2013-04-07 23:28:42.404 Sandbox[18767:c07] str: iOS Developer Tips x: 101

As I wrote in my previous post, I prefer the debug macro as follows:

#ifdef DEBUG
  #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
  #define debug(format, ...) 
#endif

Now the output looks like this:

str: iOS Developer Tips x: 101

We can add another macro for a popup debug message, and as a bonus include the method signature and source code line number:

#ifdef DEBUG
  #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
  #define debugAlert(format, ...)  {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n line: %d ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:format, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil]; [alert show]; [alert release];}
#else
  #define debug(format, ...) 
  #define debugAlert(format, ...)
#endif

Here is the pop alert version: