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

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

问题描述

我正在做一个有关在 Xcode 中调试的演示文稿,并希望获得有关有效使用 NSLog 的更多信息.

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:

  • 有没有办法轻松 NSLog 当前方法的名称/行号?
  • 有没有办法在编译发布代码之前轻松禁用"所有 NSLog?

推荐答案

下面是一些我经常使用的关于 NSLog 的有用宏:

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__)

DLog 宏仅用于在设置了 DEBUG 变量时输出(项目的 C 标志中的 -DDEBUG 用于调试配置).

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 将始终输出文本(如常规 NSLog).

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

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

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

-[LibraryController awakeFromNib] [Line 364] Hello world

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

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