【简介】
Easy logger 是个轻量级的日志库,详细的介绍可以通过GitHub 的链接查看( https://github.com/armink/EasyLogger )。从github 获取的 Easy Logger 的源文件的目录结构还是很清晰的顶层目录的文件如下。


移植适配相对也很容易,移植适配的流程可以参照此链接的说明(https://github.com/armink/EasyLogger/blob/master/docs/zh/port/kernel.md)
按照适配说明文档将源代码加入到工程中编译。

将Easylogger 的头文件路径加入到工程中。

源文件加入工程后按照移植适配的文档适配port 接口,本地使用的no_os 的裸机的方式,按照demo 路径下的 EasyLogger\demo\non_os\stm32f10x\components\easylogger\port\elog_port.c 文件的接口适配Easy Logger 依赖的接口。
对应接口实现如下:
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015, 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: Portable interface for S32K3 platform.
* Created on: 2015-04-28
*/
#include "elog.h"
#include <stdio.h>
#if defined (CPU_S32K324)
#include "S32K324_COMMON.h"
#elif defined (CPU_S32K312)
#include "S32K312_COMMON.h"
#else
#error "Need add S32K3XX_COMMON.h"
#endif
#include "core_cm7.h"
#if defined (CFG_EASY_LOGGER_TERMINAL_UART)
#include "Lpuart_Uart_Ip.h"
#endif
/**
* EasyLogger port initialize
*
* @return result
*/
ElogErrCode elog_port_init(void) {
ElogErrCode result = ELOG_NO_ERR;
return result;
}
/**
* EasyLogger port deinitialize
*
*/
void elog_port_deinit(void) {
/* add your code here */
}
/**
* output log port interface
*
* @param log output of log
* @param size log size
*/
void elog_port_output(const char *log, size_t size) {
#if defined (CFG_EASY_LOGGER_TERMINAL_UART)
for(unsigned int i = 0; i < size; i++)
{
while((CFG_EASY_LOGGER_TERMINAL_UART->STAT & LPUART_STAT_TC_MASK)>>LPUART_STAT_TC_SHIFT==0);
CFG_EASY_LOGGER_TERMINAL_UART->DATA = log[i];
}
#else
/* output to terminal */
printf("%.*s", size, log);
#endif
//TODO output to flash
}
/**
* output lock
*/
void elog_port_output_lock(void) {
__disable_irq();
}
/**
* output unlock
*/
void elog_port_output_unlock(void) {
__enable_irq();
}
/**
* get current time interface
*
* @return current time
*/
const char *elog_port_get_time(void) {
return "10:08:12";
}
/**
* get current process name interface
*
* @return current process name
*/
const char *elog_port_get_p_info(void) {
return "pid:1008";
}
/**
* get current thread name interface
*
* @return current thread name
*/
const char *elog_port_get_t_info(void) {
return "tid:24";
}依赖的接口适配完成后添加如下的配置文件elog_cfg.h
/* * This file is part of the EasyLogger Library. * * Copyright (c) 2015-2016, 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 the configure head file for this library. * Created on: 2015-07-30 */ #ifndef _ELOG_CFG_H_ #define _ELOG_CFG_H_ /*---------------------------------------------------------------------------*/ /* enable log output. default open this macro */ #define ELOG_OUTPUT_ENABLE /* setting static output log level */ #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 16 /* 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 "\r\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_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) /*---------------------------------------------------------------------------*/ /* enable log fmt */ /* comment it if you don't want to output them at all */ #define ELOG_FMT_USING_FUNC #define ELOG_FMT_USING_DIR #define ELOG_FMT_USING_LINE #endif /* _ELOG_CFG_H_ */
适配配置完成后添加如下的测试代码来验证Easy Logger 的功能。
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2017, 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: windows demo.
* Created on: 2015-07-30
*/
#define LOG_TAG "main"
#include <elog.h>
#include <stdio.h>
#include <stdlib.h>
static void test_elog(void);
int main(void) {
/* initialize EasyLogger */
elog_init();
/* set EasyLogger log format */
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_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
/* start EasyLogger */
elog_start();
/* dynamic set enable or disable for output logs (true or false) */
// elog_set_output_enabled(false);
/* dynamic set output logs's level (from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE) */
// elog_set_filter_lvl(ELOG_LVL_WARN);
/* dynamic set output logs's filter for tag */
// elog_set_filter_tag("main");
/* dynamic set output logs's filter for keyword */
// elog_set_filter_kw("Hello");
/* dynamic set output logs's tag filter */
// elog_set_filter_tag_lvl("main", ELOG_LVL_WARN);
/* test logger output */
test_elog();
return EXIT_SUCCESS;
}
/**
* EasyLogger demo
*/
void test_elog(void) {
while(true) {
/* test log output for all level */
log_a("Hello EasyLogger!");
log_e("Hello EasyLogger!");
log_w("Hello EasyLogger!");
log_i("Hello EasyLogger!");
log_d("Hello EasyLogger!");
log_v("Hello EasyLogger!");
}
}上述代码运行后Easy Logger 的输出日志按照配进行输出,运行结果如下:

我要赚赏金
