iOS开发:Debug与Release版本NSLog屏蔽方法
来源:互联网 发布:如何使用linux系统 编辑:程序博客网 时间:2024/05/21 07:46
在开发过程中一般会用到NSLog(<#NSString *format, ...#>)来获得具体的信息。
#ifdef DEBUG# define DLog(...) NSLog(__VA_ARGS__)#else# define DLog(...) /* */#endif#define ALog(...) NSLog(__VA_ARGS__)
2.进入Xcode 4,选择菜单“Product”->“Manage Schemes”,选择一个项目,点击“Edit”,Bulid Configuration
附: 检查DEBUG标志是否正确定义,xcode一般会在debug运行配置项里面已经定义号了DEBUG标志,如果没定义我们就自己写上,以我的xcode 4 为例,如下图:
找到PreProcessor Macros 这个属性,对于Debug配置我们给他写上DEBUG,而在Release配置中把它留空。 这样我们刚才那段预处理命令就可以根据这个标志来判断我们编译的时调试版本还是发布版本,从而控制NSLog的输出。 (因为xcode 4 会把debug/release 两个配置项同时对比展现出来,而3.x版本的只能分别设置, 如果你用的时xcode 3.x 开发工具, 那么就分别对Debug/Release 都检查一下)。
Dropping NSLog in release builds
NSLog() is a great tool that helps debugging efforts. Unfortunately it is expensive, especially on the iPhone, and depending on how it’s used or what you’re logging, it could leak sensitive or proprietary information. If you look around the web, you’ll find a few different ways to drop NSLog in your release builds. Here is what I’ve put together based on those.
First add the following to the <AppName>_Prefix.pch file in your Xcode project:
123456
#ifdef DEBUG# define DLog(...) NSLog(__VA_ARGS__)#else# define DLog(...) /* */#endif#define ALog(...) NSLog(__VA_ARGS__)
Right-click on your target and click Get Info. Select the Build tab. Make sure Configuration is set to Debug. Add-DDEBUG to the Other C Flags of your target.
And that’s about it. When you want to log only in debug builds use DLog(). In release builds DLog() will be compiled as an empty comment. Otherwise use ALog() for logging in both debug and release builds. (A as in always.)
I like this approach for a few reasons:
- Some approaches comment out all NSLog() statements when compiled. This approach lets me keep some logging if I want.
- Using ALog() and DLog(), I make a conscious choice about which builds I’m going to log in.
- There is very little setup.
- There is no overhead. Because DLog() is defined as NSLog(), executing DLog() is no different than executing NSLog()
If you’d like to replace NSLog with DLog in your source files, here’s a quick sed command that you can run in Terminal:$ sed -i ".bak" 's/NSLog/DLog/' *.m
FYI, this will make a backup of each of the .m files whether it changed them or not.
- iOS开发:Debug与Release版本NSLog屏蔽方法
- iOS开发:Debug与Release版本NSLog屏蔽方法
- iOS开发debug跟release版本NSLog屏蔽方法
- iOS开发debug跟release版本NSLog屏蔽方法
- iOS开发debug跟release版本NSLog屏蔽方法
- iOS开发debug跟release版本log屏蔽方法
- iOS开发debug跟release版本log屏蔽方法
- iOS开发debug跟release版本log屏蔽方法
- ios debug release nslog
- 设置开发(Debug模式)与提交(Release模式)的运行环境以及如何屏蔽NSLog
- ios 在release时屏蔽掉NSlog()的几个方法
- iOS debug版本与release版本(问题)
- ios Debug Release,的NSlog显示问题
- iOS开发:Release/Debug模式与Configuration
- iOS release版本去除NSLog打印信息
- iOS release版本中去掉NSLog输出
- iOS release版本中去掉NSLog输出
- debug版本与release版本
- 50个python库(收藏)
- 高分辨率AVD显示不全问题
- LinuxC/C++编程基础(10) quicksort的简单实现
- 数理逻辑:公理化算术(13)形成新递归项的例子
- 你见过我的银行吗?不如来看看吧。。。
- iOS开发:Debug与Release版本NSLog屏蔽方法
- 数理逻辑:公理化算术(14)可计算谓词和项的几个例子
- 数理逻辑:公理化算术(15)可计算函数
- eclipse下修改和查看快捷键
- 数理逻辑:公理化算术(16)公理算术中的一些定理
- SOA调用数据库的API方式
- struts2 convention插件的使用(一)
- 《Linux企业应用案例精解》样章
- c++ iterator