How to print file names, line numbers, and function names

Source: Internet
Author: User

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
  1. /**************************************************************************
  2. * @Copyright (c), CHENMH, All rights reserved.
  3. * @file: Main.cpp
  4. * @version: Ver 1.0
  5. * @author: CHENMH
  6. * @date: 2013/05/22 14:07
  7. * @brief: The method of printing the file name, line number, function name.
  8. **************************************************************************/
  9. #include <cstdio>
  10. Define the print macro and include the file name, line number, and function name before printing the information
  11. This macro expands, similar to printf ("123"), printf ("456");
  12. #define TRACE_CMH_1 (printf ("%s (%d)-<%s>:", __file__, __line__, __function__), printf)
  13. This macro expands, similar to printf ("%d" "%d", 1, 2);
  14. #define TRACE_CMH_2 (FMT,...) \
  15. printf ("%s (%d)-<%s>:" # #fmt, __file__, __line__, __function__, # #__VA_ARGS__)
  16. Note: Because the first macro trace_cmh_1 called printf two times, efficiency does not have a second macro high.
  17. If the compiler supports the C99 standard, you can use a second macro.
  18. int count = 1;
  19. Class CBase
  20. {
  21. Public
  22. CBase ()
  23. {
  24. //Print the file, line number, function, and other information of the current line.
  25. Trace_cmh_2 ("BASE: [%d]\n", count++);
  26. }
  27. };
  28. Class Csub: Public CBase
  29. {
  30. Public
  31. Csub ()
  32. {
  33. //Print the file, line number, function, and other information of the current line.
  34. Trace_cmh_1 ("SUB: [%d]\n", count++);
  35. }
  36. };
  37. int main (int argc, char **argv)
  38. {
  39. Csub Sub;
  40. return 0;
  41. }

2. Use in the project

[CPP]View PlainCopy
  1. /********************************************************
  2. * @author: CHENMH
  3. * @date: 2013/05/24 10:11
  4. * @brief: Define DEBUG Print Macros
  5. ********************************************************/
  6. #define _DEBUG_TRACE_CMH_ 2
  7. #if 0! = _debug_trace_cmh_
  8. #include <cstdio>
  9. #endif
  10. #if 1==_debug_trace_cmh_//Normal printing
  11. #define TRACE_CMH printf
  12. #elif 2==_debug_trace_cmh_//print file name, line number
  13. #define TRACE_CMH (FMT,...) \
  14. printf ("%s (%d):" # #fmt, __file__, __line__, # #__VA_ARGS__)
  15. #elif 3==_debug_trace_cmh_//print file name, line number, function name
  16. #define TRACE_CMH (FMT,...) \
  17. printf ("%s (%d)-<%s>:" # #fmt, __file__, __line__, __function__, # #__VA_ARGS__)
  18. #else
  19. #define TRACE_CMH
  20. #endif//_trace_cmh_debug_
  21. /*******************************************************/

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

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.