Today, when tracking a piece of open source code, because the code is not familiar with, so to print some key information. Then I sorted the macro that could print the file name, line number, and function name.
1, print file name, line number, function of two ways
/************************************************************************** * @Copyright (c) 2013, ChenMH, all rights
Reserved. * @file: Main.cpp * @version: Ver 1.0 * @author: CHENMH * @date: 2013/05/22 14:07 * @bri
EF: A way to print file names, line numbers, and function names. /#include <cstdio>//define print macros,
And before printing information, add file name, line number, function name//This macro expanded, 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, there is no second macro-high efficiency.
If the compiler supports the C99 standard, you can use the second macro.
int count = 1;
Class CBase {public:cbase () {//print the file, line number, function, and other information for 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 for 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
/********************************************************
* @author : chenmh
* @date : 2013/05/24 10:11
* @brief : Defining debug Print Macros
********************************************************/
#define _DEBUG_TRACE_CMH_ 2
#if 0!= _debug_trace_cmh_
#include <cstdio>
#endif
#if 1 ==_debug_trace_cmh_ //normal print
#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 filename, 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_
/*******************************************************/
This code uses these several macros:
1 __va_args__ is a variable-parameter macro that is added to the new C99 specification and is currently supported by both GCC and VC6.0 (VC6.0 compiler does not support it). The macro is preceded by a # #的作用在于, where the number of variable parameters is 0 o'clock, where the # #起到把前面多余的 "," removes the effect.
2) __file__ macros are replaced with the current source file name when precompiled
3) __line__ macros are replaced with the current line number when precompiled
4) __FUNCTION__ macros are replaced with the current function name when precompiled