rcutils
master
C API providing common utilities and data structures.
|
Go to the documentation of this file.
17 #ifndef RCUTILS__ERROR_HANDLING_H_
18 #define RCUTILS__ERROR_HANDLING_H_
25 #ifndef __STDC_WANT_LIB_EXT1__
26 #define __STDC_WANT_LIB_EXT1__ 1 // indicate we would like strnlen_s if available
43 #ifdef __STDC_LIB_EXT1__
49 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
50 do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
52 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
54 do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
66 #define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) \
68 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
69 int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \
71 RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \
73 RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \
78 #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615"
81 #define RCUTILS_ERROR_FORMATTING_CHARACTERS 6 // ', at ' + ':'
84 #define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1024
91 #define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 768
97 #define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH ( \
98 RCUTILS_ERROR_MESSAGE_MAX_LENGTH - \
99 RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH - \
100 RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH - \
101 RCUTILS_ERROR_FORMATTING_CHARACTERS - \
124 #if __STDC_VERSION__ >= 201112L
132 "Maximum length calculations incorrect");
204 #define RCUTILS_CHECK_ARGUMENT_FOR_NULL(argument, error_return_type) \
205 RCUTILS_CHECK_FOR_NULL_WITH_MSG( \
206 argument, #argument " argument is null", \
207 return error_return_type)
218 #define RCUTILS_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement) \
220 if (NULL == value) { \
221 RCUTILS_SET_ERROR_MSG(msg); \
236 #define RCUTILS_SET_ERROR_MSG(msg) \
237 do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
248 #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
250 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
251 int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \
253 RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \
255 RCUTILS_SET_ERROR_MSG(output_msg); \
270 #define RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(error_return_value) \
271 RCUTILS_CAN_FAIL_WITH( \
273 RCUTILS_SET_ERROR_MSG("Injecting " RCUTILS_STRINGIFY(error_return_value)); \
274 return error_return_value; \
321 #endif // RCUTILS__ERROR_HANDLING_H_
bool rcutils_error_is_set(void)
Return true if the error is set, otherwise false.
struct rcutils_error_state_t rcutils_error_state_t
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
const rcutils_error_state_t * rcutils_get_error_state(void)
Return an rcutils_error_state_t which was set with rcutils_set_error_state().
#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH
The maximum length a formatted number is allowed to have.
Definition: error_handling.h:78
#define RCUTILS_ERROR_FORMATTING_CHARACTERS
The maximum number of formatting characters allowed.
Definition: error_handling.h:81
Struct wrapping a fixed-size c string used for returning the formatted error string.
Definition: error_handling.h:105
rcutils_error_string_t rcutils_get_error_string(void)
Return the error message followed by , at <file>:<line> if set, else "error not set".
char file[RCUTILS_ERROR_STATE_FILE_MAX_LENGTH]
Definition: error_handling.h:118
uint64_t line_number
Line number of error.
Definition: error_handling.h:120
void rcutils_reset_error(void)
Reset the error state by clearing any previously set error state.
#define RCUTILS_ERROR_MESSAGE_MAX_LENGTH
The maximum formatted string length.
Definition: error_handling.h:84
#define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH
The maximum length for user defined error message.
Definition: error_handling.h:91
int rcutils_ret_t
The type that holds a return value for an rcutils operation.
Definition: rcutils_ret.h:26
struct rcutils_error_string_t rcutils_error_string_t
Struct wrapping a fixed-size c string used for returning the formatted error string.
#define RCUTILS_WARN_UNUSED
A macro to make the compiler warn when the return value of a function is not used.
Definition: macros.h:27
Encapsulation of an allocator.
Definition: allocator.h:47
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
void rcutils_set_error_state(const char *error_string, const char *file, size_t line_number)
Set the error message, as well as the file and line on which it occurred.
char str[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]
The fixed-size C string used for returning the formatted error string.
Definition: error_handling.h:108
char message[RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH]
User message storage, limited to RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH characters.
Definition: error_handling.h:115
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
Definition: error_handling.h:112
rcutils_ret_t rcutils_initialize_error_handling_thread_local_storage(rcutils_allocator_t allocator)
Forces initialization of thread-local storage if called in a newly created thread.
#define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH
The calculated maximum length for the filename.
Definition: error_handling.h:97