IPhone: to suppress NSLog in production?

I got a bunch of NSLog instructions in my code. Is there anyway I can suppress them from execution in production, but fulfill when I design? I want to not comment on them, and they returned them back during development, as this is a hassle.

+1
source share
4 answers

DEBUG is a macro that is defined only (by default xcode) when you build the debug / development assembly of your project. This will cause any code between the #ifdef DEBUG and #endif lines to not be included during the release build, but they will be compiled and included in the debug / dev / test build.

Example:

 #ifdef DEBUG NSLog(@"Hello World!"); #endif 

This is what I use in my Project-Prefix.pch (called DEBUGLOG(@"abc %@", someStr); ):

 #ifdef DEBUG extern void DBGQuietLog(NSString *format, ...); #define DEBUGLOG(fmt, ...) DBGQuietLog((@"[Line: %d] %s: " fmt), __LINE__, __FUNCTION__, ##__VA_ARGS__); #else #define DEBUGLOG(fmt, ...) #endif #ifdef DEBUG void DBGQuietLog(NSString *format, ...) { if (format == nil) { printf("nil\n"); return; } va_list argList; va_start(argList, format); NSString *s = [[NSString alloc] initWithFormat:format arguments:argList]; printf("%s\n", [[s stringByReplacingOccurrencesOfString:@"%%" withString:@"%%%%"] UTF8String]); [s release]; va_end(argList); } #endif 

This prints the console lines (only during debugging) with the line number, class name and method name as follows:

 [Line: 36] -[iPhoneAppDelegate application:didFinishLaunchingWithOptions:]: Hello World! 
+3
source

You can create a proxy server and define it as a preprocessor macro:

 #ifdef DEBUG #define MyLog(str, ...) NSLog(str, ##__VA_ARGS__) #else #define MyLog(str, ...) #endif 

Then you use MyLog () instead of NSLog () in your code, and when you don't compile in debug mode, NSLog will be replaced with no-op.

+2
source

Wrap them with a macro. Unfortunately, Apple does not already do this with a β€œcanned” solution, but many do, and there are several examples on the Internet.

Something like:

 #ifdef DEBUG_MODE #define DebugLog( s, ... ) NSLog(@"<%@:(%d)> %@", [NSString stringWithUTF8String __PRETTY_FUNCTION__], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) #else #define DebugLog( s, ... ) #endif 

(Hope this is correct, manually copied from another screen)

+1
source

As an extension for CajunLuke's answer, I can only advise you to use this: http://kdl.nobugware.com/post/2009/03/11/better-nslog/

+1
source

All Articles