Debug Logging in iOS Objective-C Apps

So recently I’ve been working on Veteris (My App Store client/server) and YewTube (My YouTube client) and needed a better way to add logging to my apps without it logging on release builds.

Whilst searching for a solution I found a ancient Ars Technica article pointing to this link, Of course the links on the article are dead but I was able to extract enough information to get it working for me. Here’s how I implemented it.

So first, in a class you’d like to use, I used the AppDelegate you just need to add these lines to your header.

#ifdef DEBUG
#define DebugLog(args...) _DebugLog(__FILE__,__LINE__,args);
#define DebugLog(x...)

@interface YourClass: NSObject
void _DebugLog(const char *file, int lineNumber, NSString *format,...);

And you need to add these lines to the implementation.

void _DebugLog(const char *file, int lineNumber, NSString *message,...) {
  NSString *string = [[NSString alloc] initWithUTF8String:file];
  NSLog(@"%@:%d: %@", string, lineNumber, message);

Of course, there is probably a better way to write this implementation but I’m content with this. You can call the function like so.

DebugLog([NSString stringWithFormat:@"VAPIDeviceString = %@", [VAPIHelper getVAPIDeviceString]]);

Thank you for coming to my TED Talk and I hope someone finds this useful now, or in the future.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.