Variable parameter macros (DEBUG)

Source: Internet
Author: User

have been written around printf to print debugging information, do not need to be a comment out. After the Internet query found there are many ways to debug printing, parameters variable and convenient one-time switch.

#define DEBUG (FMT,...) printf (fmt,__va_args__) Here "..." refers to a mutable parameter.

int main () {DEBUG ("Hello%d", ten); return 0;}

Sometimes, a module has output information, error messages, and so on. If you want to open a section individually, you can design:

  1: #define DRV_DEBUG       1
  2: #define DRV_DEBUG_IN    0x0001
  3: #define Drv_debug_out   0x0002
  4: #define DRV_DEBUG_ERR   0x0004
  5: #define Drv_debug_all   0xFFFF
  6: #if drv_debug
  7:      unsignedint drv_flags = Drv_debug_err | Drv_debug_out;
  8:      #define DRV_PRINT (flag, FMT, ...)        
  9: Do        {                                  
:            if (drv_flags & Flag) {           
One:                 printf(FMT, __va_args__);}  
:            } while(0)
: #Else
: #define DRV_PRINT (FMT,...)
: #endif

In this way, I just print out and err:

  void drv_write (char * msg_out)
  2: {
  3:drv_print (Drv_debug_out, "%s", msg_out); //Output
  4:drv_print (Drv_debug_err, "%s", msg_out); //Output
  5:drv_print (drv_debug_in, "%s", msg_out); //No output
  6:}
Further, can be designed for the entire system of different modules of the log output control! TCP/IP protocol stack implementation LWIP that's how it's done.
But found that the light has printf is not enough, although debugging information is out, but in so many debugging information can not immediately know where this information is printed from. So you can use a different method to print the file name and source location.
There are several macros in ANSI C
__line__: Inserting the current line number
__file__: Inserting the current original file name
__date__: Inserting the current compilation date
__time__: Inserting the current compile time
So that's what the macro looks like. Debug (fmt,...)    printf ("FILE:" __file__ "", line:%d: "FMT" \ n ", __line__,# #__VA_ARGS__)
Pay special attention to the following # #__VA_ARGS__这里的 # #是很有必要的, because he means that if there are no arguments, let the preprocessor ignore the preceding comma. The first paragraph above does not add parameters to the error. So the first paragraph must have parameters.
Drv_print (drv_debug_out, "Hello World");//There is a problem, you need to change the macro definition, preceded by # #

Variable parameter macros (DEBUG)

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.