如何仅在 iPhone 应用程序的调试模式下显示日志?

发布于 2024-12-05 14:15:56 字数 149 浏览 6 评论 0原文

我正在 iPhone Simulator 中调试应用程序。我想仅当应用程序在 iPhone Simulator 中以调试模式运行时显示日志。我正在使用 NSLog。如何设置 NSLog 的条件,以便日志仅在调试模式下打印在控制台上?

I am debugging the application in iPhone Simulator.I want to show log only when the application is running in debug mode in iPhone Simulator.I am using the NSLog. How can I put the condition for NSLog so that the logs should be printed on console in debug mode only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-12-12 14:15:56

将其放入您的 .pch 文件中:

#ifndef DLog
#ifdef DEBUG
#define DLog(_format_, ...) NSLog(_format_, ## __VA_ARGS__)
#else
#define DLog(_format_, ...)
#endif
#endif

现在您可以使用 DLog 而不是 NSLog 来处理仅应在调试版本中打印的所有日志消息。

人们还可以重新定义 NSLog,但有时您确实希望在设备日志中显示一些日志消息(例如严重错误消息)。

另请参阅 NSLog 替代品的演变,了解如何改进调试日志宏。

Put this in your .pch file:

#ifndef DLog
#ifdef DEBUG
#define DLog(_format_, ...) NSLog(_format_, ## __VA_ARGS__)
#else
#define DLog(_format_, ...)
#endif
#endif

Now you can use DLog instead of NSLog for all log messages that should only be printed in your debug builds.

One could also redefine NSLog but there sometimes are log messages that you do want to appear in the device logs (like critical error messages).

See also The Evolution of a Replacement for NSLog for ideas on how to improve the debug log macro.

寄居人 2024-12-12 14:15:56
#ifdef DEBUG
NSLog(@"Your log statement");
#endif
#ifdef DEBUG
NSLog(@"Your log statement");
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文