总结几种log打印printf函数的宏定义

来源:互联网 发布:python 基础教程 pdf 编辑:程序博客网 时间:2024/05/22 02:08

[c-sharp] view plaincopy
  1. #include <stdio.h>  
  2.  
  3. #define LOG_DEBUG "DEBUG"  
  4. #define LOG_TRACE "TRACE"  
  5. #define LOG_ERROR "ERROR"  
  6. #define LOG_INFO  "INFOR"  
  7. #define LOG_CRIT  "CRTCL"  
  8.  
  9. #define LOG(level, format, ...) \  
  10.     do { \  
  11.         fprintf(stderr, "[%s|%s@%s,%d] " format "\n", \  
  12.             level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ ); \  
  13.     } while (0)  
  14.   
  15. int main()  
  16. {  
  17.     LOG(LOG_DEBUG, "a=%d", 10);  
  18.     return 0;  
  19. }  

或者

[c-sharp] view plaincopy
  1. #define DBG(format, args...) fprintf(stderr, "[%s|%s@%s,%d] " format "\n", APP_NAME, __FUNCTION__, __FILE__, __LINE__, ## args );  

/*
a.c
*/
#include<stdio.h>
main()
{
  stderr=fopen("./err.log","w");
  fprintf(stderr,"hello world\n");
}
然后编译并执行:
[root@DevServer ch6]# gcc -o a a.c
[root@DevServer ch6]# ./a
[root@DevServer ch6]# cat err.log 
hello world

0 0
原创粉丝点击