Output _ file __,__ line __of program debugging information, macro definition of indefinite Parameters

Source: Internet
Author: User

Preface:

When writing a program, we always add some printf and other statements to output debugging information, however, the most inconvenient part of the printf statement is that when we need to release a program, we need to delete these statements one by one. Once we need to debug the program again, these statements have to be added one by one, which makes us very inconvenient, wastes a lot of time, and causes low debugging efficiency. Therefore, many may choose to use macro-defined methods to output debugging statements.

For example, define a macro switch:

# DEFINE _ debug

When debugging is required, use the following statement:

# Ifdef _ debug

Printf (XXX );

# Endif

For this debugging method, you can use UNDEF _ debug to notify the compiler not to compile these statements, so that these statements are no longer output. However, the trouble with this method is obvious. Every debugging statement needs to be surrounded by two macro definitions. This is not only inconvenient for coding, but also for the source code structure, the workload is still small.

If we can program one of these three statements, how comfortable should we be ~, Therefore, we want to use the following statement:

# Ifdef _ debug

# Define debug (Info) printf (Info)

# Else

# Define debug (Info)

# Endif

In this way, we can use a debug statement when writing code. We can open the macro toggle _ debug, and all the debug (Info) macro definition information will be replaced with printf (Info), and closed will be replaced with null, so it will not be compiled. Well, this is much more convenient. You can use a single statement ~~~ However, the problem also arises. printf supports multiple parameters and is an indefinite parameter. When you use the following statement, an error is returned:

Debug ("% s", MSG)

This is because the macro definition of DEBUG (Info) only supports replacement of one parameter.

Therefore, we hope that debug can support multiple parameters like printf, and these parameters are just expanded as the parameters used by the printf statement. For example, we want debug ("% s ", MSG) can be expanded to printf ("% s", MSG)

Body:

After reading the data on the Internet, we found that since the c99 standard, the compiler began to support the macro definition of indefinite parameters, just like printf.

You can take a look at this article: http://blog.csdn.net/aobai219/archive/2010/12/22/6092292.aspx

(This link is also transferred. I can't find out who the original author is. Alas, Internet ...)

Therefore, we define such a thing:

# Define debug (format ,...) printf (format, ##__va_args _) ('#' indicates that if a variable parameter is ignored or empty, the Preprocessor (Preprocessor) is enabled) remove the comma before it .)

As a result, we find that debug completely replaces printf, and all debug (...) All are replaced with printf (...), It will no longer be troubled by the hateful comma.

However, we found that printf alone is not enough. Although debugging information is output, many debugging information is output, and we cannot immediately know where the information is printed, so we thought again, could we print out the current file name and the location of the source code line? Isn't that clear? Where can we use it, where can I find the debugging information and print it out!

So we have the following story...

Compiler built-in macros:

First, we will introduce several built-in macro definitions of the compiler. These macro definitions can not only help us write cross-platform source code, but also help us output useful debugging information flexibly.

The ansi c standard has several predefined macros (which are also commonly used ):

_ Line __: Insert the current source code line number into the source code;

_ File __: insert the name of the current source file into the source file;

_ Date __: Insert the current compilation date into the source file

_ Time __: Insert the current compilation time into the source file;

_ Stdc __: when the program strictly follows the ansi c standard, the ID is assigned 1;

_ Cplusplus: This identifier is defined when a C ++ program is compiled.

The compiler automatically replaces these macros with the corresponding content during source code compilation.

Here, your eyes should be highlighted. Well, yes, __file _ and _ line _ are exactly what we wanted to output. Therefore, each of our statements becomes:

Debug ("file: % s, line: % d... ",__ File __,__ line __,...)

In fact, there is no need, __file _ itself will be replaced by a character constant by the compiler, so our statement becomes like this again:

Debug ("file:" _ file _ ", line: % d... ",__ Line __,...)

However, we still do not meet the requirements. We still find it annoying. Why should we write "file:" _ file _ ", line: % d and, __line, what about these two parts? Isn't that a waste of time?

Haha, yes, this is the final conclusion. Write debug as follows:

Debug (format ,...) printf ("file:" _ file _ ", line: % d:" format "/N", _ line __,#__ va_args __)

That's it! Below, all the debug information will be output in this way:

File: XXX, line: XXX ,.......

Finally:

Finally, the old rule is coding testing.

View
Plain · 40 ·· · 50 ······ · 90 ···· · 140 · 150
  1. // ================================================ ==============================================
  2. // Name:
    Debug. cpp
  3. // Author
    : Boyce
  4. // Version
    1.0
  5. // Copyright
    : PKU
  6. // Description
    : Hello world in C ++, ANSI-style
  7. // ================================================ ==============================================
  8. # Include
  9. # DEFINE _ debug __
  10. # Ifdef _ debug __
  11. # Define debug (format ,...)
    Printf ("file:" _ file _ ", line: % 05d:" format "/N", _ line __, ##__ va_args __)
  12. # Else
  13. # Define debug (format ,...)
  14. # Endif
  15. Int
    Main (){
  16. Char
    STR [] = "Hello World ";
  17. Debug ("A ha, check
    Me: % s ", STR );
  18. Return
    0;
  19. }

Test results:

Is it cool? O (∩) O Haha ~

Http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html

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.