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

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

#include <stdio.h>

#define lU_DEBUG_PREFIX "##########"

#define LU_DEBUG_CMD 0x01
#define LU_DEBUG_DATA 0x02
#define LU_DEBUG_ERROR 0x04

#define LU_PRINTF_cmd(msg...) do{if(lu_debugs & LU_DEBUG_CMD)printf(lU_DEBUG_PREFIX msg);}while(0)
#define LU_PRINTF_data(msg...) do{if(lu_debugs & LU_DEBUG_DATA)printf(lU_DEBUG_PREFIX msg);}while(0)
#define LU_PRINTF_error(msg...) do{if(lu_debugs & LU_DEBUG_ERROR)printf(lU_DEBUG_PREFIX msg);}while(0)


#define lu_printf(level, msg...) LU_PRINTF_##level(msg)
#define lu_printf2(...) printf(__VA_ARGS__)
#define lu_printf3(...) lu_printf(__VA_ARGS__)
static int lu_printf4_format(int prio, const char *fmt, ...);
#define lu_printf4(prio, fmt...) lu_printf4_format(prio, fmt)


int lu_debugs;

int main(int argc, char *argv[])
{
    lu_debugs |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;
    printf("lu_debugs = %p\n", lu_debugs);
    lu_printf(cmd,"this is cmd\n");
    lu_printf(data,"this is data\n");
    lu_printf(error,"this is error\n");
    lu_debugs &= ~(LU_DEBUG_CMD | LU_DEBUG_DATA);
    printf("lu_debugs = %p\n", lu_debugs);
    lu_printf(cmd,"this is cmd\n");
    lu_printf(data,"this is data\n");
    lu_printf(error,"this is error\n");
    lu_printf2("aa%d,%s,%dbbbbb\n", 20, "eeeeeee", 100);
    lu_debugs |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;
    printf("lu_debugs = %p\n", lu_debugs);
    lu_printf3(cmd,"this is cmd \n");
    lu_printf3(data,"this is data\n");
    lu_printf3(error,"this is error\n");
    lu_printf4(0,"luther %s ,%d ,%d\n", "gliethttp", 1, 2);
    return 0;
}

#include <stdarg.h>
static int lu_printf4_format(int prio, const char *fmt, ...)
{
#define LOG_BUF_SIZE (4096)
    va_list ap;
    char buf[LOG_BUF_SIZE]; 

    va_start(ap, fmt);
    vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
    va_end(ap);

    printf("<%d>: %s", prio, buf);
    printf("------------------------\n");
    printf(buf);
}

#define ENTER() LOGD("enter into %s", __FUNCTION__)


#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))

#define LOG(priority, tag, ...) \
    LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)

#define LOG_PRI(priority, tag, ...) \
    android_printLog(priority, tag, __VA_ARGS__)


#define android_printLog(prio, tag, fmt...) \
    __android_log_print(prio, tag, fmt)

[经验分享]程序日志中自动记录所在函数名、文件名、行号

#include <stdio.h>

#define LOG_DEBUG "DEBUG"
#define LOG_TRACE "TRACE"
#define LOG_ERROR "ERROR"
#define LOG_INFO  "INFOR"
#define LOG_CRIT  "CRTCL"

#define LOG(level, format, ...) \
    do { \
        fprintf(stderr, "[%s|%s@%s,%d] " format "\n", \
            level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ ); \
    } while (0)

int main()
{
    LOG(LOG_DEBUG, "a=%d", 10);
    return 0;
}

或者

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值