View Full Version : preprocessor marcros : How to override NSLog ?

Feb 24, 2009, 06:55 AM

i simply want to override the NSLog function when I generate the RELEASE version of an iPhone app.

so it should look like this :

#define MyLog (x) NSLog(x)
#define MyLog (x) ;

but it does not work, seems you cannot use parameters in preprocessor macros like you can do in c/c++ ?

If you have annother more effective solution to override NSLog, that would help me, too.

thank you in advance.

Feb 24, 2009, 07:03 AM
shouldn't it be #ifdef DEBUG

Feb 24, 2009, 07:08 AM
the line :

#define ALog(X) NSLog(X)

is causing the error. Actually there is no #ifdef #else #endif in my code yet. All I tried was to replace the name NSLog by ALog, which does fine with :
#define ALog NSLog

but then I cannot override the function with :
#define ALog(X) ;

the whole idea is to use everywhere
ALog(@"Name=%@", name);

and then replaces it to :


may be there is another way to tell NSLog to turn off its functionality ?

Feb 24, 2009, 07:24 AM
this works for me now:

#ifdef DEBUG
#define ALog NSLog
#define ALog //NSLog

if I keep care of not to add another command to the same line it would comment out the NSLog like

ALog(@"Count = %i", count); [self dont:@"this is no good."];

// NSLog(@"Count = %i", count); [self dont:@"this is no good."];

NSLog(@"Count = %i", count); [self dont:@"this is no good."];

Feb 24, 2009, 08:42 AM
#define ALog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
#define MARK ALog(@"%s", __PRETTY_FUNCTION__);
#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; ALog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
#define ALog(format, ...)
#define MARK
#define END_TIMER(msg)

Feb 24, 2009, 09:48 AM
...seems you cannot use parameters in preprocessor macros like you can do in c/c++ ?

#define ALog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
See, you can use parameters with preprocessor macros with Objective-C. You just have to be careful how you use it. I think your problem was you were trying to define it on a method that takes a variable number of arguments. For example,
#define SQUARE(x) ( (x) * (x) )
is perfectly legitimate. (Tip o' the hat to Stephen Kochan for this example).