In the development process, we often need to use NSLog output some information, and even some development process, must view the output in the console, experienced programmers can know the entire data interaction process through the console output. However, a released program with too much NSLog output will definitely affect the performance of the App. In this case, we can use a macro definition to deal with it. Use DEBUG mode during development and RELEASE mode during RELEASE. In this way, the distributed App does not do a lot of NSLog output inside the program.

#if defined (DEBUG)||defined(_DEBUG)   
 NSLog(@"Test code");    
NSLog(@"Test Coding");
#endif
Copy the code

The #if #endif macro definition above means that if DEBUG is defined, NSLog output is used; Otherwise, the code is simply ignored. Don’t worry about this, it comes from Xcode’s default Settings. You can disable DEBUG and enable RELEASE mode, as shown in the screenshot below

How do I determine if a project is in Debug or Release mode? Check whether the project’s Build Settings have been set with macro definition DEBUG.

#ifdef DEBUG    
// do sth
#else   
// do sth
#endif
Copy the code

You can also use the regular NSLog statement:

#ifndef DEBUG
#undef NSLog
#define NSLog(args,...)
#endif
Copy the code

Fortunately, there is an easiest way to log — through a macro that makes NSLog only work during debug builds. Add this functionality to globally accessible header files. This way you can use logs as much as you like, and when you do production, there is no log code involved. The following code:

#ifdef DEBUG 
#defineDMLog(...)
NSLog(@"%s %@", __PRETTY_FUNCTION__,[NSString stringWithFormat:__VA_ARGS__])
#else# defineDMLog(...) do{}while(0)
Copy the code

P.S. If the above is a bit complicated the following is a lot easier

//release mask NSLog // put in.pch file#ifdef DEBUG
#else
#define NSLog(...) {};
#endif
Copy the code

Now if you use DMLog, the log will only be printed during a Debug build. Production builds don’t have any logs. __PRETTY_FUNCTION__ prints the function that prints the log.

Summary: While NSLog is excellent, it has some limitations: it can only be printed locally and does not support logs with levels (such as critical, warning, etc.). NSLog is inefficient. NSLog can seriously affect program performance when you’re doing a lot of processing and there are frameworks for logging on the Internet that you can use to avoid some of the limitations of NSLog. Here are two good ones: Cocoa LumberJack – this is a logging framework that is well known for Cocoa. It’s a little hard to use at first, but it’s very powerful. SNLog – an alternative to NSLog.