Customize your own print function---beginner's chapter

Source: Internet
Author: User

  

usually in the Linux platform when writing code, although GDB debugging function is very powerful, but many times I prefer to call the system printing function to debug my program, but the direct call will inevitably appear very stupid (China seems to be not lack of stupid coder), Unless I need to print more than 3 places, or I absolutely can not tolerate, but what should be done, do not worry, first go to pour a cup of water, and then casually bubble something, then sit down to step-by-step slowly finishing.

First of all to clarify their own needs, limited to the primary article, so I set the requirements are:

    • You can print information like a system print function;
    • Use can not be more complex than the system printing function;
    • can automatically output the specific location of the printing information;
    • With unified turn on and off printing function;

To meet the above requirements, there seems to be no better solution than the macro definition, first look at a gcc-style:

#define MYDEBUG (format, args ...) printf (format, # #args)

If you do not use GCC to compile your C program, you can use the C99 style:

#define MYDEBUG (format, ...) printf (format, # #__VA_ARGS__)

as for which, my advice is: Although the GCC style is a bit more readable, it is not a C-language standard, so use the standard style. Also Xu People will have doubts about ' # # ', in fact, its main function is to connect the two parameters, if the latter parameter is empty, will remove the extra comma, to prevent grammatical errors.

So far the macro definition above can only meet the first two requirements, let's take a look at the third requirement, then we need to use some standard booking macros:

  • __line__: Inserting the current source code line number in the source code
  • __FILE__: Inserts the current source file name in the source files
  • __DATE__: Insert the current compilation date in the source file
  • __time__: Inserting the current compilation time in the source file
  • __FUNC__: Inserts the current function name in the source code
  • __STDC__: This identity is assigned a value of 1 when a program is required to strictly adhere to the ANSI C standard
  • __cplusplus: This identifier is defined when you write a C + + program

like the function of the name related to the macro and __func__, __function__, as to exactly which one, I want to say is "Standard is the king." With these macros, the rest of the problem is very simple, now let us solve all the needs at once, see the Sword:

#if 1#define mydebug (format, ...)  printf ("[%s:%d]" format "", __file__, __line__, # #__VA_ARGS__)#else#define mydebug (format, ...) #endif

At present this way is the most elementary customization, to cope with small-scale debugging enough, if you feel that the above format does not like, you can enjoy to change to the format you like, I just provided a template only, in view of the topic of this article, so no further discussion, the next section, I will let our printing function has a hierarchical concept, Depending on the level of the output of different colors, please look forward to ...

Customize your own print function---beginner's chapter

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.