新闻  |   论坛  |   博客  |   在线研讨会
调试打印级别的简单实现
电子禅石 | 2020-08-03 19:06:00    阅读:1044   发布文章

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

int g_nMizarLogLevel = 1;


void trace_info(const char* format,const int level,const char* file, int line, const char* func,  ...);

// #define LOGV(format, arg...)  trace_info(format, 4, __FILE__, __LINE__, __func__, ##arg);
// #define LOGD(format, arg...)  trace_info(format, 4, __FILE__, __LINE__, __func__, ##arg);
// #define LOGI(format, arg...)  trace_info(format, 3, __FILE__, __LINE__, __func__, ##arg);
// #define LOGW(format, arg...)  trace_info(format, 2, __FILE__, __LINE__, __func__, ##arg);
// #define LOGE(format, arg...)  trace_info(format, 1, __FILE__, __LINE__, __func__, ##arg);
// #define LOGF(format, arg...)  trace_info(format, 1, __FILE__, __LINE__, __func__, ##arg);
#define LOGD(format, ...) port_printf(format, ##__VA_ARGS__)

//#define LOG_TRACE(format, level, file, line, func, ...)  port_printf("\n%s - %s(%d) - %s: ",format, level, file, line, func, ##__VA_ARGS__)
#define LOG_TRACE(format, level, file, line, func, arg...)  trace_info(format, level, file, line, func)//好奇怪这样就可以了。

#define LOGW(format, ...)  LOG_TRACE(format, 2, __FILE__, __LINE__, __func__, __VA_ARGS__)
void port_printf(const char *format, ...){

    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
}

void trace_info(const char* format,const int level,const char* file, int line, const char* func,  ...)
{
    
    if (g_nMizarLogLevel < level)
    {
        return;
    }
    fprintf(stdout, "%d :[%s:%d]%s: ",level, file, line, func);
    va_list vaList;
    va_start(vaList, format);
    vfprintf(stdout, format, vaList);
    va_end(vaList);
    fprintf(stdout, "%c", '\n');
}

int main(int argc, char *argv[])
{
    LOGD("hello \n");
    LOGW("log warning test \n");
    return 0;
}


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客