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:
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.
原文:http://www.cimgf.com/2009/01/24/dropping-nslog-in-release-builds/