Chris
Chris

Reputation: 2907

Disallow NSLog to be used

Is it possible to disallow the use of NSLog, so that it will come up as an error if used at compile time? Ideally some sort of compiler flag with the name of the method that is disallowed?

Thanks

Upvotes: 2

Views: 274

Answers (3)

Martin R
Martin R

Reputation: 539765

If you re-declare NSLog (and perhaps also NSLogv) as

void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;

in your precompiled header file, you get a nice error message:

main.m:199:3: error: 'NSLog' is unavailable
                NSLog(@"%@", s1);
                ^

You can even provide a custom error message (found in Messages on deprecated and unavailable Attributes of the Clang documentation):

void NSLog(NSString *format, ...) __attribute__((unavailable("You should not do this!")));

main.m:202:3: error: 'NSLog' is unavailable: You should not do this!
                NSLog(@"%@", s1);
                ^

Upvotes: 11

waylonion
waylonion

Reputation: 6976

Try this!

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...)
 #endif

The solution can be found here: Enable and Disable NSLog in DEBUG mode

Hope this helped!

Upvotes: 1

Alexei Sholik
Alexei Sholik

Reputation: 7469

In your prefix header:

#define NSLog(x, ...) (__please_dont_use_NSLog__)

Upvotes: 2

Related Questions