zoukankan      html  css  js  c++  java
  • C语言:类似linux内核的分等级DEBUG宏(打印宏)

    总结几种log打印printf函数的宏定义 http://blog.chinaunix.net/uid-20564848-id-73402.html

    #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(g_lu_debugs_level & LU_DEBUG_CMD)printf(lU_DEBUG_PREFIX msg);}while(0)
    #define LU_PRINTF_data(msg...) do{if(g_lu_debugs_level & LU_DEBUG_DATA)printf(lU_DEBUG_PREFIX msg);}while(0)
    #define LU_PRINTF_error(msg...) do{if(g_lu_debugs_level & 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 g_lu_debugs_level; //控制打印等级的全局开关
    //lu_printf 类似内核的分等级打印宏,根据g_lu_debugs_level和输入的第一个标号名来决定该句打印是否输出。
    //lu_printf3 等同于 lu_printf
    //lu_printf2 等同于 printf
    //lu_printf4 等同于 lu_printf4_format,作用是把输入的第一个整型参数用<val>的格式打印出来
    int main(int argc, char *argv[])
    {
        g_lu_debugs_level |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;
        printf("g_lu_debugs_level = %p
    ", g_lu_debugs_level);
        lu_printf(cmd,"this is cmd
    ");
        lu_printf(data,"this is data
    ");
        lu_printf(error,"this is error
    ");
        g_lu_debugs_level &= ~(LU_DEBUG_CMD | LU_DEBUG_DATA);
        printf("g_lu_debugs_level = %p
    ", g_lu_debugs_level);
        lu_printf(cmd,"this is cmd
    ");
        lu_printf(data,"this is data
    ");
        lu_printf(error,"this is error
    ");
        lu_printf2("aa%d,%s,%dbbbbb
    ", 20, "eeeeeee", 100);
        g_lu_debugs_level |= LU_DEBUG_CMD | LU_DEBUG_DATA | LU_DEBUG_ERROR;
        printf("g_lu_debugs_level = %p
    ", g_lu_debugs_level);
        lu_printf3(cmd,"this is cmd 
    ");
        lu_printf3(data,"this is data
    ");
        lu_printf3(error,"this is error
    ");
        lu_printf4(0,"luther %s ,%d ,%d
    ", "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("------------------------
    ");
        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)
  • 相关阅读:
    NX 调试批处理文件
    NXOpen 更改UI对话框宽度
    NXOpen 遍历体 移动图层
    NXOpen 设置工作图层 一键开关图层
    NXOpen Block UI弹出另一个对话框实例
    NXOpen 座标UI获取 (原点 矩阵变换)
    [StackExchange]Redis 的几种类型的操作(string,hash,lists,set,sorted set)
    IIS 并发连接 设置与测试
    [StackExchage]Redis 的连接与操作(跨机器)
    Redis 事务
  • 原文地址:https://www.cnblogs.com/mylinux/p/5906576.html
Copyright © 2011-2022 走看看