There is something that I fundamentally don’t understand because I cannot for the life of me get this to work. I want to create a macro that extends NSLog(NSString *format, ...). Here is what I came up with:
NSLogExtensions.h
#define DebugLog(debugMode, format, ...) MyDebugLog(debugMode, [[NSString stringWithFormat:@"<%@> (Line %d): ", NSStringFromClass([self class]), __LINE__] stringByAppendingString:format], ##__VA_ARGS__)
@interface NSLogExtensions : NSObject
@end
NSLogExtensions.m
#import "NSLogExtensions.h"
@implementation NSLogExtensions
void MyDebugLog(bool debugMode, NSString *format, ...) {
va_list(argumentList);
va_start(argumentList, format);
if (debugMode) {
NSLogv(format, argumentList);
}
va_end(argumentList);
}
@end
I expected to be able to include the NSLogExtensions header file and then be able to use the macro, but I receive the GCC implicit function declaration warning still.
The
DebugLogmacro uses the functionMyDebugLog, therefore you must add the prototypeto NSLogExtensions.h. Then it compiles without warnings.
BUT your solution has one (in my opinion) great disadvantage compared to the solutions that @calvinBhai refers to: Even if you switch off logging by setting
debugModetofalse, theDebugLogmacro will evaluate all arguments and callMyDebugLog. With the other solutions, the preprocessor will remove everything if logging is disabled.