Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sendel

macrumors newbie
Original poster
Feb 5, 2009
5
0
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.
 

Sendel

macrumors newbie
Original poster
Feb 5, 2009
5
0
the line :

Code:
#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 :
Code:
#define ALog  NSLog

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

the whole idea is to use everywhere
Code:
..
ALog(@"Name=%@", name);
..
and then replaces it to :
Code:
..
;
..

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

Sendel

macrumors newbie
Original poster
Feb 5, 2009
5
0
One Solution could be :

this works for me now:


PHP:
#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 :
PHP:
ALog(@"Count = %i", count); [self dont:@"this is no good."];


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


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

Sendel

macrumors newbie
Original poster
Feb 5, 2009
5
0
Ultimative Solution

#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:mad:"%@ Time = %f", msg, stop-start]);
#else
#define ALog(format, ...)
#define MARK
#define START_TIMER
#define END_TIMER(msg)
#endif
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...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,
Code:
#define SQUARE(x) ( (x) * (x) )
is perfectly legitimate. (Tip o' the hat to Stephen Kochan for this example).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.