Enable and Disable NSLog in DEBUG mode

Objective CCocoaNslog

Objective C Problem Overview


I want to enable NSLog when I am in debug and disable it otherwise. A very simple thing is:

#ifdef DEBUG
NSLog(@"My log");
#endif

But all this #ifdef and #endif is borring... :( So I try other thing: (.pch is good place to put it)

#ifdef DEBUG
#   define NSLog(text) NSLog(text);
#else 
#   define NSLog(text) 
#endif

This work very fine (isn't recursive). But the problem is that NSLog have infinite arguments.

void NSLog(NSString *format, ...)

How I solve this to work in preprocessor mode?

-- Edit --

This code make your NSLog better:

#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(...)
#endif

Objective C Solutions


Solution 1 - Objective C

This should do the trick:

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

Solution 2 - Objective C

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

Solution 3 - Objective C

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.

Solution 4 - Objective C

#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.

Solution 5 - Objective C

Here is a nice trick... add to any .m

#define EXTRANSLog if([[NSUserDefaults standardUserDefaults] boolForKey:@"SomeFancyKey"] == YES) NSLog 

Replace any

NSLog(@"????");

with

EXTRANSLog(@"????");

In this example, I created a NSUser key and set the BOOL to YES,, use some form of switch etc to change the key to NO or remove altogether if you don't want to view the EXTRANSLog's via console debugger.

I use this when troubleshooting and don't want all the excessive logs to appear. Only when SomeFancyKey == YES.

This is the same as

#define NSLog if(1) NSLog

where 1 is YES show NSLog, and 0 is NO.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodrigoView Question on Stackoverflow
Solution 1 - Objective CJustSidView Answer on Stackoverflow
Solution 2 - Objective CAram KocharyanView Answer on Stackoverflow
Solution 3 - Objective CNaXirView Answer on Stackoverflow
Solution 4 - Objective CPrashantView Answer on Stackoverflow
Solution 5 - Objective CCanea RowanView Answer on Stackoverflow