Skip to content

Commit e3917e4

Browse files
committed
set mbed_set_error_hook() to deprecated
1 parent 17e8b9c commit e3917e4

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

platform/mbed_error.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,16 @@ MBED_NORETURN void error(const char *format, ...) MBED_PRINTF(1, 2);
940940
*/
941941
#define MBED_MAKE_ERROR(module, error_code) MBED_MAKE_SYSTEM_ERROR(module, error_code)
942942

943+
/**
944+
* Callback/Error hook function prototype. Applications needing a callback when an error is reported can use mbed_set_error_hook function
945+
* to register a callback/error hook function using the following prototype. When an error happens in the system error handling
946+
* implementation will invoke this callback with the mbed_error_status_t reported and the error context at the time of error.
947+
* @param error_ctx Error context structure associated with this error.
948+
* @return void
949+
*
950+
*/
951+
typedef void (*mbed_error_hook_t)(const mbed_error_ctx *error_ctx);
952+
943953
/**
944954
* Callback/Error hook function. If application implementation needs to receive this callback when an error is reported,
945955
* mbed_error_hook function should be overridden with custom implementation. When an error happens in the system error handling
@@ -950,7 +960,6 @@ MBED_NORETURN void error(const char *format, ...) MBED_PRINTF(1, 2);
950960
*/
951961
void mbed_error_hook(const mbed_error_ctx *error_context);
952962

953-
954963
/**
955964
* Callback function for reporting error context during boot up. When MbedOS error handling system detects a fatal error
956965
* it will auto-reboot the system(if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED is enabled) after capturing the
@@ -1070,6 +1079,30 @@ bool mbed_get_error_in_progress(void);
10701079
*/
10711080
MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number);
10721081

1082+
/**
1083+
* Registers an application defined error callback with the error handling system.
1084+
* This function will be called with error context info whenever system handles a mbed_error/mbed_warning call
1085+
* NOTE: This function should be implemented for re-entrancy as multiple threads may invoke mbed_error which may cause error hook to be called.
1086+
* @param custom_error_hook mbed_error_status_t status to be set(See mbed_error_status_t enum above for available error status values).
1087+
* @return 0 or MBED_SUCCESS on success.
1088+
* MBED_ERROR_INVALID_ARGUMENT in case of NULL for custom_error_hook
1089+
*
1090+
* @code
1091+
*
1092+
* mbed_error_status_t my_custom_error_hook(mbed_error_status_t error_status, const mbed_error_ctx *error_ctx) {
1093+
* //Do something with the error_status or error_ctx
1094+
* }
1095+
*
1096+
* mbed_set_error_hook( my_custom_error_hook )
1097+
*
1098+
* @endcode
1099+
* @note The erro hook function implementation should be re-entrant.
1100+
*
1101+
* @deprecated You should use an overridden mbed_error_hook() function if you like to catch errors in your application.
1102+
* With mbed_set_error_hook() it is not possible to catch errors before your application started.
1103+
*/
1104+
mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t custom_error_hook);
1105+
10731106
/**
10741107
* Reads the first error context information captured.
10751108
* @param error_info This is the mbed_error_context info captured as part of the first mbed_error call. The caller should pass a pointer to mbed_error_context struct allocated by the caller.

platform/source/mbed_error.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static int error_count = 0;
5858
static mbed_error_ctx first_error_ctx = {0};
5959

6060
static mbed_error_ctx last_error_ctx = {0};
61+
static mbed_error_hook_t error_hook = NULL;
6162
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller);
6263

6364
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
@@ -193,8 +194,12 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
193194
mbed_error_hist_put(&current_error_ctx);
194195
#endif
195196

196-
//Call the error hook
197-
mbed_error_hook(&last_error_ctx);
197+
//Call the error hook if available
198+
if (error_hook != NULL) {
199+
error_hook(&last_error_ctx);
200+
} else {
201+
mbed_error_hook(&last_error_ctx);
202+
}
198203

199204
core_util_critical_section_exit();
200205

@@ -324,6 +329,19 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
324329
mbed_halt_system();
325330
}
326331

332+
//Register an application defined callback with error handling
333+
MBED_DEPRECATED("Use an overridden mbed_error_hook() function")
334+
mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t error_hook_in)
335+
{
336+
//register the new hook/callback
337+
if (error_hook_in != NULL) {
338+
error_hook = error_hook_in;
339+
return MBED_SUCCESS;
340+
}
341+
342+
return MBED_ERROR_INVALID_ARGUMENT;
343+
}
344+
327345
//Reset the reboot error context
328346
mbed_error_status_t mbed_reset_reboot_error_info()
329347
{

0 commit comments

Comments
 (0)