edc1591 edc1591 - 3 months ago 8x
Objective-C Question

Custom logging macro not accepting multiple arguments

I made a custom logging class that logs certain things to a file. I'm trying to make a macro so that I can use my custom class just like

, but it doesn't seem to be working right.

Here's how I'm defining the macro:

#define ECLog(fmt, ...) [ECLogger logText:fmt, ## __VA_ARGS__]

is declared like this:

+ (void)logText:(NSString *)theString;

If I only pass one argument, it works fine. Like this:


But if I pass another argument, like this:

ECLog(@"test %@",someString);

I get an error that it was only expecting one argument.

Any ideas?


No, the warning is about the method call. The error seems to be about the macro because you're viewing the code before the preprocessor has worked on it, but the compiler's pointing to the same line, post-processing.

You've declared this method as taking one argument, but you're passing it more than that.

ECLog(@"test %@", someString);

gets rewritten by the preprocessor to:

[ECLogger logText:@"test %@", someString];

That's the literal text that is now passed to the compiler, just as if you'd typed it that way yourself. The method is declared as taking a single NSString, but you're passing it two things. You need to change the declaration of the method:

+ (void) logText: (NSString *)theString, ...;