diff --git a/demo/os/rt-thread/stm32f10x/README.md b/demo/os/rt-thread/stm32f10x/README.md index f0909e1..5e2d827 100644 --- a/demo/os/rt-thread/stm32f10x/README.md +++ b/demo/os/rt-thread/stm32f10x/README.md @@ -25,4 +25,5 @@ ## 3、其他功能 -- 1、新增 RTT断言及硬件异常的钩子的方法,使得系统在出现异常时,错误日志依然可以被输出或保存。参考 `app\src\app_task.c` 中的 `assert_hook` 及 `exception_hook` 方法。 \ No newline at end of file +- 1、新增 RTT断言及硬件异常的钩子方法,使得系统在出现异常时,错误日志依然可以被输出或保存。参考 `app\src\app_task.c` 中的 `rtt_user_assert_hook` 及 `exception_hook` 方法。 +- 2、新增 EasyLogger断言的钩子方法,使得系统在出现异常时,错误日志依然可以被输出或保存。参考 `app\src\app_task.c` 中的 `elog_user_assert_hook` 方法。 \ No newline at end of file diff --git a/demo/os/rt-thread/stm32f10x/app/src/app_task.c b/demo/os/rt-thread/stm32f10x/app/src/app_task.c index ca16dd2..ad6251a 100644 --- a/demo/os/rt-thread/stm32f10x/app/src/app_task.c +++ b/demo/os/rt-thread/stm32f10x/app/src/app_task.c @@ -32,7 +32,8 @@ static rt_uint8_t thread_sys_monitor_stack[512]; struct rt_thread thread_sys_monitor; static void test_elog(void); -static void assert_hook(const char* ex, const char* func, rt_size_t line); +static void rtt_user_assert_hook(const char* ex, const char* func, rt_size_t line); +static void elog_user_assert_hook(const char* ex, const char* func, size_t line); static rt_err_t exception_hook(void *context); /** @@ -92,10 +93,12 @@ void sys_init_thread(void* parameter){ /* set enabled format */ elog_set_fmt(ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME /*| ELOG_FMT_P_INFO*/ | ELOG_FMT_T_INFO | ELOG_FMT_DIR /*| ELOG_FMT_FUNC*/ | ELOG_FMT_LINE); + /* set EasyLogger assert hook */ + elog_assert_set_hook(elog_user_assert_hook); /* set hardware exception hook */ rt_hw_exception_install(exception_hook); /* set RT-Thread assert hook */ - rt_assert_set_hook(assert_hook); + rt_assert_set_hook(rtt_user_assert_hook); /* initialize OK and switch to running status */ set_system_status(SYSTEM_STATUS_RUN); } else { @@ -106,11 +109,19 @@ void sys_init_thread(void* parameter){ rt_thread_delete(rt_thread_self()); } -static void assert_hook(const char* ex, const char* func, rt_size_t line) { +static void elog_user_assert_hook(const char* ex, const char* func, size_t line) { elog_output_lock_enabled(false); - //elog_flash_lock_enabled(false); - elog_a("assert", "(%s) has assert failed at %s:%ld.\n", ex, func, line); - //elog_flash_flush(); +// elog_flash_lock_enabled(false); + elog_a("elog", "(%s) has assert failed at %s:%ld.", ex, func, line); +// elog_flash_flush(); + while(1); +} + +static void rtt_user_assert_hook(const char* ex, const char* func, rt_size_t line) { + elog_output_lock_enabled(false); +// elog_flash_lock_enabled(false); + elog_a("rtt", "(%s) has assert failed at %s:%ld.\n", ex, func, line); +// elog_flash_flush(); while(1); } diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index ea4256f..f8b6e44 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -61,14 +61,18 @@ extern "C" { /* output newline sign */ #define ELOG_NEWLINE_SIGN "\r\n" /* EasyLogger software version number */ -#define ELOG_SW_VERSION "0.06.25" +#define ELOG_SW_VERSION "0.06.27" /* EasyLogger assert for developer. */ -#define ELOG_ASSERT(EXPR) \ -if (!(EXPR)) \ -{ \ - elog_a("elog", "(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \ - while (1); \ +#define ELOG_ASSERT(EXPR) \ +if (!(EXPR)) \ +{ \ + if (elog_assert_hook == NULL) { \ + elog_a("elog", "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \ + while (1); \ + } else { \ + elog_assert_hook(#EXPR, __FUNCTION__, __LINE__); \ + } \ } /* all formats index */ @@ -116,6 +120,8 @@ void elog_raw(const char *format, ...); void elog_output(uint8_t level, const char *tag, const char *file, const char *func, const long line, const char *format, ...); void elog_output_lock_enabled(bool enabled); +extern void (*elog_assert_hook)(const char* expr, const char* func, size_t line); +void elog_assert_set_hook(void (*hook)(const char* expr, const char* func, size_t line)); #ifndef ELOG_OUTPUT_ENABLE diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 811260e..2347357 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -56,6 +56,9 @@ static void output_lock(void); static void output_unlock(void); static bool get_fmt_enabled(size_t set); +/* EasyLogger assert hook */ +void (*elog_assert_hook)(const char* expr, const char* func, size_t line); + /** * EasyLogger initialize. * @@ -393,3 +396,12 @@ static void output_unlock(void) { output_is_locked_before_enable = false; } } + +/** + * Set a hook function to EasyLogger assert. It will run when the expression is false. + * + * @param hook the hook function + */ +void elog_assert_set_hook(void (*hook)(const char* expr, const char* func, size_t line)) { + elog_assert_hook = hook; +}