在NSLog编译器中设置一个标志不显示
是否有任何特定的标志,可以设置为不显示NSLog输出debugging或释放?
谢谢。
一个select可能是使用macros作为NSLog的替代(如果在这一点上它很容易改变的东西)。 我喜欢这些家伙使用的前缀头文件:
http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/
基本上他们的日志function是:
#ifdef DEBUG #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__]) #else #define DLog(...) do { } while (0) #endif
所以,如果你不在DEBUG版本中,那么日志loggingmacros将变成一个无操作。
通常情况下,人们编写自己的macros – 如DebugLog – 将日志“编译出来”:
#undef DebugLog #ifdef DEBUG_MODE #define DebugLog( s, ... ) NSLog( @"%@", [NSString stringWithFormat:(s), ##__VA_ARGS] ) #else #define DebugLog( s, ... ) #endif
在Xcode中,您可以为特定的构buildconfiguration定义macros。 例如,在这里,我已经为debugging版本定义了DEBUG
,没有任何发布版本。
然后在代码中使用它,包装你的NSLog(...)
语句(或者你select使用macros, update :darren的方法对于这种技术来说是相当不错的):
#ifdef DEBUG NSLog(...); #endif
使用相同的机制来释放configuration只有逻辑。
如果您有不同数量的构buildconfiguration,则可以使用此function,如果要针对不同的构buildconfiguration启用/禁用不同级别的function,则可以定义多个macros。
一个新的Xcode项目的默认标志是DEBUG
的debugging和没有释放。 所以你将这样隐藏NSLog
:
#ifndef DEBUG # define NSLog(...) # define NSLogv(...) #endif
或者,如果你想要漂亮的自定义日志(在这里不使用#ifdef
,因为没有简单的#elifdef
):
#if DEBUG # define NSLog(format, ...) NSLog((@"%s " format), __PRETTY_FUNCTION__, ##__VA_ARGS__) # define NSLogv(format, ...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [[NSString alloc] initWithFormat:format arguments:__VA_ARGS__]) #elif ADHOC // default logs #else// Release # define NSLog(...) # define NSLogv(...) #endif
如果你想覆盖这些macros,并且有时还会logging一些东西,你可以使用:
(NSLog)(@"This log is always visible!");
那么,如何隐藏这些呢? 这将需要你#define NSLog NoLog
定义#define NSLog NoLog
并将#define NSLog NoLog
定义为一个extern函数,其中包含void NoLog(NSString *format, ...) {}
的空实现。 但是避免完全使用(NSLog)
可能会更干净,而是使用带有LogLevel枚举的函数:
typedef NS_ENUM(NSUInteger, LogLevel) { LogLevelRelease, LogLevelAdHoc, LogLevelDeveloper, }; void CustomLog(LogLevel level, NSString *format, ...) { #if !DEBUG if (LogLevel == LogLevelDeveloper) return; #if !ADHOC if (LogLevel == LogLevelAdHoc) return; #endif #endif va_list ap; va_start (ap, format); NSLogv(format, ap); va_end (ap); }
这个CustomLog的缺点是参数总是被评估,甚至在Release中。 所以一个最佳的解决scheme是多个macros:
#define NSLog(format, ...) NSLog((@"%s " format), __PRETTY_FUNCTION__, ##__VA_ARGS__) #if DEBUG # define DebugLog(...) NSLog(__VA_ARGS__) # define AdHocLog(...) NSLog(__VA_ARGS__) # define ReleaseLog(...) NSLog(__VA_ARGS__) #elif ADHOC # define DebugLog(...) # define AdHocLog(...) NSLog(__VA_ARGS__) # define ReleaseLog(...) NSLog(__VA_ARGS__) #else// Release # define NSLogv(...) # define DebugLog(...) # define AdHocLog(...) # define ReleaseLog(...) NSLog(__VA_ARGS__) #endif
使用这个macros检查debugging模式
#ifdef DEBUG //NSLog(@"XYZ"); #endif
上面的NSLog不会打印在发布模式
您可以在预编译头文件(.pch)中定义一个macros来禁用NSLog:
#ifdef RELEASE # define NSLog(...) #endif
这会禁用NSLog的发行版本。
添加这个B / C没有任何答案似乎解决您可能有警告(取决于项目设置)与这些方法中的一些。
这是唯一的方法,我可以find压制NSLog没有得到任何警告。 否则,我会用我们使用的设置(警告未使用的variables)。
#undef NSLog #define NSLog(fmt, ...) if (0) { printf("%s", [[NSString stringWithFormat:fmt, ##__VA_ARGS__] UTF8String]); }
然后,您只需将其放置在您用于发布版本的任何pch文件或无论您想要抑制日志的位置。
感谢@Hot Licks提供了如何使variables“used”的例子。