【简介】
之前分别在裸机(【EasyLogger】裸机环境适配)环境和FreeRTOS (【EasyLogger】适配 FreeRTOS 环境)环境下适配了EasyLogger模块。我们在FreeRTOS环境的基础上继续使用异步的方式输出loger,异步的方式需要创建一个task 来管理输出log。
异步的log 接口需要将该文件加入到工程中编译,并实现 elog_async_output_notice 函数通知log 管理task 来输出log信息。

将上述文件加入工程编译后同时要更新配置项,开启异步log输出模式,本地配置如下

在port init 接口中创建信号量和log 输出task.
static SemaphoreHandle_t output_lock;
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
static SemaphoreHandle_t output_notice;
static void async_output(void *arg);
#endif
/**
* EasyLogger port initialize
*
* @return result
*/
ElogErrCode elog_port_init(void) {
ElogErrCode result = ELOG_NO_ERR;
output_lock = xSemaphoreCreateMutex();
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
output_notice = xSemaphoreCreateCounting(1, 1);
xTaskCreate(async_output, "elog_async", 512, NULL, tskIDLE_PRIORITY + 1, NULL);
#endif
return result;
}添加 elog_async_output_notice 接口和async_out task
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
void elog_async_output_notice(void) {
xSemaphoreGive(output_notice);
}
static void async_output(void *arg) {
(void)arg;
size_t get_log_size = 0;
#ifdef ELOG_ASYNC_LINE_OUTPUT
static char poll_get_buf[ELOG_LINE_BUF_SIZE - 4];
#else
static char poll_get_buf[ELOG_ASYNC_OUTPUT_BUF_SIZE - 4];
#endif
for(;;)
{
/* waiting log */
xSemaphoreTake(output_notice, portMAX_DELAY);
/* polling gets and outputs the log */
while (1) {
#ifdef ELOG_ASYNC_LINE_OUTPUT
get_log_size = elog_async_get_line_log(poll_get_buf, sizeof(poll_get_buf));
#else
get_log_size = elog_async_get_log(poll_get_buf, sizeof(poll_get_buf));
#endif
if (get_log_size) {
elog_port_output(poll_get_buf, get_log_size);
} else {
break;
}
}
}
}
#endif
上述修改完成后运行测试程序 log 输出已经切换为异步模式输出。

我要赚赏金
