在调试模式下启用和禁用NSLog [英] Enable and Disable NSLog in DEBUG mode

查看:144
本文介绍了在调试模式下启用和禁用NSLog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用NSLog当我在调试,否则禁用它。一个很简单的事情是:

  #ifdef DEBUG 
NSLog(@My log);
#endif

但所有这些 #ifdef #endif 是无聊... :(所以我尝试其他的东西:(.pch是好地方放)

  #ifdef DEBUG 
#define NSLog(text)NSLog(text);
#else

#endif

这个工作很好(不是递归)。 NSLog有无穷的参数。

  void NSLog(NSString * format,...)



我如何解决这个问题在预处理模式下工作?





此代码使您的NSLog更好:

  #ifdef DEBUG 
#define NSLog(FORMAT,...)fprintf(stderr,%s\\\
,[[NSString stringWithFormat:FORMAT,## __ VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif


解决方案

这应该做的诀窍:

  #ifdef DEBUG 
#define NSLog(...)NSLog __VA_ARGS__)
#else
#define NSLog(...)(void)0
#endif


I want to enable NSLog when I am in debug and disable it otherwise. A very simple thing is:

#ifdef DEBUG
NSLog(@"My log");
#endif

But all this #ifdef and #endif is borring... :( So I try other thing: (.pch is good place to put it)

#ifdef DEBUG
#   define NSLog(text) NSLog(text);
#else 
#   define NSLog(text) 
#endif

This work very fine (isn't recursive). But the problem is that NSLog have infinite arguments.

void NSLog(NSString *format, ...)

How I solve this to work in preprocessor mode?

-- Edit --

This code make your NSLog better:

#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(...)
#endif

解决方案

This should do the trick:

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...) (void)0
 #endif

这篇关于在调试模式下启用和禁用NSLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆