美文网首页
easylogger移植

easylogger移植

作者: xEndLess | 来源:发表于2022-12-28 22:11 被阅读0次

1.找到源码

gitee上找到[easylogger源码](EasyLogger: 一款超轻量级(ROM<1.6K, RAM<0.3k)、高性能的 C/C++ 日志库 (gitee.com))。将源码下载下来。

2.加入工程

将源码复制进自己工程。如下图所示:


目录格式.png

makefile中增加如下内容:

//C_SOURCES 下增加
Middlewares/easylogger/src/elog.c \
Middlewares/easylogger/src/elog_utils.c \
Middlewares/easylogger/port/elog_port.c \

//C_INCLUDES 下增加
-IMiddlewares/easylogger/inc \

3.接口移植

关注Middlewares\easylogger\port\elog_port.c。内容如下:

#include <FreeRTOS.h>
#include <elog.h>
#include "semphr.h"
#include "uart.h"
#include <stdio.h>

static uart_t *logcom;
static SemaphoreHandle_t lock;
/**
 * EasyLogger port initialize
 *
 * @return result
 */
ElogErrCode elog_port_init(void)
{
    ElogErrCode result = ELOG_NO_ERR;
    uint32_t rx_buffer_size = 512;

    lock = xSemaphoreCreateMutex();
    logcom = bsp_uart_open("/UART3/", NULL);
    bsp_uart_ioctl(logcom, ioctlUSE_UART_POLLED_TX, NULL);
    bsp_uart_ioctl(logcom, ioctlUSE_UART_CIRCULAR_BUFFER_IDLE_RX, &rx_buffer_size);
    return result;
}

/**
 * EasyLogger port deinitialize
 *
 */
void elog_port_deinit(void)
{
    vSemaphoreDelete(lock);
}

/**
 * output log port interface
 *
 * @param log output of log
 * @param size log size
 */
void elog_port_output(const char *log, size_t size)
{
    bsp_uart_write(logcom, (const uint8_t *)log, size, 3000);
}

/**
 * output lock
 */
void elog_port_output_lock(void)
{
    xSemaphoreTake(lock, pdMS_TO_TICKS(1000));
}

/**
 * output unlock
 */
void elog_port_output_unlock(void)
{
    xSemaphoreGive(lock);
}

/**
 * get current time interface
 *
 * @return current time
 */
const char *elog_port_get_time(void)
{
    static char cur_system_time[16] = "";
    snprintf(cur_system_time, 16, "%lu", xTaskGetTickCount());
    return cur_system_time;
}

/**
 * get current process name interface
 *
 * @return current process name
 */
const char *elog_port_get_p_info(void)
{
    return "";
}

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

4.基本设置

初始化中调用如下几个函数:

    elog_init();
    /* 设置自己格式 */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_TAG | ELOG_FMT_TIME | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_TAG | ELOG_FMT_TIME | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_TAG | ELOG_FMT_TIME | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_TAG | ELOG_FMT_TIME | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_start();

elog_set_fmt()的相关参数在Middlewares\easylogger\inc\elog.h中,

#define ELOG_LVL_ASSERT                      0
#define ELOG_LVL_ERROR                       1
#define ELOG_LVL_WARN                        2
#define ELOG_LVL_INFO                        3
#define ELOG_LVL_DEBUG                       4
#define ELOG_LVL_VERBOSE                     5

/* all formats index */
typedef enum {
    ELOG_FMT_LVL    = 1 << 0, /**< level */
    ELOG_FMT_TAG    = 1 << 1, /**< tag */
    ELOG_FMT_TIME   = 1 << 2, /**< current time */
    ELOG_FMT_P_INFO = 1 << 3, /**< process info */
    ELOG_FMT_T_INFO = 1 << 4, /**< thread info */
    ELOG_FMT_DIR    = 1 << 5, /**< file directory and name */
    ELOG_FMT_FUNC   = 1 << 6, /**< function name */
    ELOG_FMT_LINE   = 1 << 7, /**< line number */
} ElogFmtIndex;

使用中遇到的问题

输出的日志没有左对齐:

打印格式异常.png

Middlewares\easylogger\inc\elog_cfg.h中修改

/* output newline sign */
// #define ELOG_NEWLINE_SIGN                        "\n"
#define ELOG_NEWLINE_SIGN                        "\r\n"

相关文章

  • Log 库

    C语言 EasyLogger zlog c-log syslog 推荐使用: zlog;大型项目可以考虑封装log...

  • 拼包和移植(包括patchrom )

    拼包移植 拼包方法我归纳为:正向移植、逆向移植和混合移植,其中MTK平台多用逆向移植,高通平台多用正向移植,混合移...

  • Android Recovery 支持 Adb

    很早写过Busybox的移植, 觉得方式比较繁琐 Android Recovery 移植 Busybox 新的移植...

  • 移植

    昨天进行试管移植了,心中有些忐忑不安。不知道会是怎样的一种结果。 很早就在群里询问拼针的事,结果还不...

  • 移植

    医院里有个恐怖传说:太平间的尸体会抢夺活人心脏,想借此复活。 传说源自一个女人和丈夫的吵架,女人一气之下夺门而出,...

  • 移植

    “菲利克斯先生,非常感谢您的再次光顾,上次为您定制的双臂和双腿用得还习惯吗?”克莱尔人体工程集团的金牌销售员史密斯...

  • 移植

    在今年的植树节,全体教职工齐动手,共同在校园内种植了七十三棵树。从种植之日起,我心中便有了新的牵挂,每次回到校园,...

  • 移植

    看到好东西,我就喜欢据为己有。 比如,野外这朵花。 我用小铲子连根把她铲起来,移植到了家里。 朋友说,路边的野花不...

  • 移植

    昨天傍晚,妹妹和我两人合力移植了一棵大葡萄树。它原本是在植物房里的一个巨大塑料盆里,我们把它移植到新建雨棚下,地栽...

  • Freescale Layerscape 嵌入式系统构建笔记

    索引 目标 系统硬件组成 启动流程 环境搭建 u-boot 移植 Kernel移植 文件系统移植 目标 研究Fre...

网友评论

      本文标题:easylogger移植

      本文链接:https://www.haomeiwen.com/subject/sucbqdtx.html