IPhone uses macros to control log output for debug and release, improving development efficiency

Source: Internet
Author: User

From: http://www.j2megame.com/html/xwzx/ty/2050.html

IPhone uses macros to control log output for debug and release, improving development efficiency

When developing applications, we often use nslog to debug our Program As the project grows, the log output for debugging becomes difficult to manage. When releasing the official version, you must block all background output.
When developing applications, we often use nslog to debug our programs. As the project grows, the log output for debugging becomes 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 the nslog call in one row and comment out it. 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 block all log output without any changes when generating the release version.
First, we need to define such a pre-processing command, and the file name can start at will, such as clog. h.
# Ifdef debug
# Define clog (format,...) nslog (format, # _ va_args __)
# Else
# Define clog (format ,...)
# 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:
//
// Prefix header for all source files
//
# Import <availability. h>
# Ifndef _ iphone_3_0
# Warning "This project uses features only available in iPhone sdks 3.0 and later ."
# Endif
# Ifdef _ objc __
# Import <uikit/uikit. h>
# Import <Foundation/Foundation. h>
# 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 are hardly modified, so xcode only compiles these files once in the early stage, so that we can use these files repeatedly in the subsequent build process. 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:
# Import <availability. h>
# Ifndef _ iphone_3_0
# Warning "This project uses features only available in iPhone sdks 3.0 and later ."
# Endif
# Ifdef _ objc __
# Import <uikit/uikit. h>
# Import <Foundation/Foundation. h>
# Import "clog. H"
# 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.

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.