Search code examples
iphonedebuggingloggingnslog

How to show log in debug mode only for iPhone application?


I am debugging the application in iPhone Simulator.I want to show log only when the application is running in debug mode in iPhone Simulator.I am using the NSLog. How can I put the condition for NSLog so that the logs should be printed on console in debug mode only?


Solution

  • Put this in your .pch file:

    #ifndef DLog
    #ifdef DEBUG
    #define DLog(_format_, ...) NSLog(_format_, ## __VA_ARGS__)
    #else
    #define DLog(_format_, ...)
    #endif
    #endif
    

    Now you can use DLog instead of NSLog for all log messages that should only be printed in your debug builds.

    One could also redefine NSLog but there sometimes are log messages that you do want to appear in the device logs (like critical error messages).

    See also The Evolution of a Replacement for NSLog for ideas on how to improve the debug log macro.