1、【增加】flash log插件,实现日志的flash存储功能。
Signed-off-by: armink <armink.ztl@gmail.com>pull/3/head
parent
58ef4d7be0
commit
b88104085a
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the EasyLogger Library.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Function: Is is an head file for flash log plugin. You can see all be called functions.
|
||||||
|
* Created on: 2015-06-05
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __ELOG_FLASH_H__
|
||||||
|
#define __ELOG_FLASH_H__
|
||||||
|
|
||||||
|
#include "elog.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* EasyLogger flash save plugin's using buffer mode */
|
||||||
|
#define ELOG_FLASH_USING_BUF_MODE
|
||||||
|
/* EasyLogger flash save plugin's RAM buffer size */
|
||||||
|
#define ELOG_FLASH_BUF_SIZE 1024
|
||||||
|
/* EasyLogger flash save plugin's software version number */
|
||||||
|
#define ELOG_FLASH_SW_VERSION "0.06.09"
|
||||||
|
|
||||||
|
/* elog_flash.c */
|
||||||
|
ElogErrCode elog_flash_init(void);
|
||||||
|
void elog_flash_outout_all(void);
|
||||||
|
void elog_flash_set_filter(uint8_t level,const char *tag,const char *keyword);
|
||||||
|
void elog_flash_write(const char *log, size_t size);
|
||||||
|
void elog_flash_clean(void);
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
void elog_flash_flush(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* elog_port.c */
|
||||||
|
void elog_flash_port_output(const char *output, size_t size);
|
||||||
|
void elog_flash_port_lock(void);
|
||||||
|
void elog_flash_port_unlock(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __ELOG_FLASH_H__ */
|
||||||
@ -0,0 +1,217 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the EasyLogger Library.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Function: Save log to flash. Must use EasyFlash(https://github.com/armink/EasyFlash) library.
|
||||||
|
* Created on: 2015-06-05
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "elog_flash.h"
|
||||||
|
#include "flash.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define log_a(...) elog_a("elog.flash", __VA_ARGS__)
|
||||||
|
#define log_e(...) elog_e("elog.flash", __VA_ARGS__)
|
||||||
|
#define log_w(...) elog_w("elog.flash", __VA_ARGS__)
|
||||||
|
#define log_i(...) elog_i("elog.flash", __VA_ARGS__)
|
||||||
|
#define log_d(...) elog_d("elog.flash", __VA_ARGS__)
|
||||||
|
#define log_v(...) elog_v("elog.flash", __VA_ARGS__)
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
/* flash log buffer */
|
||||||
|
static char log_buf[ELOG_FLASH_BUF_SIZE] = { 0 };
|
||||||
|
/* current flash log buffer write position */
|
||||||
|
static size_t cur_buf_size = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* initialize OK flag */
|
||||||
|
static bool init_ok = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EasyLogger flash save plugin initialize.
|
||||||
|
*
|
||||||
|
* @return result
|
||||||
|
*/
|
||||||
|
ElogErrCode elog_flash_init(void) {
|
||||||
|
ElogErrCode result = ELOG_NO_ERR;
|
||||||
|
|
||||||
|
/* buffer size must be word alignment */
|
||||||
|
ELOG_ASSERT(ELOG_FLASH_BUF_SIZE % 4 == 0)
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
/* initialize current flash log buffer write position */
|
||||||
|
cur_buf_size = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* initialize OK */
|
||||||
|
init_ok = true;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read and output all log which saved in flash. @note It will use filter. @see elog_flash_set_filter
|
||||||
|
*/
|
||||||
|
void elog_flash_outout_all(void) {
|
||||||
|
/* 128 bytes buffer */
|
||||||
|
uint32_t buf[32] = { 0 };
|
||||||
|
size_t log_total_size = flash_log_get_used_size();
|
||||||
|
size_t buf_szie = sizeof(buf);
|
||||||
|
size_t read_size = 0, read_overage_size = 0;
|
||||||
|
|
||||||
|
/* must be call this function after initialize OK */
|
||||||
|
ELOG_ASSERT(init_ok);
|
||||||
|
/* lock flash log buffer */
|
||||||
|
elog_flash_port_lock();
|
||||||
|
/* Output all flash saved log. It will use filter */
|
||||||
|
while (true) {
|
||||||
|
if (read_size + buf_szie < log_total_size) {
|
||||||
|
flash_log_read(read_size, buf, buf_szie);
|
||||||
|
elog_flash_port_output((const char*)buf, buf_szie);
|
||||||
|
read_size += buf_szie;
|
||||||
|
} else {
|
||||||
|
/* flash read is word alignment */
|
||||||
|
if ((log_total_size - read_size) % 4 == 0) {
|
||||||
|
read_overage_size = 0;
|
||||||
|
} else {
|
||||||
|
read_overage_size = 4 - ((log_total_size - read_size) % 4);
|
||||||
|
}
|
||||||
|
flash_log_read(read_size, buf, log_total_size - read_size + read_overage_size);
|
||||||
|
elog_flash_port_output((const char*)buf, log_total_size - read_size);
|
||||||
|
//TODO CRLF 后期需要统一在头文件宏定义
|
||||||
|
elog_flash_port_output("\r\n", 2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* unlock flash log buffer */
|
||||||
|
elog_flash_port_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write log to flash. The flash write use buffer mode.
|
||||||
|
*
|
||||||
|
* @param log log
|
||||||
|
* @param size log size
|
||||||
|
*/
|
||||||
|
void elog_flash_write(const char *log, size_t size) {
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
size_t write_size = 0, write_index = 0;
|
||||||
|
#else
|
||||||
|
size_t write_size_temp = 0;
|
||||||
|
FlashErrCode result = FLASH_NO_ERR;
|
||||||
|
/* write some '\r' for word alignment */
|
||||||
|
char write_overage_c[4] = { '\r', '\r', '\r', '\r' };
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* must be call this function after initialize OK */
|
||||||
|
ELOG_ASSERT(init_ok);
|
||||||
|
|
||||||
|
/* lock flash log buffer */
|
||||||
|
elog_flash_port_lock();
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
while (true) {
|
||||||
|
if (cur_buf_size + size > ELOG_FLASH_BUF_SIZE) {
|
||||||
|
write_size = ELOG_FLASH_BUF_SIZE - cur_buf_size;
|
||||||
|
memcpy(log_buf + cur_buf_size, log + write_index, write_size);
|
||||||
|
write_index += write_size;
|
||||||
|
size -= write_size;
|
||||||
|
cur_buf_size += write_size;
|
||||||
|
/* unlock flash log buffer */
|
||||||
|
elog_flash_port_unlock();
|
||||||
|
/* write all buffered log to flash, cur_buf_size will reset */
|
||||||
|
elog_flash_flush();
|
||||||
|
/* lock flash log buffer */
|
||||||
|
elog_flash_port_lock();
|
||||||
|
} else {
|
||||||
|
memcpy(log_buf + cur_buf_size, log + write_index, size);
|
||||||
|
cur_buf_size += size;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
/* calculate the word alignment write size */
|
||||||
|
write_size_temp = size / 4 * 4;
|
||||||
|
/* write log to flash */
|
||||||
|
result = flash_log_write((uint32_t *) log, write_size_temp);
|
||||||
|
/* write last word alignment data */
|
||||||
|
if ((result == FLASH_NO_ERR) && (write_size_temp != size)) {
|
||||||
|
memcpy(write_overage_c, log + write_size_temp, size - write_size_temp);
|
||||||
|
flash_log_write((uint32_t *) write_overage_c, 4);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* unlock flash log buffer */
|
||||||
|
elog_flash_port_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
/**
|
||||||
|
* write all buffered log to flash
|
||||||
|
*/
|
||||||
|
void elog_flash_flush(void) {
|
||||||
|
size_t write_overage_size = 0;
|
||||||
|
|
||||||
|
/* must be call this function after initialize OK */
|
||||||
|
ELOG_ASSERT(init_ok);
|
||||||
|
/* lock flash log buffer */
|
||||||
|
elog_flash_port_lock();
|
||||||
|
/* flash write is word alignment */
|
||||||
|
if (cur_buf_size % 4 != 0) {
|
||||||
|
write_overage_size = 4 - (cur_buf_size % 4);
|
||||||
|
}
|
||||||
|
/* fill '\r' for word alignment */
|
||||||
|
memset(log_buf + cur_buf_size, '\r', write_overage_size);
|
||||||
|
/* write all buffered log to flash */
|
||||||
|
flash_log_write((uint32_t *) log_buf, cur_buf_size + write_overage_size);
|
||||||
|
/* reset position */
|
||||||
|
cur_buf_size = 0;
|
||||||
|
/* unlock flash log buffer */
|
||||||
|
elog_flash_port_unlock();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clean all log which in flash and ram buffer
|
||||||
|
*/
|
||||||
|
void elog_flash_clean(void) {
|
||||||
|
FlashErrCode clean_result = FLASH_NO_ERR;
|
||||||
|
|
||||||
|
/* must be call this function after initialize OK */
|
||||||
|
ELOG_ASSERT(init_ok);
|
||||||
|
/* lock flash log buffer */
|
||||||
|
elog_flash_port_lock();
|
||||||
|
/* clean all log which in flash */
|
||||||
|
clean_result = flash_log_clean();
|
||||||
|
|
||||||
|
#ifdef ELOG_FLASH_USING_BUF_MODE
|
||||||
|
/* reset position */
|
||||||
|
cur_buf_size = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* unlock flash log buffer */
|
||||||
|
elog_flash_port_unlock();
|
||||||
|
|
||||||
|
if(clean_result == FLASH_NO_ERR) {
|
||||||
|
log_i("All logs which in flash is clean OK.");
|
||||||
|
} else {
|
||||||
|
log_e("Clean logs which in flash has an error!");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue