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!

99 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. How fine of you!!!! Really awesome efforts you have shown.
    payday loan

    ReplyDelete
  13. 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
  14. It’s an outstanding article in favor of all the web guests; they will get benefit from it I am confident.
    Uliea Homepage

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    ReplyDelete
  33. 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
  34. I envisage feeling to have the increase that your knowledge.
    infographic design

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    ReplyDelete
  49. congratulations guys, quality information you have given!!! english help online

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

    ReplyDelete
  51. You people have actually provided the best blogs that are easy to understand for the folks. restaurants near woodfield mall

    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. You people have actually provided the best blogs that are easy to understand for the folks. pirater un compte facebook

    ReplyDelete
  55. 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
  56. I would be flattened if all websites gave articles like that. vietnam travel

    ReplyDelete
  57. Your blogs are truly awesome I have no words to praise for your blogs.
    mango exporter pakistan

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

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

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

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

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


    Manhattan central booking

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

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

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

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

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

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

    filezilla client download

    ReplyDelete
  69. This blog post is really great; the standard stuff of the post is genuinely amazing.
    vietnam tour

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

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

    ReplyDelete
  72. 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
  73. The blogs and the articles have exemplifying stuff!!! Good job.
    best credit

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

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

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

    ReplyDelete
  77. 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
  78. This is one of the most important blogs that I have seen, keep it up!
    electrical contractors brisbane

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

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

    ReplyDelete
  81. We are dealing in superior quality of methylone, our research center takes very step to produce the best quality methylone.for more detail visit our site

    http://methyloneshoppe.com/

    ReplyDelete
  82. I absolutely respect and appreciate your point on each and every object. find villa marrakech

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

    ReplyDelete
  84. Your writers are enormously tremendous. villas online

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

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

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

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

    ReplyDelete