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?
To complement the answers above, it can be quite useful to use a replacement for NSLog in certain situations, especially when debugging. For example, getting rid of all the date and process name/id information on each line can make output more readable and faster to boot.
The following link provides quite a bit of useful ammo for making simple logging much nicer.
http://cocoaheads.byu.edu/wiki/a-different-nslog
It is simple,for Example
Output: -[AppDelegate applicationWillEnterForeground:]
There are a new trick that no answer give. You can use
printf
insteadNSLog
. This will give you a clean log:With
NSLog
you get things like this:But with
printf
you get only:Use this code
building on top of above answers, here is what I plagiarized and came up with. Also added memory logging.
Outputs file name, line number, and function name:
__FUNCTION__
in C++ shows mangled name__PRETTY_FUNCTION__
shows nice function name, in cocoa they look the same.I'm not sure what is the proper way of disabling NSLog, I did:
And no logging output showed up, however I don't know if this has any side effects.
Here are some useful macros around NSLog I use a lot:
The DLog macro is used to only output when the DEBUG variable is set (-DDEBUG in the projects's C flags for the debug confirguration).
ALog will always output text (like the regular NSLog).
The output (e.g. ALog(@"Hello world") ) will look like this: