3

I want to treat any NSLog in my code as an error. The line should be a compile time error in Xcode. Just like how usage of release in an ARC enabled project is treated as an error.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Vinoth Kumar
  • 156
  • 8
  • Then how "release" is shown as error in ARC enabled project? – Vinoth Kumar Mar 25 '13 at 11:44
  • You can simply search all `NSLog (command+shift+F)` and replace it with `//NSLog` before debugging. – Baby Groot Mar 25 '13 at 11:46
  • Yes, "Search and replace" is a Simple solution, but still people who join new in team, may still use NSLog, forgetting to replace NSLog sometimes, So checking possibility to prevent it ultimately by marking as compile time error. – Vinoth Kumar Mar 25 '13 at 11:51
  • @VinothKumar I edited your question for clarity and tags to help get the best answers apologies if I misrepresented anything – Carl Veazey Mar 25 '13 at 11:52
  • @CarlVeazey, Thanks for the editing. It is more clear now. – Vinoth Kumar Mar 25 '13 at 11:58
  • Just now found when I deliberately use [foo release] in arc enabled project, in this case also the two errors namely Apple Mach-O error and DSymbutil error are appearing. So accepting the answers of iMartin, dreamlax and Bhargavi. Thanks all. – Vinoth Kumar Mar 25 '13 at 12:41

3 Answers3

3

Try this,

#define NSLog_UNAVAILABLE __attribute__((unavailable("NSLog is treated as Error.")))
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NSLog_UNAVAILABLE;
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
1

You can redeclare prototype of NSLog in your code with __deprecated, so any use of it will produce warning:

FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) __deprecated;

Then maybe there is some way to turn warnign into error.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
0

You can use this if you use Clang as your compiler (actually just realised GCC also supports C11's _Static_assert too from version 4.6):

#define NSLog(...) _Static_assert(0, "Not allowed to use NSLog!")

Compilation will fail if a _Static_assert fails.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • NSlog shown as error in Xcode 4.6, however 2 more errors are also getting added new, namely Apple Mach-O linker error and Symutil Error. – Vinoth Kumar Mar 25 '13 at 12:28
  • FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) __unavailable; This also works as iMartin suggested, however 2 more errors are getting added up, namely Apple Mach-O error and DSymbutil error – Vinoth Kumar Mar 25 '13 at 12:28
  • @VinothKumar: I don't know what you mean, I tried it and it works, so maybe your error is elsewhere? Make sure you spell everything in the correct case. – dreamlax Mar 25 '13 at 12:36