设备上的 NSLog,这是一个问题还是我必须删除它?

发布于 2024-11-15 23:48:59 字数 297 浏览 6 评论 0原文

我读过这篇文章:当 NSLog 信息发生什么情况时在设备上运行?

...但是想知道 NSLog 是否在分发应用程序时出现问题,例如填满内存或其他什么?我只有在测试数据库输入数据的一致性时才有兴趣看到它。

原因是当我将数据加载到模拟器中的数据库中时,我有 NSLog 来控制。我可以在上传时将其删除,但如果我不需要的话会很好吗?

I have read this post: what happens to NSLog info when running on a device?

...but wondering if NSLog is a problem when distributing the app such as filling up the memory or something? I am only interested to see it when i test the consistency of my input data to the database.

The reason is that I have NSLog to control when i load the data into my database in the simulator. I could remove it when i upload but it would be good if i do not need to?

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

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

发布评论

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

评论(5

檐上三寸雪 2024-11-22 23:48:59

你应该删除它。例如,如果您记录 UITableViewCell 的内容(在 -tableView:cellForRowAtIndexPath: 中),它可能会对性能产生很大影响,尤其是在较慢的硬件上。

使用宏将 NSLog 输出保持在调试模式,但将其从发布模式删除。可以在以下站点找到示例:http:// /iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog

You should remove it. For example if you log contents of a UITableViewCell (in -tableView:cellForRowAtIndexPath:), it can make a big difference in performance, especially on slower hardware.

Use a macro to keep NSLog output in Debug mode, but remove it from Release mode. An example can be found on the following site: http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog

爱你不解释 2024-11-22 23:48:59

我在 pch 文件中使用了一组对此非常方便的宏。

请参阅http://www.cimgf.com/2010 /05/02/my-current-prefix-pch-file/ 了解详细信息。

#ifdef DEBUG
  #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
  #define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
  #define DLog(...) do { } while (0)
  #ifndef NS_BLOCK_ASSERTIONS
    #define NS_BLOCK_ASSERTIONS
  #endif
  #define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif

#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)

I use a set of macros in my pch file that are quite handy for this.

See http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/ for details.

#ifdef DEBUG
  #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
  #define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
  #define DLog(...) do { } while (0)
  #ifndef NS_BLOCK_ASSERTIONS
    #define NS_BLOCK_ASSERTIONS
  #endif
  #define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif

#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)
丢了幸福的猪 2024-11-22 23:48:59

设备上的日志只保留大约一个小时的数据,即使是苹果的应用程序也会在那里记录很多东西。因此,在日志中具有有意义的输出通常是可以接受的。

但由于日志记录是一项磁盘操作,您可能会发现过多的日志记录会减慢您的应用程序的速度,因为每个 NSLog 的写入都会在短时间内阻塞主(= UI)线程。

因此,如果应用程序中发生错误,您应该只记录提供更多信息的内容,以便于查找问题所在。

The log on the device only keeps around an hour of data, even Apple's apps log quite a few things there. So it is generally acceptable to have meaningful output in the log.

But since logging is a disk operation you might find that excessive logging slows down your app since the writing blocks the main (= UI) thread for a short while for every NSLog.

Because of this you should only log things that give more information if errors are happening in your app to facilitate finding what is going wrong.

淡紫姑娘! 2024-11-22 23:48:59

考虑切换到 cocoalumberjack,它是 NSLog 的直接替代品,并且具有显着更有用的功能和更好的性能。

它在概念上与 log4j 等其他流行的日志框架类似,但专为 Objective-C 设计,并利用多线程、中央调度(如果可用)、无锁原子操作等功能,以及Objective-C 运行时的动态特性。

Consider switching to cocoalumberjack, it is a drop-in replacement for NSLog and has significantly more useful functionality and better performance.

It is similar in concept to other popular logging frameworks such as log4j, yet is designed specifically for objective-c, and takes advantage of features such as multi-threading, grand central dispatch (if available), lockless atomic operations, and the dynamic nature of the objective-c runtime.

谈情不如逗狗 2024-11-22 23:48:59

删除 NSLog 条目的唯一真正原因是通过运行不必要的代码来节省内存消耗,但除非您持续添加数据,否则这不应该是一个太大的问题。
此外,如果用户遇到应用程序崩溃等问题,可以提交 NSlog,开发人员可以读取这些日志来找出崩溃的原因。如果您在 NSlog 中加载了大量不必要的数据,那么以后您需要通过该日志来查找用户的问题以解决问题,这可能会很麻烦。

最终,我不会太担心删除它们。无论如何,这就是我的2分钱。

There only real reason to remove your NSLog entries is to save on memory consumption by running unnecessary code, but unless you're consistenly adding data, it shouldn't be too much of an issue.
Furthermore, if a user has an issue such as app crash, etc. NSlogs can be submitted which the dev can read to work out the reason for the crash. If you're loading the NSlog with a great deal of unnecessary data, it can be troublesome at a later date if you need to go through said log to find a user's issue in order to fix the issue.

Ultimately, I wouldn't worry too much about removing them. That's my 2 cents anyhow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文