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 
15 // Note: migrated from rmw/error_handling.h in 2017-04
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 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "rcutils/allocator.h"
32 #include "rcutils/format_string.h"
33 #include "rcutils/macros.h"
36 
38 typedef struct rcutils_error_state_t
39 {
40  const char * message;
41  const char * file;
42  size_t line_number;
45 
46 // TODO(dhood): use __STDC_LIB_EXT1__ if/when supported in other implementations.
47 #if defined(_WIN32)
48 // Limit the buffer size in the `fwrite` call to give an upper bound to buffer overrun in the case
49 // of non-null terminated `msg`.
50 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr)
51 #else
52 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) fwrite(msg, sizeof(char), strlen(msg), stderr)
53 #endif
54 
56 
72 
75 void
77 
79 
101 void
103  const char * error_string, const char * file, size_t line_number, rcutils_allocator_t allocator);
104 
106 
114 #define RCUTILS_CHECK_ARGUMENT_FOR_NULL(argument, error_return_type, allocator) \
115  RCUTILS_CHECK_FOR_NULL_WITH_MSG(argument, #argument " argument is null", \
116  return error_return_type, allocator)
117 
119 
128 #define RCUTILS_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement, allocator) \
129  if (NULL == value) { \
130  RCUTILS_SET_ERROR_MSG(msg, allocator); \
131  error_statement; \
132  }
133 
135 
145 #define RCUTILS_SET_ERROR_MSG(msg, allocator) \
146  rcutils_set_error_state(msg, __FILE__, __LINE__, allocator);
147 
149 
157 #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(allocator, format_string, ...) \
158  do { \
159  char * output_msg = NULL; \
160  output_msg = rcutils_format_string(allocator, format_string, __VA_ARGS__); \
161  if (output_msg) { \
162  RCUTILS_SET_ERROR_MSG(output_msg, allocator); \
163  allocator.deallocate(output_msg, allocator.state); \
164  } else { \
165  RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to allocate memory for error message\n"); \
166  } \
167  } while (false)
168 
171 bool
173 
175 
184 const rcutils_error_state_t *
186 
188 
195 const char *
197 
199 
208 const char *
210 
213 void
214 rcutils_reset_error(void);
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 #endif // RCUTILS__ERROR_HANDLING_H_
const char * file
Definition: error_handling.h:41
void rcutils_set_error_state(const char *error_string, const char *file, size_t line_number, rcutils_allocator_t allocator)
Set the error message, as well as the file and line on which it occurred.
struct rcutils_error_state_t rcutils_error_state_t
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
const char * rcutils_get_error_string_safe(void)
Return the error message followed by , at <file>:<line> if set, else "error not set".
void rcutils_reset_error(void)
Reset the error state by clearing any previously set error state.
int rcutils_ret_t
Definition: rcutils_ret.h:23
Encapsulation of an allocator.
Definition: allocator.h:45
rcutils_ret_t rcutils_error_state_copy(const rcutils_error_state_t *src, rcutils_error_state_t *dst)
Copy an error state into a destination error state.
const char * message
Definition: error_handling.h:40
#define RCUTILS_WARN_UNUSED
Definition: macros.h:24
const char * rcutils_get_error_string(void)
Return the error message followed by , at <file>:<line>, or NULL.
void rcutils_error_state_fini(rcutils_error_state_t *error_state)
Finalizes a copied error state.
size_t line_number
Definition: error_handling.h:42
#define RCUTILS_PUBLIC
Definition: visibility_control.h:48
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_allocator_t allocator
Definition: error_handling.h:43
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
Definition: error_handling.h:38
bool rcutils_error_is_set(void)
Return true if the error is set, otherwise false.