The iPhone application shields log output based on the Debug and Release status changes.

Source: Internet
Author: User

IPhoneBased on the Debug andReleaseStatus change to shieldLog outputThis is what we want to introduce. We often use it when developing applications.NSLogTo debug our program. As the project growsLog outputIt will become difficult to manage. When releasing the official version, you must block all background outputs because these outputs consume system resources. At this point, we have to find one line at a time.NSLogCall and comment out.

This is more effective when the project is small, but as the project grows, it will become increasingly difficult to control. Next we will introduce a simple method for you to generateReleaseAll versions can be blocked without any changes.Log output.

First, we need to define such a pre-processing command, and the file name can start at will, such as CLog. h.

 
 
  1. #ifdef DEBUG  
  2. #define CLog(format, ...) NSLog(format, ## __VA_ARGS__)  
  3. #else  
  4. #define CLog(format, ...)  
  5. #endif 

Here, we determine whether the macro DEBUG is defined. If there is a definition, we will replace the CLog macro with the NSLog call. If there is no DEBUG flag defined, we will skip it directly. This is not hard to understand.

Check whether the DEBUG flag is correctly defined. Xcode usually defines the debug flag in the DEBUG running configuration item. If it is not defined, write it on our own. Take Xcode 4 as an example, for example:

Find the PreProcessor Macros attribute and write Debug for the DEBUG configuration, while leave it blank in the Release configuration. In this way, the pre-processing command just now can be used to determine whether the debug version or the release version is compiled to control the NSLog output. (Because Xcode 4 compares the two configuration items debug and release at the same time, while version 3.x can only be set separately. If you use xcode 3.x development tool, check both Debug and Release ).

At this point, our judgment is complete, but it is a bit more troublesome. If we want to use the CLog macro, We must import the CLog. h header file. However, Xcode provides us with a very clever solution. Let's take a look at the project file, is there a file called xxx-prefix.pch, as long as you notice the pch extension. What is the purpose of this file? The following is a sample of a pch file:

 
 
  1. //  
  2. // Prefix header for all source files  
  3. //  
  4. #import <Availability.h> 
  5. #ifndef __IPHONE_3_0  
  6. #warning "This project uses features only available in iPhone SDK 3.0 and later."  
  7. #endif  
  8. #ifdef __OBJC__  
  9.     #import <UIKit/UIKit.h> 
  10.     #import <Foundation/Foundation.h> 
  11. #endif 

Some header files are introduced here, which is actually a pre-compilation mechanism of Xcode. When we compile a project, there will be many common source files, and these code files will be hardly modified, therefore, Xcode only compiles these files once in the early stage, so that we can build these files multiple times in the future. For example, in UIKit and Foundation, this mechanism can speed up every time we build a project.

Of course, we don't have to go into it too deeply here. After knowing its role, we can use it to facilitate our development. We just need to introduce the just-created CLog. h here, so that all the files in our project can access the CLog macro we just defined. The following is the pch file after completion:

 
 
  1. #import <Availability.h> 
  2. #ifndef __IPHONE_3_0  
  3. #warning "This project uses features only available in iPhone SDK 3.0 and later."  
  4. #endif  
  5. #ifdef __OBJC__  
  6.     #import <UIKit/UIKit.h> 
  7.     #import <Foundation/Foundation.h> 
  8.     #import "CLog.h"  
  9. #endif 

In this way, our CLog is complete. Now we can use the CLog macro to output logs in any source file. The pre-processing command will automatically judge the current compilation configuration. If it is Debug, logs are output, but nothing is output.

Summary:IPhoneBased on the Debug andReleaseStatus change to shieldLog outputAfter the introduction, I hope it will help you through learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.