我目前在我的代码中的任何地方都使用NSLog来测试它,除了使用标准NSLog之外,还有更好更专业的方法吗?介意分享其他专业目标-C编码器在调试时用于记录日志的情况吗?日志调试目标C代码时使用哪些日志记录解决方案?
1
A
回答
3
这是我的日志记录类。它只是NSLog周围的宏,但它允许你有可切换的级别。我从别人那里借来修改过这些东西,但不记得在哪里 - 我希望我能给道具。评论是一个很好的使用指南。希望能帮助到你。
/*
* There are three levels of logging: debug, info and error, and each can be enabled independently
* via the ENLOGGING_LEVEL_DEBUG, ENLOGGING_LEVEL_INFO, and ENLOGGING_LEVEL_ERROR switches below, respectively.
* In addition, ALL logging can be enabled or disabled via the ENLOGGING_ENABLED switch below.
*
* To perform logging, use any of the following function calls in your code:
*
* ENDebug(fmt, …) – will print if ENLOGGING_LEVEL_DEBUG is set on.
* ENInfo(fmt, …) – will print if ENLOGGING_LEVEL_INFO is set on.
* ENHeading(fmt, …) – will print if ENLOGGING_LEVEL_INFO is set on.
* ENError(fmt, …) – will print if ENLOGGING_LEVEL_ERROR is set on.
*
* Each logging entry can optionally automatically include class, method and line information by
* enabling the ENLOGGING_INCLUDE_CODE_LOCATION switch.
*
* Logging functions are implemented here via macros, so disabling logging, either entirely,
* or at a specific level, removes the corresponding log invocations from the compiled code,
* thus completely eliminating both the memory and CPU overhead that the logging calls would add.
*/
#define ENLOGGING_ENABLED 1
// Set any or all of these switches to enable or disable logging at specific levels.
#define ENLOGGING_LEVEL_DEBUG 1
#define ENLOGGING_LEVEL_INFO 1
#define ENLOGGING_LEVEL_ERROR 1
// Set this switch to set whether or not to include class, method and line information in the log entries.
#define ENLOGGING_INCLUDE_CODE_LOCATION 0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Implementation
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if !(defined(ENLOGGING_ENABLED) && ENLOGGING_ENABLED)
#undef ENLOGGING_LEVEL_DEBUG
#undef ENLOGGING_LEVEL_INFO
#undef ENLOGGING_LEVEL_ERROR
#endif
// Logging format
#define ENLOG_FORMAT_NO_LOCATION(fmt, lvl, ...) NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
#define ENLOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) NSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
#if defined(ENLOGGING_INCLUDE_CODE_LOCATION) && ENLOGGING_INCLUDE_CODE_LOCATION
#define ENLOG_FORMAT(fmt, lvl, ...) ENLOG_FORMAT_WITH_LOCATION(fmt, lvl, ##__VA_ARGS__)
#else
#define ENLOG_FORMAT(fmt, lvl, ...) ENLOG_FORMAT_NO_LOCATION(fmt, lvl, ##__VA_ARGS__)
#endif
// Debug level logging
#if defined(ENLOGGING_LEVEL_DEBUG) && ENLOGGING_LEVEL_DEBUG
#define ENDebug(fmt, ...) ENLOG_FORMAT(fmt, @"debug", ##__VA_ARGS__)
#else
#define ENDebug(...)
#endif
// Info level logging
#if defined(ENLOGGING_LEVEL_INFO) && ENLOGGING_LEVEL_INFO
#define ENInfo(fmt, ...) ENLOG_FORMAT(fmt, @"info", ##__VA_ARGS__)
#define ENHeading(fmt, ...) ENLOG_FORMAT(@"#################### " fmt " ####################", @"HD", ##__VA_ARGS__)
#else
#define ENInfo(...)
#define ENHeading(...)
#endif
// Error level logging
#if defined(ENLOGGING_LEVEL_ERROR) && ENLOGGING_LEVEL_ERROR
#define ENError(fmt, ...) ENLOG_FORMAT(fmt, @"***ERROR***", ##__VA_ARGS__)
#else
#define ENError(...)
#endif
#if defined(ENLOGGING_LEVEL_ERROR) && ENLOGGING_LEVEL_ERROR
#define ENResult(result, error) if (result == NO) ENError("%@", error)
#else
#define ENResult(...)
#endif
+0
看起来比#ifdef K_DEBUG更好! –
3
我正在使用标准工具在Xcode
中进行调试,例如,通过向某些行添加断点并在其停止时查看iVar的值。您还可以使用Instruments
来测试性能和内存管理。
+0
这并不总是那么好。特别是如果你经常执行代码,即在游戏中每帧运行一次的方法。有时将所需数据转储到控制台会更方便。在每个NSLog周围, – JustSid
相关问题
- 1. PHP日志解决方案?
- 2. 用于日志记录的最佳NoSQL解决方案
- 3. 如何在单元测试目标c代码时禁用日志记录
- 4. C++调试Windows上的日志记录
- 5. 简单的Swift 3日志记录解决方案
- 6. AWS的日志记录/监测解决方案
- 7. 集中日志解决方案
- 8. 日志记录subprocess.Popen调用
- 9. EasyMock日志记录调用
- 10. 禁用Hiera调试日志记录
- 11. Java日志记录方法调用?
- 12. 为解决方案中的多个项目配置日志记录
- 13. 使用系统日志时禁用/启用日志记录
- 14. 调整apache公共日志记录的日志记录级别?
- 15. 使用Python日志记录管理日志记录器
- 16. C/C++日志工具调用取决于日志级别
- 17. Windows Azure日志记录和调试
- 18. Theos/Logos调试日志记录
- 19. Django调试工具栏日志记录
- 20. Python日志记录和Pydev调试器?
- 21. 解析码头日志记录
- 22. Rails日志记录到Apache日志而不是应用日志
- 23. JavaScript日志记录库(日志级别)
- 24. django日志记录:未创建日志
- 25. 没有日志库的日志记录
- 26. 通过Gradle测试时记录日志
- 27. 实时日志记录
- 28. Java日志记录 - 我的日志文件在哪里?
- 29. 解释崩溃日志目标C
- 30. 系统日志在日志轮转后停止日志记录
呃,一个调试器?像[gdb](http://www.gnu.org/s/gdb/)? –
我认为他指的是更强大的日志基础设施。 Adit,你可能想改变标题,因为我有相同的初始反应,但是然后阅读你的最后一行。调试器是显而易见的,但与NSLog乱码代码有问题。有些东西可以在不同的层次上打开和关闭,这是非常有用的。我可以执行某些操作并检查日志,但速度比您可以更快一些,但两者都有明显的用处。 – bryanmac
对不起,我忘了把它的日志关键字,谢谢指出, – adit