如何打印出方法名称和行号并有条件地禁用NSLog? [英] How to print out the method name and line number and conditionally disable NSLog?

查看:154
本文介绍了如何打印出方法名称和行号并有条件地禁用NSLog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个问题:

p>


  • 是否有方法可以轻松地NSLog当前方法的名称/行号?

  • 在编译发布代码之前,是否可以轻松地禁用所有NSLog?


解决方案

NSLog的宏使用了很多:

  #ifdef DEBUG 
#define DLog(fmt,...)NSLog (...)($ @%s [Line%d]fmt),__PRETTY_FUNCTION__,__LINE__,## __ VA_ARGS__)
#else
#define DLog(...)
#endif

// ALog总是显示输出而不管调试设置
#define ALog(fmt,...)NSLog((@%s [Line%d]fmt),__PRETTY_FUNCTION__,__LINE__, DLOG宏仅用于在设置DEBUG变量时输出(-DDEBUG)(仅当设置了DEBUG变量时才会输出)



ALog将总是输出文本(如常规NSLog)。



输出(例如ALog(@Hello world))将如下所示:

   -  [LibraryController awakeFromNib] b $ b  


I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.

In particular, I have two questions:

  • is there a way to easily NSLog the current method's name / line number?
  • is there a way to "disable" all NSLogs easily before compiling for release code?

解决方案

Here are some useful macros around NSLog I use a lot:

#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#   define DLog(...)
#endif

// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

The DLog macro is used to only output when the DEBUG variable is set (-DDEBUG in the projects's C flags for the debug confirguration).

ALog will always output text (like the regular NSLog).

The output (e.g. ALog(@"Hello world") ) will look like this:

-[LibraryController awakeFromNib] [Line 364] Hello world

这篇关于如何打印出方法名称和行号并有条件地禁用NSLog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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