Transferred from: http://blog.csdn.net/cabinriver/article/details/8960119
When tracking a piece of open source code today, because you are not familiar with the code, you need to print some key information. I just sort this out. A macro that can print the file name, line number, and function name.
1, print the file name, line number, function of two ways
[CPP]View PlainCopy
- /**************************************************************************
- * @Copyright (c), CHENMH, All rights reserved.
- * @file: Main.cpp
- * @version: Ver 1.0
- * @author: CHENMH
- * @date: 2013/05/22 14:07
- * @brief: The method of printing the file name, line number, function name.
- **************************************************************************/
- #include <cstdio>
- Define the print macro and include the file name, line number, and function name before printing the information
- This macro expands, similar to printf ("123"), printf ("456");
- #define TRACE_CMH_1 (printf ("%s (%d)-<%s>:", __file__, __line__, __function__), printf)
- This macro expands, similar to printf ("%d" "%d", 1, 2);
- #define TRACE_CMH_2 (FMT,...) \
- printf ("%s (%d)-<%s>:" # #fmt, __file__, __line__, __function__, # #__VA_ARGS__)
- Note: Because the first macro trace_cmh_1 called printf two times, efficiency does not have a second macro high.
- If the compiler supports the C99 standard, you can use a second macro.
- int count = 1;
- Class CBase
- {
- Public
- CBase ()
- {
- //Print the file, line number, function, and other information of the current line.
- Trace_cmh_2 ("BASE: [%d]\n", count++);
- }
- };
- Class Csub: Public CBase
- {
- Public
- Csub ()
- {
- //Print the file, line number, function, and other information of the current line.
- Trace_cmh_1 ("SUB: [%d]\n", count++);
- }
- };
- int main (int argc, char **argv)
- {
- Csub Sub;
- return 0;
- }
2. Use in the project
[CPP]View PlainCopy
- /********************************************************
- * @author: CHENMH
- * @date: 2013/05/24 10:11
- * @brief: Define DEBUG Print Macros
- ********************************************************/
- #define _DEBUG_TRACE_CMH_ 2
- #if 0! = _debug_trace_cmh_
- #include <cstdio>
- #endif
- #if 1==_debug_trace_cmh_//Normal printing
- #define TRACE_CMH printf
- #elif 2==_debug_trace_cmh_//print file name, line number
- #define TRACE_CMH (FMT,...) \
- printf ("%s (%d):" # #fmt, __file__, __line__, # #__VA_ARGS__)
- #elif 3==_debug_trace_cmh_//print file name, line number, function name
- #define TRACE_CMH (FMT,...) \
- printf ("%s (%d)-<%s>:" # #fmt, __file__, __line__, __function__, # #__VA_ARGS__)
- #else
- #define TRACE_CMH
- #endif//_trace_cmh_debug_
- /*******************************************************/
These are some of the macros used in this code:
1) __va_args__ is a variable parameter macro, this macro is new in the C99 specification, and now seems to support both GCC and VC6.0 (VC6.0 compiler does not support). The macro front Plus # #的作用在于, when the number of variable parameters is 0 o'clock, here the # #起到把前面多余的 "," remove the role.
2) __file__ macro will be replaced with the current source file name at precompilation
3) __LINE__ macro will be replaced with the current line number at precompilation
4) __FUNCTION__ macro will be replaced with the current function name at precompilation
Go from: http://blog.csdn.net/ly61baby/article/details/6777772 Print the name and number of lines of the current source file __file__, __line__2009-12-03 22:35
printf ("%s\nline%d:\n", __file__, __line__);
Prints the line number and source filename of the current statement in the source file.
#define DEBUG_MSG (printf ("%s[%d]:", __file__, __line__), printf)
Outputs debugging information at a line in the program.
__func__ get the current function name
How to print file names, line numbers, and function names