单步调试总有无法胜任的时候,通常我们会打log.如:

#ifdef _DEBUG #define mylog(str) {/ FILE *fp = fopen("c://log.txt","ab");/ fprintf(fp, str); / fprintf(fp, "/n"); / fclose(fp);} #else #define mylog(...) 0 #endif

VS2005的编译器和GCC都支持宏的变长参数.

VS2005的语法如下:

#ifdef DEBUG #define PERROR(...) { fprintf(stderr, __VA_ARGS__); / fprintf(stderr, "/n"); } #else #define PERROR(...) 0 #endif

接着调用

PERROR("test");

输出到stderr其实就是输出到"输出窗口",比写到文件里方便多了.看这个:

使用宏的变长参数打LOG

GCC的方法如下(没有经过测试):

#ifdef DEBUG #define PERROR(args...) { fprintf(stderr, args); / fprintf(stderr, "/n"); } #else #define PERROR(...) 0 #endif

相关文章:

  • 2022-12-23
  • 2022-02-27
  • 2021-10-26
  • 2021-09-11
  • 2022-02-08
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2022-03-09
  • 2022-12-23
  • 2021-11-15
  • 2021-11-15
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案