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


[c-sharp]
  view plain copy
  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)  

 

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

比上面的要好多了:

[c-sharp]  view plain copy
  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 plain copy
  1. #define DBG(format, args...) fprintf(stderr, "[%s|%s@%s,%d] " format "\n", APP_NAME, __FUNCTION__, __FILE__, __LINE__, ## args );  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值