日志调试Objective-C代码时要使用哪些日志记录解决方案? [英] What logging solutions to use while log-debugging objective-C code?

查看:101
本文介绍了日志调试Objective-C代码时要使用哪些日志记录解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的代码中到处使用NSLog只是为了测试它,除了使用标准的NSLog之外还有更好更专业的方法吗?在调试时,请分享其他专业目标-C编码人员用来做日志记录的事情吗?

I am currently using NSLog everywhere in my code just to test it, is there a better and more professional way besides using standard NSLog? Mind sharing what other professional objective-C coders use to do logging while debugging?

推荐答案

这是我的日志记录类。它只是围绕NSLog的宏,但它允许你有可切换的级别。我从其他人那里借来并修改了这个,但不记得在哪里 - 我希望我可以给道具。评论是一个很好的使用指南。希望它有所帮助。

Here's my logging class. It just macros around NSLog but it allows you to have switchable levels. I borrowed and modified this from others but can't remember where - I wish I could give props. The comments are a good guide to use. Hope it helps.

/*
 * 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

这篇关于日志调试Objective-C代码时要使用哪些日志记录解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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