diff --git a/README.md b/README.md index c61e994..93e0551 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ EasyLogger是一款超轻量级(ROM<1.6K, RAM<0.3k)、高性能的C日志库, 1、RAW格式:未经过格式化的原始日志。 2、标签:在软件中可以按照文件、模块、功能等方面,对需要打印的日志设定标签,实现日志分类。 -## 1.2 支持的插件 +## 1.2 插件 - 1. Flash Log: 使用[EasyFlash](https://github.com/armink/EasyFlash)库提供的无缝接口,可以把日志直接存储在Flash中。 diff --git a/easylogger/inc/elog_flash.h b/easylogger/inc/elog_flash.h index f0e84aa..c43e115 100644 --- a/easylogger/inc/elog_flash.h +++ b/easylogger/inc/elog_flash.h @@ -34,11 +34,13 @@ extern "C" { /* 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" +#define ELOG_FLASH_SW_VERSION "0.06.10" /* elog_flash.c */ ElogErrCode elog_flash_init(void); +void elog_flash_outout(size_t pos, size_t size); void elog_flash_outout_all(void); +void elog_flash_outout_recent(size_t size); 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); diff --git a/easylogger/src/elog_flash.c b/easylogger/src/elog_flash.c index 975d0f4..73d758b 100644 --- a/easylogger/src/elog_flash.c +++ b/easylogger/src/elog_flash.c @@ -66,34 +66,46 @@ ElogErrCode elog_flash_init(void) { } /** - * Read and output all log which saved in flash. @note It will use filter. @see elog_flash_set_filter + * Read and output log which saved in flash. + * + * @param index index for saved log. + * Minimum index is 0. + * Maximum index is log used flash total size - 1. + * @param size */ -void elog_flash_outout_all(void) { +void elog_flash_outout(size_t index, size_t size) { /* 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; + if (index + size > log_total_size) { + log_i("The output position and size is out of bound. The max size is %d.", log_total_size); + return; + } + /* 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); + if (index + read_size + buf_szie < log_total_size) { + flash_log_read(index + 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) { + if ((log_total_size - index - read_size) % 4 == 0) { read_overage_size = 0; } else { - read_overage_size = 4 - ((log_total_size - read_size) % 4); + read_overage_size = 4 - ((log_total_size - index - 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); + flash_log_read(index + read_size - read_overage_size, buf, + log_total_size - index - read_size + read_overage_size); + elog_flash_port_output((const char*) buf + read_overage_size, + log_total_size - index - read_size); //TODO CRLF Ҫͳһͷļ궨 elog_flash_port_output("\r\n", 2); break; @@ -103,6 +115,27 @@ void elog_flash_outout_all(void) { elog_flash_port_unlock(); } +/** + * Read and output all log which saved in flash. + */ +void elog_flash_outout_all(void) { + elog_flash_outout(0, flash_log_get_used_size()); +} + +/** + * Read and output recent log which saved in flash. + * + * @param size recent log size + */ +void elog_flash_outout_recent(size_t size) { + size_t max_size = flash_log_get_used_size(); + if (size > max_size) { + log_i("The output size is out of bound. The max size is %d.", max_size); + } else { + elog_flash_outout(max_size - size, size); + } +} + /** * Write log to flash. The flash write use buffer mode. *