preprocessor marcros : How to override NSLog ?

Discussion in 'iPhone/iPad Programming' started by Sendel, Feb 24, 2009.

  1. macrumors newbie

    Joined:
    Feb 5, 2009
    #1
    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.
     
  2. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    shouldn't it be #ifdef DEBUG
     
  3. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #3
    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 ?
     
  4. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #4
    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."];
     
  5. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #5
    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
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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).
     

Share This Page