rcutils  master
C API providing common utilities and data structures.
error_handling.h
Go to the documentation of this file.
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #ifndef RCUTILS__ERROR_HANDLING_H_
18 #define RCUTILS__ERROR_HANDLING_H_
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 #ifndef __STDC_WANT_LIB_EXT1__
26 #define __STDC_WANT_LIB_EXT1__ 1 // indicate we would like strnlen_s if available
27 #endif
28 #include <assert.h>
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "rcutils/allocator.h"
37 #include "rcutils/macros.h"
38 #include "rcutils/snprintf.h"
42 
43 #ifdef __STDC_LIB_EXT1__
44 
49 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
50  do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
51 #else
52 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
54  do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
55 #endif
56 
58 
66 #define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) \
67  do { \
68  char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
69  int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \
70  if (ret < 0) { \
71  RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \
72  } else { \
73  RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \
74  } \
75  } while (0)
76 
78 #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615"
79 
81 #define RCUTILS_ERROR_FORMATTING_CHARACTERS 6 // ', at ' + ':'
82 
84 #define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1024
85 
87 
91 #define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 768
92 
94 
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 - \
102  1)
103 
106 {
110 
112 typedef struct rcutils_error_state_t
113 {
120  uint64_t line_number;
122 
123 // make sure our math is right...
124 #if __STDC_VERSION__ >= 201112L
125 static_assert(
126  sizeof(rcutils_error_string_t) == (
131  1 /* null terminating character */),
132  "Maximum length calculations incorrect");
133 #endif
134 
136 
177 
179 
193 void
194 rcutils_set_error_state(const char * error_string, const char * file, size_t line_number);
195 
197 
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)
208 
210 
218 #define RCUTILS_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement) \
219  do { \
220  if (NULL == value) { \
221  RCUTILS_SET_ERROR_MSG(msg); \
222  error_statement; \
223  } \
224  } while (0)
225 
227 
236 #define RCUTILS_SET_ERROR_MSG(msg) \
237  do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
238 
240 
248 #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
249  do { \
250  char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
251  int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \
252  if (ret < 0) { \
253  RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \
254  } else { \
255  RCUTILS_SET_ERROR_MSG(output_msg); \
256  } \
257  } while (0)
258 
260 
270 #define RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(error_return_value) \
271  RCUTILS_CAN_FAIL_WITH( \
272  { \
273  RCUTILS_SET_ERROR_MSG("Injecting " RCUTILS_STRINGIFY(error_return_value)); \
274  return error_return_value; \
275  })
276 
280 bool
282 
284 
294 const rcutils_error_state_t *
296 
298 
311 
314 void
315 rcutils_reset_error(void);
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
321 #endif // RCUTILS__ERROR_HANDLING_H_
rcutils_error_is_set
bool rcutils_error_is_set(void)
Return true if the error is set, otherwise false.
rcutils_error_state_t
struct rcutils_error_state_t rcutils_error_state_t
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
rcutils_get_error_state
const rcutils_error_state_t * rcutils_get_error_state(void)
Return an rcutils_error_state_t which was set with rcutils_set_error_state().
RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH
#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH
The maximum length a formatted number is allowed to have.
Definition: error_handling.h:78
RCUTILS_ERROR_FORMATTING_CHARACTERS
#define RCUTILS_ERROR_FORMATTING_CHARACTERS
The maximum number of formatting characters allowed.
Definition: error_handling.h:81
macros.h
rcutils_error_string_t
Struct wrapping a fixed-size c string used for returning the formatted error string.
Definition: error_handling.h:105
snprintf.h
rcutils_get_error_string
rcutils_error_string_t rcutils_get_error_string(void)
Return the error message followed by , at <file>:<line> if set, else "error not set".
rcutils_error_state_t::file
char file[RCUTILS_ERROR_STATE_FILE_MAX_LENGTH]
Definition: error_handling.h:118
rcutils_error_state_t::line_number
uint64_t line_number
Line number of error.
Definition: error_handling.h:120
rcutils_reset_error
void rcutils_reset_error(void)
Reset the error state by clearing any previously set error state.
RCUTILS_ERROR_MESSAGE_MAX_LENGTH
#define RCUTILS_ERROR_MESSAGE_MAX_LENGTH
The maximum formatted string length.
Definition: error_handling.h:84
RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH
#define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH
The maximum length for user defined error message.
Definition: error_handling.h:91
rcutils_ret_t
int rcutils_ret_t
The type that holds a return value for an rcutils operation.
Definition: rcutils_ret.h:26
rcutils_error_string_t
struct rcutils_error_string_t rcutils_error_string_t
Struct wrapping a fixed-size c string used for returning the formatted error string.
RCUTILS_WARN_UNUSED
#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
rcutils_ret.h
visibility_control.h
rcutils_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:47
allocator.h
RCUTILS_PUBLIC
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
fault_injection.h
rcutils_set_error_state
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.
rcutils_error_string_t::str
char str[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]
The fixed-size C string used for returning the formatted error string.
Definition: error_handling.h:108
rcutils_error_state_t::message
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
rcutils_error_state_t
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
Definition: error_handling.h:112
rcutils_initialize_error_handling_thread_local_storage
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.
RCUTILS_ERROR_STATE_FILE_MAX_LENGTH
#define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH
The calculated maximum length for the filename.
Definition: error_handling.h:97