6

I would like to know if having many NSLog() calls affects app performance or memory. Does anyone know about such thing?

I want to put an NSLog() call in every function in my app (which is a lot) so that I can see crash logs after and trace problems.

Thanks.

Zigglzworth
  • 6,645
  • 9
  • 68
  • 107

3 Answers3

11

Yes. So I define this in my pch file.

#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif

// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

Instead of using NSLog, I use DLog and ALog.

(Note or copyrights: I got this code long long ago from some other SO post which I don't remember. Pasting it again from my snippet library)

Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • Yes, for NSLogs you expect to leave in the code for debugging, you should define macros that allow the "debugging" ones to be compiled out. Some, of course, should always be logged, so those use a different macro. Adding `__PRETTY_FUNCTION__` and `__LINE__` reduces the need to explicitly identify where you are in the code. (There's nothing more useless than an "I've had an error" message with no clue as to where it was sent.) – Hot Licks Sep 18 '11 at 12:58
  • This seems to be the origin of this idea, for those who are curious to know its evolution: http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog – benvolioT Feb 09 '12 at 04:04
7

Another easy solution to 'undefine' NSLog

In .pch file:

#ifndef DEBUG
#define NSLog(...) /* */
#endif
DarthMike
  • 3,471
  • 1
  • 22
  • 18
2

Yes, it slows down the performance, especially if the function is supposed to take very short time, the NSLog (which is an I/O process) will make it take more time than expected.

mohdajami
  • 9,604
  • 3
  • 32
  • 53