11

I want to define a macro to call the following, Is this possible? I also want it to accept format string.

- (void)logString:(NSString *)string withLogLogLevel:(LogLevel)logLevel { // Sav log to file } DLog("text"); [Logger logString:text withLogLevel:LogLevelDebug]; ILog("text"); [Logger logString:text withLogLevel:LogLevelInfo]; ELog("text"); [Logger logString:text withLogLevel:LogLevelInfo]; 
3
  • It's not quite clear what you want. Commented Dec 13, 2012 at 21:37
  • I want to define a macro that calls a method with required parameters Commented Dec 13, 2012 at 21:38
  • 2
    While it's pretty easy to do what you've asked (see @dasblinkenlight's answer for a very simple approach), the logging problem is somewhat complex to do well, and there are good pre-built solutions to take care of this for you. See github.com/robbiehanson/CocoaLumberjack for a good example. Commented Dec 13, 2012 at 21:43

1 Answer 1

10

Assuming that logString:withLogLevel: takes a single string parameter in addition to the log level, this should be possible:

#define DLog(x) [Logger logString:(x) withLogLevel:LogLevelDebug] 

Note the parentheses around the macro parameter, it is useful when macros are called with composite expressions.

Assuming that the logger takes NSString objects, not C string, you should use the macro like this:

DLog(@"Text");

However, in this case it is not clear why would one prefer a macro to a simple function call:

void DLog(NSString *str) { [Logger logString:str withLogLevel:LogLevelDebug]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

__VA_ARGS__ are worth mentioning, IMHO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.