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 #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"
41 
42 #ifdef __STDC_LIB_EXT1__
43 // Limit the buffer size in the `fwrite` call to give an upper bound to buffer overrun in the case
44 // of non-null terminated `msg`.
45 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
46  do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
47 #else
48 #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
49  do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
50 #endif
51 
52 // fixed constraints
53 #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615"
54 #define RCUTILS_ERROR_FORMATTING_CHARACTERS 6 // ', at ' + ':'
55 
56 // max formatted string length
57 #define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1024
58 
59 // adjustable max length for user defined error message
60 // remember "chained" errors will include previously specified file paths
61 // e.g. "some error, at /path/to/a.c:42, at /path/to/b.c:42"
62 #define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 768
63 // with RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH = 768, RCUTILS_ERROR_STATE_FILE_MAX_LENGTH == 229
64 #define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH ( \
65  RCUTILS_ERROR_MESSAGE_MAX_LENGTH - \
66  RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH - \
67  RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH - \
68  RCUTILS_ERROR_FORMATTING_CHARACTERS - \
69  1)
70 
72 typedef struct rcutils_error_string_t
73 {
76 
78 typedef struct rcutils_error_state_t
79 {
86  uint64_t line_number;
88 
89 // make sure our math is right...
90 #if __STDC_VERSION__ >= 201112L
91 static_assert(
92  sizeof(rcutils_error_string_t) == (
97  1 /* null terminating character */),
98  "Maximum length calculations incorrect");
99 #endif
100 
102 
142 
144 
158 void
159 rcutils_set_error_state(const char * error_string, const char * file, size_t line_number);
160 
162 
169 #define RCUTILS_CHECK_ARGUMENT_FOR_NULL(argument, error_return_type) \
170  RCUTILS_CHECK_FOR_NULL_WITH_MSG(argument, #argument " argument is null", \
171  return error_return_type)
172 
174 
182 #define RCUTILS_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement) \
183  do { \
184  if (NULL == value) { \
185  RCUTILS_SET_ERROR_MSG(msg); \
186  error_statement; \
187  } \
188  } while (0)
189 
191 
200 #define RCUTILS_SET_ERROR_MSG(msg) \
201  do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
202 
204 
212 #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
213  do { \
214  char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
215  int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \
216  if (ret < 0) { \
217  RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \
218  } else { \
219  RCUTILS_SET_ERROR_MSG(output_msg); \
220  } \
221  } while (0)
222 
226 bool
228 
230 
240 const rcutils_error_state_t *
242 
244 
257 
260 void
261 rcutils_reset_error(void);
262 
263 #ifdef __cplusplus
264 }
265 #endif
266 
267 #endif // RCUTILS__ERROR_HANDLING_H_
#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH
Definition: error_handling.h:53
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.
uint64_t line_number
Line number of error.
Definition: error_handling.h:86
#define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH
Definition: error_handling.h:64
char str[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]
Definition: error_handling.h:74
struct rcutils_error_state_t rcutils_error_state_t
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
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
rcutils_error_string_t rcutils_get_error_string(void)
Return the error message followed by , at <file>:<line> if set, else "error not set".
#define RCUTILS_ERROR_FORMATTING_CHARACTERS
Definition: error_handling.h:54
struct rcutils_error_string_t rcutils_error_string_t
Struct wrapping a fixed-size c string used for returning the formatted error string.
Encapsulation of an allocator.
Definition: allocator.h:45
#define RCUTILS_ERROR_MESSAGE_MAX_LENGTH
Definition: error_handling.h:57
#define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH
Definition: error_handling.h:62
#define RCUTILS_WARN_UNUSED
Definition: macros.h:24
#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().
Struct wrapping a fixed-size c string used for returning the formatted error string.
Definition: error_handling.h:72
Struct which encapsulates the error state set by RCUTILS_SET_ERROR_MSG().
Definition: error_handling.h:78
bool rcutils_error_is_set(void)
Return true if the error is set, otherwise false.
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.