Search code examples
iosobjective-cmacrosc-preprocessortestflight

Define a macro to replace code


I'm using TestFlight and I've got this macro to replace NSLog the the TestFlight remote logging equivalent.

 #define NSLog(__FORMAT__, ...) TFLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

I have just copy pasted this from the TestFlight website and now I'd like to create my own for a slightly different purpose.

I'd like to be able to type...

MyEventLog(@"Something happened.");

...and for it to interpret it as...

[[MyEventLogManager sharedInstance] newLogWithText:@"Something happened"];

I'm just not sure how the syntax works.


Solution

  • #define MyEventLog(message) [[MyEventLogManager sharedInstance] newLogWithText:message]
    

    However you will find it useful to provide a varargs-version of that method, so you can pass it formatted text:

    - (void)newLogWithFormat:(NSString *)format, ...
    {
        va_list va;
        va_start(va, format);
        NSString *message = [[NSString alloc] initWithFormat:format arguments:va];
        va_end(va);
    
        [self newLogWithText:message];
    
        // If not using ARC, then:
        // [message release];
    }
    

    and use:

    #define MyEventFormat(__FORMAT__, ...) [[MyEventLogManager sharedInstance] newLogWithFormat:__FORMAT__, ##__VA_ARGS__]