Macro definition of debugging code

Source: Internet
Author: User

During program debugging, we often need to output some debugging information. After the debugging is completed, we no longer need to use it. So how can I quickly switch between the debugging and publishing statuses? We usually use pre-compilation and macro definition to solve this problem, for example:
# Ifdef debug
Debug code
# Endif
If we use printf to display some debugging information, it is very troublesome to add # ifdef and # endif in every place. We can define a function of dbgprs INTF to deal with these things. Just put # ifdef and # endif in the dbgprs INTF function. However, when the code is running, the callback function is still called, which wastes time. Can macro definition be used to implement macros that are not generated by code compilation?
You can try the following macro code:
# Ifdef debug
# Define dbgprs INTF printf
# Else
# Define dbuplintf //
/Dbuplintf
# Endif
If debug has already been defined, it should not be said that, of course, it is replaced by dbgprs INTF with printf.
The following is only an analysis of the undefined debug scenario. This macro definition defines "dbgprs INTF" as "// dbgprs INTF". Due to the function of the line feed, # When define is defined, the annotator "//" is not found, but after the code is expanded, it becomes the annotator "//", that is, if your original code is dbuplintf ("% d", x);, after the macro is expanded, it becomes // dbuplintf ("% d", x );, it is equivalent to automatically adding the annotator "//" in front. It should be noted that the "/" following the line feed must be written in the top level, otherwise it will not be. In addition, this macro can only be used in one row, because it has commented out the code behind this row.

This is mainly because some compilers do not support the definition of the variable length parameter macro, while the printf function is just a variable length parameter. Therefore, it is difficult to define a blank macro, I came up with a way to change the macro into a "//" comment. You can also use
// # Define debug
# Ifdef debug
# Define dbg (CODE) Code
# Else
# Define dbg (CODE)
# Endif

It is a little troublesome to add dbg to debugging information when writing code, such

Dbg (printf ("% d", x );)

In this way, there will be no problem, and many lines of code can be written in it.

In Linux, there are usually the following statements:

#ifdef _DEBUG 
#define DBG2 (fmt, args...) printf(fmt, ##args) 
#else 
#define DBG2 (fmt, args...) 
#endif
#ifdef DEBUG3
#ifndef DEBUG2
#define DEBUG2
#endif
#endif

#ifdef DEBUG2
#ifndef DEBUG1
#define DEBUG1
#endif
#endif

#ifdef DEBUG1
#ifndef DEBUG0
#define DEBUG0
#endif
#endif

#ifdef DEBUG0
#ifndef DEBUG
#define DEBUG
#endif
#endif

#ifdef DEBUG
#ifndef DEBUG0
#define DEBUG0
#endif
#endif

#ifdef DEBUG3
#define debug3_print(...) printf(__VA_ARGS__)
#else
#define debug3_print(...)
#endif

#ifdef DEBUG2
#define debug2_print(...) printf(__VA_ARGS__)
#else
#define debug2_print(...)
#endif

#ifdef DEBUG1
#define debug1_print(...) printf(__VA_ARGS__)
#else
#define debug1_print(...)
#endif

#ifdef DEBUG0
#define debug0_print(...) printf(__VA_ARGS__)
#else
#define debug0_print(...)
#endif

#ifdef DEBUG
#define debug_print(...) printf(__VA_ARGS__)
#else
#define debug_print(...)
#endif

#endif
 

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.