This is a better way to use NSLog. Adding the next snipped of code in your app pch file (app-name-Prefix.pch) and then replacing all the “NSLog” of your project for “DebugLog” we can improve the performance of the app when it is not in the debug mode.
Another feature is that this code also shows the File name, line number and method name for each log. I added some more stuff so all the logs appears aligned by columns and are easy to read.

1 .Add this code in your pch file.

// Shows the logs just in the debug mode with the fileName, line number and method.
#ifdef DEBUG
#define DebugLog( s, ... ) NSLog( @"[FILE]%@ %*s [LINE]%-*d [METHOD]%@ %*s [MESSAGE]%@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent],30 - ([[[NSString stringWithUTF8String:__FILE__] lastPathComponent] length]),"", 5,__LINE__, NSStringFromSelector(_cmd), 75 - ([NSStringFromSelector(_cmd) length]),"", [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#define DebugLog( s, ... )

2. Replace the NSLog for DebugLog.

 // Before:

NSLog(@"Log with some message");

// After:
DebugLog(@"Log with some message");



 // Results Before:

Log with some message

// Results After:

[FILE]ViewController.m [LINE]30 [METHOD]viewDidLoad [MESSAGE]Log with some message


Hope this is helpful guys!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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