PDA

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




Sendel
Feb 24, 2009, 06:55 AM
Hi,

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

so it should look like this :

#if DEBUG
#define MyLog (x) NSLog(x)
#else
#define MyLog (x) ;
#endif

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.



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

Sendel
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 ?

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


#ifdef DEBUG
#define ALog NSLog
#else
#define ALog //NSLog
#endif


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

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


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


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

Sendel
Feb 24, 2009, 08:42 AM
#if DEBUG
#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]);
#else
#define ALog(format, ...)
#define MARK
#define START_TIMER
#define END_TIMER(msg)
#endif

dejo
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).