2

I tried to disable NSLog in release build.

I put the following code in .pch file

#ifndef __OPTIMIZE__
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...) {}
#endif

It's not working, so I tried

# define NSLog(...) {}

It still prints output to the console.

Any help will be good, thanks!

yiwei
  • 21
  • 2

5 Answers5

1

you would declare your own log function and use that instead. its implementation could go through NSLogv, if you want a va list. its implementation would also not forward the messages to NSLogv when disabled, so you will likely need more than one flavor of logger.

justin
  • 104,054
  • 14
  • 179
  • 226
1

Sounds like what you want is similar to this definition for DLog from this previous SO answer on NSLog tips and tricks.

#ifdef DEBUG 
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); 
#else 
#   define DLog(...)
#endif
Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
0

Preprocessor macro set in Build Setting of project having: DEBUG=1 $(inherited)

So now just use : if(DEBUG) NSLog(@"Your log");

Sandip Patel - SM
  • 3,346
  • 29
  • 27
0

In you application .pch file put following line. define NSLog(dese,...)

It will stop printing.

greja
  • 134
  • 10
0

Make sure __OPTIMIZE__ is defined in the build settings or higher up in the .pch file itself and that the pch file is set in the Prefix Header (GCC_PREFIX_HEADER) Build Settings. Also you can define NSLog(...) as nothing

#define NSLog(...)

Joe
  • 56,979
  • 9
  • 128
  • 135
  • Probably not best to define it out as nothing - this can cause unintended side effects in code, e.g., an if statement with a single NSLog following and no brackets... – fbrereto Mar 31 '11 at 20:02
  • 1
    Although I said good point I just realized that you still need a semicolon after the NSLog so `for(int i = 0; i< 10; i++);` is valid. Where it could cause an issue is using a comma in code. `int i;i=10,NSLog("i %d", i);` – Joe Mar 31 '11 at 20:21
  • {} do not work with the comma operator either o.O `i=10,{};` = !Expected expression. – Joe Mar 31 '11 at 20:58