I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.
In particular, I have two questions:
- is there a way to easily NSLog the current method's name / line number?
- is there a way to "disable" all NSLogs easily before compiling for release code?
For some time I've been using a site of macros adopted from several above. Mine focus on logging in the Console, with the emphasis on controlled & filtered verbosity; if you don't mind a lot of log lines but want to easily switch batches of them on & off, then you might find this useful.
First, I optionally replace NSLog with printf as described by @Rodrigo above
Next, I switch logging on or off.
In the main block, define various categories corresponding to modules in your app. Also define a logging level above which logging calls won't be called. Then define various flavours of NSLog output
Thus, with current settings for kLOGIFcategory and kLOGIFdetailLTEQ, a call like
will print but this won't
nor will
If you want to override the settings for an individual log call, use a negative level:
I find the few extra characters of typing each line are worth as I can then
I'm sure many will find this a bit of an overkill, but just in case someone finds it suits their purposes..
It's easy to change your existing NSLogs to display line number and class from which they are called. Add one line of code to your prefix file:
New addition to DLog. Instead of totally removing debug from released application, only disable it. When user has problems, which would require debugging, just tell how to enable debug in released application and request log data via email.
Short version: create global variable (yes, lazy and simple solution) and modify DLog like this:
Longer answer at Jomnius iLessons iLearned: How to Do Dynamic Debug Logging in Released Application
Here one big collection of debug constants that we use. Enjoy.
My answer to this question might help, looks like it's similar to the one Diederik cooked up. You may also want to replace the call to
NSLog()
with a static instance of your own custom logging class, that way you can add a priority flag for debug/warning/error messages, send messages to a file or database as well as the console, or pretty much whatever else you can think of.I've taken
DLog
andALog
from above, and addedULog
which raises aUIAlertView
message.To summarize:
DLog
will output likeNSLog
only when the DEBUG variable is setALog
will always output likeNSLog
ULog
will show theUIAlertView
only when the DEBUG variable is setThis is what it looks like:
+1 Diederik