In my memory, there's some code that can let the NSLog
not work when released.
在我的记忆中,有一些代码可以让NSLog在发布时不能工作。
I don't want to remove my NSLog
in my code.It helps a lot on debug mode.
我不想在代码中删除NSLog。它在调试模式上有很大的帮助。
So I want to find a way that can leave them in my code, and also don't slow down the application when released.
所以我想找到一种方法,可以把它们留在我的代码中,并且在发布时不会减慢应用程序的速度。
Thanks for your help~~
谢谢你的帮助~ ~
2 个解决方案
#1
4
A common way to remove all NSLog(…)
call in a release is to create a macro that is between conditional preprocessor macro and compiles to nothing, something like this:
在发行版中删除所有NSLog(…)调用的一种常见方法是创建一个宏,该宏位于条件预处理器宏之间,并将其编译为空,如下所示:
#ifdef RELEASE
# define NSLog(...) //remove loggin in production
#endif
Put this code in a .h
file that is included in every other file, like a Defines.h
that is #include
'd in the prefix header (.pch
).
将此代码放入包含在每个其他文件中的.h文件中,如定义。在前缀头(.pch)中包含#的h。
RELEASE
should be a preprocessor macro defined against the Release configuration in the "Build Settings" tab of your target.
发布应该是针对目标的“构建设置”选项卡中的发布配置定义的预处理器宏。
Related Questions
- Is it true that one should not use NSLog() on production code?
- 在生产代码上不应该使用NSLog()是真的吗?
- How to print out the method name and line number and conditionally disable NSLog?
- 如何打印方法名和行号,并有条件禁用NSLog?
#2
1
You will need to replace NSLog with a custom macro.
需要用自定义宏替换NSLog。
Put for instance
例如把
#ifdef DEBUG
# define DLog(...) NSLog(__VA_ARGS__)
#else
# define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)
in your prefix pch file.
在前缀pch文件中。
Use ALog for cases where you always want log output regardless of the state of the debug flag, and DLog for log only in debug mode.
对于需要日志输出的情况,无论调试标志的状态如何,都要使用ALog;对于只有在调试模式下才能进行日志记录的情况,要使用DLog。
源
#1
4
A common way to remove all NSLog(…)
call in a release is to create a macro that is between conditional preprocessor macro and compiles to nothing, something like this:
在发行版中删除所有NSLog(…)调用的一种常见方法是创建一个宏,该宏位于条件预处理器宏之间,并将其编译为空,如下所示:
#ifdef RELEASE
# define NSLog(...) //remove loggin in production
#endif
Put this code in a .h
file that is included in every other file, like a Defines.h
that is #include
'd in the prefix header (.pch
).
将此代码放入包含在每个其他文件中的.h文件中,如定义。在前缀头(.pch)中包含#的h。
RELEASE
should be a preprocessor macro defined against the Release configuration in the "Build Settings" tab of your target.
发布应该是针对目标的“构建设置”选项卡中的发布配置定义的预处理器宏。
Related Questions
- Is it true that one should not use NSLog() on production code?
- 在生产代码上不应该使用NSLog()是真的吗?
- How to print out the method name and line number and conditionally disable NSLog?
- 如何打印方法名和行号,并有条件禁用NSLog?
#2
1
You will need to replace NSLog with a custom macro.
需要用自定义宏替换NSLog。
Put for instance
例如把
#ifdef DEBUG
# define DLog(...) NSLog(__VA_ARGS__)
#else
# define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)
in your prefix pch file.
在前缀pch文件中。
Use ALog for cases where you always want log output regardless of the state of the debug flag, and DLog for log only in debug mode.
对于需要日志输出的情况,无论调试标志的状态如何,都要使用ALog;对于只有在调试模式下才能进行日志记录的情况,要使用DLog。
源