Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable and Disable NSLog in DEBUG mode

This should do the trick:

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...) (void)0
 #endif

This is a bit shorter and also disables NSLog when using a device. If you're writing a game, NSLogs sent often can reduce your FPS from 60 to 20.

#if !defined(DEBUG) || !(TARGET_IPHONE_SIMULATOR)
    #define NSLog(...)
#endif

All the above answers are correct. I can suggest you to do it in a following way also. Suppose i have a if statement with no brackets

if(x==5)
NSLog("x is 5");

What will happen if it will replace NSLog with no statement. So we can simply replace it with an empty loop.

#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...) do {} while (0)
#endif

This statement will run an empty loop once. This will safely remove your NSLog from all of your live code.


#ifndef Debug
    #define Debug 1
#endif

#if Debug
#   define DebugLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DebugLog(...)
#endif

Set Debug to 1 for enabling the log and 0 for disabling it.