嵌入式:EasyLogger 日志库

1:easylogger源码地址

        https://github.com/armink/EasyLogger

        下载的时候选择master的版本:查看elog.h的文件注意看版本号(2.2.99),如果版本不一样源码可能不一样,不一样的话用最新的即可;

/*
 * This file is part of the EasyLogger Library.
 *
 * Copyright (c) 2015-2019, Armink, <armink.ztl@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function: It is an head file for this library. You can see all be called functions.
 * Created on: 2015-04-28
 */

...省略...

/* EasyLogger software version number */
#define ELOG_SW_VERSION                      "2.2.99"

2:代码移植

2.1:代码源码文件

图2.1.1

2.2:代码源码文件移植到自己的工程

图2.2.1

        把需要编译的文件添加到工程:本人使用的是cmake,所有修改CMakeLists.txt文件,暂时没有添加EasyFlash相关的文件。如果使用keil的话,自行把文件添加到path即可。

file(GLOB EASYLOGGER_SRC
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/src/elog.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/src/elog_utils.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/src/elog_async.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/src/elog_buf.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/port/elog_port.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/port/elog_port_switch.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/plugins/flash/elog_flash.c
    ${PROJECT_SOURCE_DIR}/third_party/easylogger/plugins/flash/elog_flash_port.c
) 

2.3:elog_cfg.h文件修改

2.3.1:控制开启颜色宏

/*---------------------------------------------------------------------------*/
/* 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_NORMAL)
#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)
/*---------------------------------------------------------------------------*/

        “ELOG_COLOR_ENABLE” 这个被定义的话输出的log会带有颜色,下面的6个红,分别是控制不同等级log输出的颜色(颜色、背景色、字号)。

2.3.2:控制单行输出log的长度

/* buffer size for every line's log */
#define ELOG_LINE_BUF_SIZE                       128

        “ELOG_LINE_BUF_SIZE” 修改成可128个字节。

2.3.3:屏蔽不需要的宏

/* 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)

        ELOG_ASYNC_OUTPUT_USING_PTHREADELOG_BUF_OUTPUT_ENABLEELOG_BUF_OUTPUT_BUF_SIZE,这个三个宏都屏蔽掉。

2.4:elog_port.c文件修改

2.4.1:必需项

        elog_port_output:函数必须要添加自己的代码,已确认log输出的方式,可以是USB、串口、SEGGER_RTT。

        本章使用的是SEGGER_RTT,SEGGER_RTT也是需要移植第三方组件(后续有其他移植文章)。

/**
 * output log port interface
 *
 * @param log output of log
 * @param size log size
 */
void elog_port_output(const char *log, size_t size) {
    SEGGER_RTT_Write(SEGGER_RTT_TERMINAL_CHANNEL, log, size);
}

2.4.2:其他项

        当前运行的任务名:elog_port_get_t_info

/**
 * get current thread name interface
 *
 * @return current thread name
 */
const char *elog_port_get_t_info(void) {
    return pcTaskGetName(NULL);
}

        时间戳:elog_port_get_time

/**
 * get current time interface
 *
 * @return current time
 */
const char *elog_port_get_time(void) {
    // Note: cannot use in ISR
    static char cur_system_time[ELOG_SYSTIME_BUFF_MAX_SIZE] = { 0 };

    am_hal_rtc_time_t time;
    am_hal_rtc_time_get(&time);
    snprintf((char *)cur_system_time, sizeof(cur_system_time), "%04d%02d%02d %02d:%02d:%02d.%02d",
        time.ui32Year + 2000, time.ui32Month, time.ui32DayOfMonth, time.ui32Hour, time.ui32Minute, time.ui32Second,
        time.ui32Hundredths);
    return cur_system_time;
}

3:测试输出

图3.1

I/GD25     [20240416 11:14:43.19 PWR] GD25LE128E DMA write addr 0x14c7f7d2, len 1

       log输出类型:I

       log的tag: GD25

        时间戳:20240416 11:14:43.19

        任务名:PWR

        内容:GD25LE128E DMA write addr 0x14c7f7d2, len 1

后续优化图3.2:

图3.2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值