Thursday, September 9, 2010

NSLog tricks: Log only in Debug mode. Add Function name, Line num

It is a common requirement to log certain statements only in the Debug mode, while needing to avoid them in the release mode. Of course, one option is to have statements like this everywhere:

#ifdef DEBUGGING
NSLog(@"Data: point.x = %f, point.y = %f", point.x, point.y);
#endif
And define DEBUGGING somewhere in the header file. However, this can get very verbose. 
A better option is to use Macros. In project_Prefix.pch file, include: 

#ifdef DEBUGGING
# define DBLog(fmt,...) NSLog(@"%@",[NSString stringWithFormat:(fmt), ##__VA_ARGS__]);
#else
# define DBLog(...)
#endif 
Also, you can include, in "Edit Active Target" -> "Build" -> (Select Debug Config) -> "Other C/C++ Flags", write:

-DDEBUGGING 

This automatically defines DEBUGGING flag in Debug mode. Leave it blank for the Release mode.  

Now, you can use DBLog instead of NSLog: 

DBLog(@"Data: point.x = %f, point.y = %f", point.x, point.y);

This will automatically print statements in the Debug mode, and not print the statements in the Release mode! 

(Alternatively, you can have a line in the Prefix.pch file:
#define DEBUGGING 
And manually comment it out for Release mode.)

Similarly, if you want you also have function name and line number, there is a nice trick:

#ifdef DEBUGGING#   define DLog(fmt, ...) NSLog((@"Func: %s , Line: %d, " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif

If you always want to print out regardless of the DEBUGGING SETTING, but with the function name and line number:
#define ALog(fmt, ...) NSLog((@"Func: %s , Line: %d, " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
Hope that helps! Happy iCoding!

90 comments:

  1. # define DBLog(fmt,...) NSLog(@"%@",[NSString stringWithFormat:(fmt), ##__VA_ARGS__]);

    why not just do

    # define DBLog(fmt,...) NSLog((fmt), ##__VA_ARGS__);

    ReplyDelete
  2. Your articles don’t beat around the bushes exact t to the point.
    backup extractor for iphone

    ReplyDelete
  3. This blog is genuinely impressive in all aspects.My Valentine Love

    ReplyDelete
  4. Nice composition on programming of iphone .
    iphone developers

    ReplyDelete
  5. I’m glad to find so many useful and informative data on your website.
    Discover National Two

    ReplyDelete
  6. I feel really nice reading these articles I mean there are writers that can write good material. Yu Eyut Ex Advice

    ReplyDelete
  7. Wonderful learning guys I’m a fan of your website. Onks Design Advice

    ReplyDelete
  8. Great blog you people have maintained there, I totally appreciate the work.
    Full D Glaa Article

    ReplyDelete
  9. I really wana thank you for providing such informative and qualitative material so often.
    Click The Park tonight Link

    ReplyDelete
  10. As like your site is informative I think you must post the articles regularly so that we can get many benefits of learning through your efforts.
    Click The Park tonight Link

    ReplyDelete
  11. I have checked this link this is really important for the people to get benefit from.
    Relationship Issues With Derry Chat

    ReplyDelete
  12. I am informing you all "fabulous information"
    payday loans online same day

    ReplyDelete
  13. How fine of you!!!! Really awesome efforts you have shown.
    payday loan

    ReplyDelete
  14. That’s really amazing and extraordinary blogs & can help those who get issues in searching this type of information. Healthy Relationship with Brock And Skelly

    ReplyDelete
  15. It’s an outstanding article in favor of all the web guests; they will get benefit from it I am confident.
    Uliea Homepage

    ReplyDelete
  16. I would never want to miss out any opportunity to read out your contents. Again Vest Homepage|

    ReplyDelete
  17. You have done really a superb job with your web site. Marvelous stuff is here to read.
    ppi

    ReplyDelete
  18. Great blog you people have made here on this blog site, I will absolutely appreciate your work.
    accident compensation

    ReplyDelete
  19. The complete blogs are really inconceivable and definitely everyone will share this information. ppi reclaim

    ReplyDelete
  20. I am informing you all "fabulous information" ppi reclaim

    ReplyDelete
  21. It’s really such nice information to get advantage from. bad credit payday loan

    ReplyDelete
  22. Yet, much is not yet determined. Could you explain in more details
    Visit

    ReplyDelete
  23. Your articles and contents are encouraging. Reclaim PPI

    ReplyDelete
  24. Personally I think overjoyed I discovered the blogs. Relationship with Blog PTR

    ReplyDelete
  25. I continuously keep on coming to your site again just in case you have posted new contents.
    Advice by Ve Scans

    ReplyDelete
  26. Your articles and contents are inspirational.
    Go to Mutekiya HK

    ReplyDelete
  27. I truly relish whilst I go through your blogs and articles.
    Learn More About Bonp Neus

    ReplyDelete
  28. Great blog you people have maintained there, I totally appreciate the work.
    click here

    ReplyDelete
  29. I am sure you have a great fan following out there.
    Relationship with Grow Your Charity

    ReplyDelete
  30. The thing that you just offer is value our case and the force.
    Look for SDJN 16Z Here

    ReplyDelete
  31. I see the greatest contents on your blog and I extremely love reading them.
    Forum E-Democratie Homepage

    ReplyDelete
  32. I’m sure you will provide the more awesome blogs like these blogs that I’ve enjoyed a lot.
    automobile insurance quote

    ReplyDelete
  33. I have spent a lot of the time in different blogs but this is really a unique blog for me.infographics design

    ReplyDelete
  34. That’s a nice site you people are carrying out there.
    infographics design

    ReplyDelete
  35. This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place.
    social media infographics

    ReplyDelete
  36. I envisage feeling to have the increase that your knowledge.
    infographic design

    ReplyDelete
  37. Superb way of explaining, and great blog to get wonderful information. infographic design

    ReplyDelete
  38. Your articles support me a lot in all mediums of subjects.
    whole life insurance

    ReplyDelete
  39. This blog site has really a huge collection of articles with impressive information. social media infographics

    ReplyDelete
  40. The Info in the blog is out of this world, I so want to read more.
    a1c chart

    ReplyDelete
  41. This is one of the most important blogs that I have seen, keep it up!
    whole life insurance prices

    ReplyDelete
  42. I feel happiness to read the content that you are posting.cheap car insurance

    ReplyDelete
  43. This is an informative blog by which I have got that info which I really wanted to get. cheap car insurance

    ReplyDelete
  44. This webpage is containing a pleasant stuff with the teachings, I loved it a lot.
    Read Full Article

    ReplyDelete
  45. Hi to all, the blog has really the dreadful information I really enjoyed a lot.
    New York accident attorney

    ReplyDelete
  46. Inconceivable points. Sound arguments. Keep going the truly amazing work.
    payday loan

    ReplyDelete
  47. You create sense out of the foremost complex topics.
    rar cracker

    ReplyDelete
  48. The blog is good enough, keep up writing such type of posts.
    paydayloan

    ReplyDelete
  49. congratulations guys, quality information you have given!!! personal loan

    ReplyDelete
  50. Keep the balls rolling!! Nice posts you have given for us.weight loss supplements for women

    ReplyDelete
  51. The blog is good enough, keep up writing such type of posts.
    pirater un compte facebook

    ReplyDelete
  52. Hi, just desired to let you know, I enjoyed this blog post. It had been funny. Carry on posting! personal trainer Winnipeg

    ReplyDelete
  53. You people have actually provided the best blogs that are easy to understand for the folks. ACLS Certification

    ReplyDelete
  54. Outstanding blog, in my opinion site owners should acquire a great deal out of this blog its very user welcoming. comment pirater un compte facebook

    ReplyDelete
  55. This comment has been removed by the author.

    ReplyDelete
  56. These are the great blogs; I assure you that I really enjoyed a lot in reading.
    http://www.epicfollowers.com

    ReplyDelete
  57. Amazing information in this blog here that is truly glancing over the every aspects of topic.
    domain name

    ReplyDelete
  58. A great job you have already done. I’m really delighted to see your amazing work.
    vietnam visa on arrival

    ReplyDelete
  59. It’s amazing to visit again n again coming to your blogs the superb effort is here.


    Manhattan central booking

    ReplyDelete
  60. Regarding all aspects the blog was perfectly nice.
    replica watches

    ReplyDelete
  61. You people have actually provided the best blogs that are easy to understand for the folks.vietnam visa on arrival

    ReplyDelete
  62. I suppose I've selected an unbelievable and interesting blog.swiss replica watches

    ReplyDelete
  63. The superb highly informative blog I’m about to share this with all my contacts.
    queens central booking

    ReplyDelete
  64. I’m glad to locate so much of informative data in your blog. dwi in new york

    ReplyDelete
  65. Your writers are extremely fantastic that have made easy to understand everything for us.

    filezilla client download

    ReplyDelete
  66. Great webpage brother I am about to notify this to all of my friends and contacts.
    vietnam holiday

    ReplyDelete
  67. The vital information in this blogs has allured me.
    sewing kit

    ReplyDelete
  68. Hi Dear, have you been certainly visiting this site daily, if that's the case you then will certainly get good knowledge.exposed skin care coupon

    ReplyDelete
  69. The blogs and the articles have exemplifying stuff!!! Good job.
    best credit

    ReplyDelete
  70. I am really appreciating very much by seeing your interesting posts. vietnam holiday

    ReplyDelete
  71. I’m soooo happy through your blog posts, and waiting for the upcoming posts.vietnam tour

    ReplyDelete
  72. I was searching for many blogs site and now finally I have got this cool place with lots of information.vietnam holiday

    ReplyDelete
  73. I don’t waste my free time that’s why I read the informative things when I got this blog I really enjoyed reading this.

    home security companies

    ReplyDelete
  74. This is one of the most important blogs that I have seen, keep it up!
    electrical contractors brisbane

    ReplyDelete
  75. I love this blog because it is user friendly with appreciative information.
    NY criminal lawyer

    ReplyDelete
  76. I wonder why different professionals don't take action your website overmuch m happy I got this.
    central booking ny

    ReplyDelete
  77. Really informative and useful information. luxury Villas Marrakech

    ReplyDelete
  78. The content on your website never confuses me villas online

    ReplyDelete
  79. Your site is for sure worth bookmarking. best electric shavor 2014

    ReplyDelete
  80. Your writers are enormously tremendous. best panasonic Electric reviews

    ReplyDelete
  81. The content on your website never confuses me infographics

    ReplyDelete
  82. The quality of information that you are providing is simply marvelous. best web 2.0 sites

    ReplyDelete
  83. Your blog is extremely brilliant especially the quality content is really appreciable.life insurance

    ReplyDelete
  84. The people are very lucky to have this blog because it has better knowledge.what are the payday loan amounts in tennessee

    ReplyDelete
  85. Great - I should definitely pronounce, impressed
    with yiur website. I hadd nno trouble navigating through all tabs and related
    infprmation ended uup being truly easy to doo to access.
    I recently found what I hooped for before you know it inn the least.
    Quite unusual. Is likely to appreciate it for those who add forums or something, website
    theme . a tones way for your client to communicate.
    Nice task.

    ReplyDelete