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

来源:互联网 发布:spss mac安装教程 编辑:程序博客网 时间:2024/05/22 04:26

http://blog.chinaunix.net/u1/38994/showart_1168773.html

[c-sharp] view plaincopy
  1. #include <stdio.h>  
  2.  
  3. #define lU_DEBUG_PREFIX "##########"  
  4.  
  5. #define LU_DEBUG_CMD 0x01  
  6. #define LU_DEBUG_DATA 0x02  
  7. #define LU_DEBUG_ERROR 0x04  
  8.  
  9. #define LU_PRINTF_cmd(msg...) do{if(lu_debugs & LU_DEBUG_CMD)printf(lU_DEBUG_PREFIX msg);}while(0)  
  10. #define LU_PRINTF_data(msg...) do{if(lu_debugs & LU_DEBUG_DATA)printf(lU_DEBUG_PREFIX msg);}while(0)  
  11. #define LU_PRINTF_error(msg...) do{if(lu_debugs & LU_DEBUG_ERROR)printf(lU_DEBUG_PREFIX msg);}while(0)  
  12.  
  13.  
  14. #define lu_printf(level, msg...) LU_PRINTF_##level(msg)  
  15. #define lu_printf2(...) printf(__VA_ARGS__)  
  16. #define lu_printf3(...) lu_printf(__VA_ARGS__)  
  17. static int lu_printf4_format(int prio, const char *fmt, ...);  
  18. #define lu_printf4(prio, fmt...) lu_printf4_format(prio, fmt)  
  19.   
  20.   
  21. int lu_debugs;  
  22.   
  23. int main(int argc, char *argv[])  
  24. {  
  25.     lu_debugs |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;  
  26.     printf("lu_debugs = %p\n", lu_debugs);  
  27.     lu_printf(cmd,"this is cmd\n");  
  28.     lu_printf(data,"this is data\n");  
  29.     lu_printf(error,"this is error\n");  
  30.     lu_debugs &= ~(LU_DEBUG_CMD | LU_DEBUG_DATA);  
  31.     printf("lu_debugs = %p\n", lu_debugs);  
  32.     lu_printf(cmd,"this is cmd\n");  
  33.     lu_printf(data,"this is data\n");  
  34.     lu_printf(error,"this is error\n");  
  35.     lu_printf2("aa%d,%s,%dbbbbb\n", 20, "eeeeeee", 100);  
  36.     lu_debugs |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;  
  37.     printf("lu_debugs = %p\n", lu_debugs);  
  38.     lu_printf3(cmd,"this is cmd \n");  
  39.     lu_printf3(data,"this is data\n");  
  40.     lu_printf3(error,"this is error\n");  
  41.     lu_printf4(0,"luther %s ,%d ,%d\n""gliethttp", 1, 2);  
  42.     return 0;  
  43. }  
  44.  
  45. #include <stdarg.h>  
  46. static int lu_printf4_format(int prio, const char *fmt, ...)  
  47. {  
  48. #define LOG_BUF_SIZE (4096)  
  49.     va_list ap;  
  50.     char buf[LOG_BUF_SIZE];   
  51.   
  52.     va_start(ap, fmt);  
  53.     vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);  
  54.     va_end(ap);  
  55.   
  56.     printf("<%d>: %s", prio, buf);  
  57.     printf("------------------------\n");  
  58.     printf(buf);  
  59. }  
  60.  
  61. #define ENTER() LOGD("enter into %s", __FUNCTION__)  
  62.  
  63.  
  64. #define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))  
  65.  
  66. #define LOG(priority, tag, ...) \  
  67.     LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)  
  68.  
  69. #define LOG_PRI(priority, tag, ...) \  
  70.     android_printLog(priority, tag, __VA_ARGS__)  
  71.  
  72.  
  73. #define android_printLog(prio, tag, fmt...) \  
  74.     __android_log_print(prio, tag, fmt)  

 

http://www.linuxsir.org/bbs/showthread.php?t=302451

 

这里有篇不错的文章,[经验分享]程序日志中自动记录所在函数名、文件名、行号

比上面的要好多了:

[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, 

原创粉丝点击