2

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

Chris
  • 2,739
  • 4
  • 29
  • 57
  • 3
    Out of curiosity, why? – Tom Redman Apr 17 '13 at 20:50
  • @TRedman - I'm reading the book "Learning Core Audio" - Theres a footnote that says the author couldn't work out why his code wasn't running, he remmed out the NSLog's and found the code worked, it turned out it was a speed issue. – Chris Apr 17 '13 at 22:32
  • The question is interesting but the book author should have tried to find an actual problem at his code. For example messing comparison like this: `BOOL byes = YES; NSLog(@"%@", (byes = NO) ? @"N" : @"Y");` - note there's no comparison operator and `byes` is `NO` after this NSLog. – A-Live Apr 17 '13 at 23:42

3 Answers3

11

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);
                ^
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I'd like to give users of my code a reason as to why I have done this, is there anyway I can do this, or should I just put a comment above this line? – Chris Apr 17 '13 at 20:35
2

In your prefix header:

#define NSLog(x, ...) (__please_dont_use_NSLog__)
Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41
1

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!

Community
  • 1
  • 1
waylonion
  • 6,866
  • 8
  • 51
  • 92