0

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 NSLog(), 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__]

logText: is declared like this:

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

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

ECLog(@"test");

But if I pass another argument, like this:

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

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

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
edc1591
  • 10,146
  • 6
  • 40
  • 63

1 Answers1

2

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, ...; 
jscs
  • 63,694
  • 13
  • 151
  • 195
  • OK, that takes care of the error, but how do I combine all the arguments into one `NSString` in my `logText:` method? – edc1591 Mar 21 '12 at 01:57
  • How were you doing it before? Aren't you processing the arguments as a `va_list` inside `logText:`? – jscs Mar 21 '12 at 01:58
  • No, I was just taking `theString` and writing it to a file. – edc1591 Mar 21 '12 at 02:00