easylogger使用

EasyLogger简单使用

移植

  1. 下载源码

    git clone git@github.com:armink/EasyLogger.git
    
  2. 将c文件和头文件复制到项目中,并包含相应的头文件路径

    image-20211018135634304

  3. 修改elog_cfg.h(不需要的直接注释)

    #ifndef _ELOG_CFG_H_
    #define _ELOG_CFG_H_
    /*---------------------------------------------------------------------------*/
    /* enable log output. */
    #define ELOG_OUTPUT_ENABLE
    /* setting static output log level. range: from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE */
    #define ELOG_OUTPUT_LVL                          ELOG_LVL_VERBOSE
    /* enable assert check */
    #define ELOG_ASSERT_ENABLE
    /* buffer size for every line's log */
    #define ELOG_LINE_BUF_SIZE                       256
    /* output line number max length */
    #define ELOG_LINE_NUM_MAX_LEN                    5
    /* output filter's tag max length */
    #define ELOG_FILTER_TAG_MAX_LEN                  30
    /* output filter's keyword max length */
    #define ELOG_FILTER_KW_MAX_LEN                   16
    /* output filter's tag level max num */
    #define ELOG_FILTER_TAG_LVL_MAX_NUM              5
    /* output newline sign */
    #define ELOG_NEWLINE_SIGN                        "\n"
    /*---------------------------------------------------------------------------*/
    /* enable log color */
    #define ELOG_COLOR_ENABLE
    /* change the some level logs to not default color if you want */
    #define ELOG_COLOR_ASSERT                        (F_MAGENTA B_NULL S_NORMAL)
    #define ELOG_COLOR_ERROR                         (F_RED B_NULL S_BLINK)
    #define ELOG_COLOR_WARN                          (F_YELLOW B_NULL S_NORMAL)
    #define ELOG_COLOR_INFO                          (F_CYAN B_NULL S_NORMAL)
    #define ELOG_COLOR_DEBUG                         (F_GREEN B_NULL S_NORMAL)
    #define ELOG_COLOR_VERBOSE                       (F_BLUE B_NULL S_NORMAL)
    /*---------------------------------------------------------------------------*/
    ///* enable asynchronous output mode */
    //#define ELOG_ASYNC_OUTPUT_ENABLE
    ///* the highest output level for async mode, other level will sync output */
    //#define ELOG_ASYNC_OUTPUT_LVL                    ELOG_LVL_ASSERT
    ///* buffer size for asynchronous output mode */
    //#define ELOG_ASYNC_OUTPUT_BUF_SIZE               (ELOG_LINE_BUF_SIZE * 10)
    ///* each asynchronous output's log which must end with newline sign */
    //#define ELOG_ASYNC_LINE_OUTPUT
    ///* asynchronous output mode using POSIX pthread implementation */
    //#define ELOG_ASYNC_OUTPUT_USING_PTHREAD
    ///*---------------------------------------------------------------------------*/
    ///* enable buffered output mode */
    //#define ELOG_BUF_OUTPUT_ENABLE
    ///* buffer size for buffered output mode */
    //#define ELOG_BUF_OUTPUT_BUF_SIZE                 (ELOG_LINE_BUF_SIZE * 10)
    
  4. 修改elog_port.c

    #include "main.h"			//hal_gettick()声明
    #include "printf.h"			//printf()声明
    uint8_t timestamp_s[10];	//时间戳字符串
    
    //输出接口
    void elog_port_output(const char *log, size_t size) {
        /* add your code here */
        printf("%.*s",size,log);
    }
    //输出上锁
    void elog_port_output_lock(void) {
        
        /* add your code here */
        //关闭全局中断(裸机)
    	__set_PRIMASK(1);
    }
    //输出关锁
    void elog_port_output_lock(void) {
        
        /* add your code here */
        //关闭全局中断(裸机)
    	__set_PRIMASK(1);
    }
    //获取时间戳
    const char *elog_port_get_time(void) {
        
        /* add your code here */
    	snprintf((char *)timestamp_s,sizeof(timestamp_s),"%09lu",HAL_GetTick());//获取tick计数值作为时间戳,09lu:32位无符号整形(字符串为9位)
    	return (char *)timestamp_s;
    }
    
    //其他接口:获取进程号,线程号等根据需要添加
    
  5. 初始化

    elog_init();								//elog初始化
    elog_set_text_color_enabled(true);			//使能文本颜色输出
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);//设置输出格式
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_T_INFO | ELOG_FMT_P_INFO));
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_T_INFO | ELOG_FMT_P_INFO));
    elog_start();								//elog启动
    

运行效果

image-20211015183245682

  1. 等级
  2. 标签
  3. 时间戳
  4. 进程
  5. 线程
  6. 目录
  7. 行号
  8. 函数名
  9. 输出

注意事项

当引入第三方printf库作为elog的输出时,要把该库的头文件包含到elog_cfg.h

image-20211026154601119

相关推荐

  1. 17_c/c++开源库 easylogging日志库

    2024-07-17 21:52:02       31 阅读
  2. conda使用,pip使用

    2024-07-17 21:52:02       53 阅读
  3. VueUse使用

    2024-07-17 21:52:02       65 阅读
  4. Git<span style='color:red;'>使用</span>

    Git使用

    2024-07-17 21:52:02      56 阅读
  5. netty使用

    2024-07-17 21:52:02       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 21:52:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 21:52:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 21:52:02       57 阅读
  4. Python语言-面向对象

    2024-07-17 21:52:02       68 阅读

热门阅读

  1. 算法训练营day72

    2024-07-17 21:52:02       23 阅读
  2. 第二章:pod-运行于kubernetes中的容器

    2024-07-17 21:52:02       17 阅读
  3. 昇思25天学习打卡营第25天|SSD目标检测

    2024-07-17 21:52:02       20 阅读
  4. 怎么选择适合自己的酱香白酒?

    2024-07-17 21:52:02       19 阅读
  5. UDP协议

    UDP协议

    2024-07-17 21:52:02      22 阅读
  6. D365 Fraud Protection Account Protection部署方案

    2024-07-17 21:52:02       25 阅读
  7. 解决数据卷root权限问题的Docker科研向实践思路

    2024-07-17 21:52:02       24 阅读
  8. Webservice使用RestSharp封送SOAP

    2024-07-17 21:52:02       23 阅读