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.
//============================================================================// Name : debug.cpp// Author : boyce// Version : 1.0// Copyright : pku// Description : Hello World in C++, Ansi-style//============================================================================#include <stdio.h>#define __DEBUG__#ifdef __DEBUG__#define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, ##__VA_ARGS__)#else#define DEBUG(format,...)#endifint main(int argc, char **argv) { char str[]="Hello World"; DEBUG("A ha, check me: %s",str); return 0;}
Test results:
Is it cool? O (∩) O Haha ~
Note:
I don't understand. Why does the source code Display Control of csdn always miss the content after # include? Isn't that a bug? Why has it not been repaired for so long?