diff --git a/RBcore/msg_center.c b/RBcore/msg_center.c index 9bd0efd..8bc3409 100644 --- a/RBcore/msg_center.c +++ b/RBcore/msg_center.c @@ -207,10 +207,9 @@ int MsgCenter_Send(const Msg_t *pstMsg) { // 发送信号量,通知有新消息 Rd_SemPost(&g_astModules[iDstIndex].stSem, __func__, NULL); - log_v("[%p] send success ID:%x to %d", __builtin_return_address(0), pstMsg->m_uiMsgID, pstMsg->m_uiDstModule); return RD_SUCCESS; } - + log_w("warning queue full send ID:%x to %d", pstMsg->m_uiMsgID, pstMsg->m_uiDstModule); return RD_FAILURE; // 队列满 } @@ -253,6 +252,7 @@ int MsgCenter_SendTo(const char *pszDstName, uint32_t uiMsgID, const void *pData stMsg.m_uiDataLen = uiDataLen; } + log_v("[%p] send success ID:%x to %d", __builtin_return_address(0), stMsg.m_uiMsgID, stMsg.m_uiDstModule); return MsgCenter_Send(&stMsg); } diff --git a/library/common/include/elog.h b/library/common/include/elog.h index f280f8c..e6b6a92 100644 --- a/library/common/include/elog.h +++ b/library/common/include/elog.h @@ -56,7 +56,7 @@ extern "C" { #define ELOG_LVL_TOTAL_NUM 6 /* EasyLogger software version number */ -#define ELOG_SW_VERSION "2.2.99" +#define ELOG_SW_VERSION "2.3.0" /* EasyLogger assert for developer. */ #ifdef ELOG_ASSERT_ENABLE @@ -213,6 +213,8 @@ void elog_set_filter_lvl(uint8_t level); void elog_set_filter_tag(const char *tag); void elog_set_filter_kw(const char *keyword); void elog_set_filter_tag_lvl(const char *tag, uint8_t level); +void elog_set_console_level(uint8_t level); +void elog_set_file_level(uint8_t level); uint8_t elog_get_filter_tag_lvl(const char *tag); void elog_raw_output(const char *format, ...); void elog_output(uint8_t level, const char *tag, const char *file, const char *func, diff --git a/library/common/include/elog_cfg.h b/library/common/include/elog_cfg.h index eb2006c..ee2e169 100644 --- a/library/common/include/elog_cfg.h +++ b/library/common/include/elog_cfg.h @@ -33,7 +33,7 @@ /* enable log output. */ #define ELOG_OUTPUT_ENABLE /* setting static output log level. range: from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE */ -#define ELOG_OUTPUT_LVL ELOG_LVL_INFO +#define ELOG_OUTPUT_LVL ELOG_LVL_VERBOSE /* enable assert check */ // #define ELOG_ASSERT_ENABLE /* buffer size for every line's log */ @@ -68,7 +68,7 @@ /* enable asynchronous output mode */ #define ELOG_ASYNC_OUTPUT_ENABLE /* the highest output level for async mode, other level will sync output */ -#define ELOG_ASYNC_OUTPUT_LVL ELOG_LVL_INFO +#define ELOG_ASYNC_OUTPUT_LVL ELOG_LVL_VERBOSE /* buffer size for asynchronous output mode */ #define ELOG_ASYNC_OUTPUT_BUF_SIZE (ELOG_LINE_BUF_SIZE * 10) /* each asynchronous output's log which must end with newline sign */ diff --git a/library/common/include/rd_thread.h b/library/common/include/rd_thread.h index 5cc59c4..066555f 100644 --- a/library/common/include/rd_thread.h +++ b/library/common/include/rd_thread.h @@ -72,7 +72,7 @@ extern "C"{ /* FreeRTOS: opaque handle, 不依赖 FreeRTOS 头文件 */ typedef void* rd_mutex_t; #define RD_MUTEX_INITIALIZER NULL -#elif defined(BUILD_PLATFORM_LINUX) +#elif defined(BUILD_PLATFORM_LINUX) || defined(__ANDROID__) || defined(BUILD_PLATFORM_WIN32) typedef pthread_mutex_t rd_mutex_t; #define RD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER #else diff --git a/library/common/rd_mempool.c b/library/common/rd_mempool.c index cdfde5c..0a885f9 100644 --- a/library/common/rd_mempool.c +++ b/library/common/rd_mempool.c @@ -217,7 +217,7 @@ WEAK void Rd_MemDoInit(void) (void)Rd_MutexInit(&g_pool_mutex); #endif - log_e("MemPool Init Success: Total %d Bytes", MEM_POOL_TOTAL_SIZE); + log_i("MemPool Init Success: Total %d Bytes", MEM_POOL_TOTAL_SIZE); } WEAK void Rd_MemDoDeinit(void) @@ -296,7 +296,7 @@ WEAK void* Rd_MemPoolMalloc(size_t size) } if (pvReturn == NULL) { - log_e("MemPool Malloc Failed: Request %zu, No Block Found", size); + log_e("MemPool Malloc Failed: Request %d, No Block Found", size); } } #ifdef USE_FREERTOS diff --git a/library/common/rd_stdio.c b/library/common/rd_stdio.c index e52c610..65a83a3 100644 --- a/library/common/rd_stdio.c +++ b/library/common/rd_stdio.c @@ -19,6 +19,7 @@ #include "rd_stdio.h" #if LV_USE_FS_LITTLEFS #include "../../lvgl/lvgl.h" +#include "../littlefs/include/lfs.h" #endif /*----------------------------------------------* * 外部变量说明 * @@ -64,6 +65,7 @@ WEAK void *Rd_Fopen(const char *path, const char *mode) #if LV_USE_FS_LITTLEFS lv_fs_mode_t lv_mode; if (strchr(mode, 'r') != NULL) lv_mode = LV_FS_MODE_RD; + else if (strchr(mode, 'a') != NULL) lv_mode = LV_FS_MODE_RD | LV_FS_MODE_WR; else lv_mode = LV_FS_MODE_WR; lv_fs_file_t *file = RD_MALLOC(sizeof(lv_fs_file_t)); @@ -83,19 +85,14 @@ WEAK void *Rd_Fopen(const char *path, const char *mode) lfs_file_t *file = RD_MALLOC(sizeof(lfs_file_t)); int flags = 0; if (strlen(path) >= 2 && path[1] == ':') p = path + 2; - /* 按 mode 首字符解析:r 只读 / w 写并截断 / a 追加; - 带 '+' 则为读写。避免 "a+" 被误判为 TRUNC 清空文件 */ - if (mode[0] == 'r') { + if (strchr(mode, 'r') != NULL) { flags = LFS_O_RDONLY; - if (strchr(mode, '+')) flags = LFS_O_RDWR; - } else if (mode[0] == 'w') { + } else if (strchr(mode, 'w') != NULL) { flags = LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC; - if (strchr(mode, '+')) flags = LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC; - } else if (mode[0] == 'a') { - flags = LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND; - if (strchr(mode, '+')) flags = LFS_O_RDWR | LFS_O_CREAT | LFS_O_APPEND; + } else if (strchr(mode, 'a') != NULL) { + flags = LFS_O_RDWR | LFS_O_CREAT | LFS_O_APPEND; } else { - flags = LFS_O_RDONLY; /* 未知 mode 默认只读 */ + flags = LFS_O_RDONLY; } int err = lfs_file_open(&g_lfs, file, p, flags); if(err) return NULL; @@ -307,7 +304,15 @@ WEAK int Rd_fflush(void *file) { #ifdef USE_W25QXX #if LV_USE_FS_LITTLEFS - (void)file; + if (file == NULL) return RD_FAILURE; + lv_fs_file_t *fp = (lv_fs_file_t *)file; + lfs_t *lfs = fp->drv->user_data; + int err = lfs_file_sync(lfs, fp->file_d); + if (err < 0) + { + printf("lfs_file_sync failed: %d\n", err); + return RD_FAILURE; + } return RD_SUCCESS; #else if (file == NULL) return RD_FAILURE; @@ -355,8 +360,12 @@ WEAK int Rd_rename(const char *oldpath, const char *newpath) int Rd_Lfs_Mount(void) { +#ifdef USE_W25QXX lfs_format(&g_lfs, &g_cfg); return lfs_mount(&g_lfs, &g_cfg); +#else + return 0; +#endif } WEAK void Rd_LittleFSInit(void) diff --git a/library/common/rd_time.c b/library/common/rd_time.c index 200e25a..2c0c385 100644 --- a/library/common/rd_time.c +++ b/library/common/rd_time.c @@ -49,21 +49,20 @@ WEAK unsigned int g_uiRandSeed = 12345; #if !WIN32 && !defined(__ANDROID__) WEAK time_t rd_time(time_t *tloc) { - if (tloc != NULL) - { - *tloc = (time_t)-1; - } - return (time_t)-1; // 表示时间功能不可用 + uint32_t HAL_GetTick(void); + time_t now = (time_t)(HAL_GetTick() / 1000); + if (tloc != NULL) *tloc = now; + return now; } WEAK struct tm *rd_localtime_r(const time_t *timep, struct tm *result) { - return NULL; + return localtime_r(timep, result); } WEAK size_t rd_strftime(char *s, size_t max, const char *format, const struct tm *tm) { - return (size_t)-1; + return strftime(s, max, format, tm); } WEAK void rd_srand(unsigned int seed) diff --git a/library/log/elog_port.c b/library/log/elog_port.c index a977406..41e570e 100644 --- a/library/log/elog_port.c +++ b/library/log/elog_port.c @@ -39,6 +39,35 @@ static pthread_mutex_t output_lock; +static uint8_t s_console_level = ELOG_FILTER_LVL_ALL; +static uint8_t s_file_level = ELOG_FILTER_LVL_ALL; + + +static uint8_t parse_log_level(const char *log) +{ + for (const char *p = log; *p; p++) { + if (p[1] == '/') { + switch (*p) { + case 'A': return ELOG_LVL_ASSERT; + case 'E': return ELOG_LVL_ERROR; + case 'W': return ELOG_LVL_WARN; + case 'I': return ELOG_LVL_INFO; + case 'D': return ELOG_LVL_DEBUG; + case 'V': return ELOG_LVL_VERBOSE; + } + } + } + return ELOG_LVL_VERBOSE; +} + +void elog_set_console_level(uint8_t level) { + s_console_level = level; +} + +void elog_set_file_level(uint8_t level) { + s_file_level = level; +} + /** * EasyLogger port initialize * @@ -67,7 +96,14 @@ WEAK void elog_port_deinit(void) { * @param size log size */ WEAK void elog_port_output(const char *log, size_t size) { - elog_file_write(log, size); + uint8_t level = parse_log_level(log); + if (level <= s_console_level) { + RD_PRINTF("%.*s", (int)size, log); + } + + if (level <= s_file_level) { + elog_file_write(log, size); + } } /** diff --git a/library/log/include/elog_file_cfg.h b/library/log/include/elog_file_cfg.h index 53604b5..99a0c16 100644 --- a/library/log/include/elog_file_cfg.h +++ b/library/log/include/elog_file_cfg.h @@ -39,4 +39,6 @@ /* EasyLogger file log plugin's using max rotate file count */ #define ELOG_FILE_MAX_ROTATE 5 +#define ELOG_FILE_FLUSH_CACHE_ENABLE + #endif /* _ELOG_FILE_CFG_H_ */ diff --git a/library/log/log.c b/library/log/log.c index acdfec3..adeef34 100644 --- a/library/log/log.c +++ b/library/log/log.c @@ -1,10 +1,8 @@ #include #include "../common/include/elog.h" -void Rd_ElogInit(void) -{ - /* close printf buffer */ - //setbuf(stdout, NULL); +void Rd_ElogInit(void) +{ /* initialize EasyLogger */ elog_init(); /* set EasyLogger log format */ @@ -14,6 +12,10 @@ void Rd_ElogInit(void) 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); + + elog_set_console_level(ELOG_LVL_WARN); + elog_set_file_level(ELOG_LVL_INFO); + /* start EasyLogger */ elog_start(); } \ No newline at end of file diff --git a/lua/lua.c b/lua/lua.c index 82bbfc3..039e13f 100644 --- a/lua/lua.c +++ b/lua/lua.c @@ -527,6 +527,55 @@ static int l_ls(lua_State *L) return 0; } +#ifdef BUILD_LOG +static int l_log_lvl(lua_State *L) +{ + uint32_t uiLevel = (uint32_t)luaL_checkinteger(L, 1); + + if (uiLevel >= 0 && uiLevel <= 5) + { + elog_set_console_level((uint8_t)uiLevel); + } + + return 0; +} + +static int l_log_tag(lua_State *L) +{ + const char *psTag = luaL_checkstring(L, 1); + + if (psTag != NULL) + { + elog_set_filter_tag(psTag); + } + + return 0; +} + +static int l_tag_lvl(lua_State *L) +{ + const char *psTag = luaL_checkstring(L, 1); + uint32_t uiLevel = (uint32_t)luaL_checkinteger(L, 2); + + if (psTag != NULL && uiLevel >= 0 && uiLevel <= 5) + { + elog_set_filter_tag_lvl(psTag, uiLevel); + } + + return 0; +} + +static int l_log(lua_State *L) +{ + log_e("This is error !"); + log_w("This is wranning !"); + log_i("This is info !"); + log_d("This is debug !"); + log_v("This is all !"); + return 0; +} +#endif + static int luaopen_default(lua_State *L) { lua_register_help(L, "help", l_help, "Show this help message"); @@ -536,6 +585,11 @@ static int luaopen_default(lua_State *L) lua_register_help(L, "mv", l_mv, "eg: mv(\"C:a.txt\", \"C:b.txt\")"); lua_register_help(L, "rm", l_rm, "eg: rm(\"C:a.txt\")"); lua_register_help(L, "ls", l_ls, "eg: ls()"); +#ifdef BUILD_LOG + lua_register_help(L, "log_lvl", l_log_lvl, "log_lvl(3)"); + lua_register_help(L, "log_tag", l_log_tag, "log_tag(\"NO_TAG\")"); + lua_register_help(L, "tag_lvl", l_tag_lvl, "tag_lvl(\"NO_TAG\", 5)"); +#endif return 0; }