summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/status-checks.h
blob: 51dba23aa69f9e32b540249588c82645acf1519d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
 * @file
 *
 * @brief Header File for Status Checks
 *
 * @warning Do not include this file in other header files.  Use it only in
 * source files.
 */

/*
 * Copyright (c) 2008
 * Embedded Brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * rtems@embedded-brains.de
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef RTEMS_STATUS_CHECKS_H
#define RTEMS_STATUS_CHECKS_H

#include <rtems/bspIo.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @defgroup rtems_status_checks Status Checks
 *
 * @ingroup RTEMSAPI
 */
/**@{**/

/**
 * @name Print Macros
 */
/**@{**/

/**
 * @brief General purpose debug print macro.
 */
#ifdef DEBUG
  #ifndef RTEMS_DEBUG_PRINT
    #ifdef RTEMS_STATUS_CHECKS_USE_PRINTK
      #define RTEMS_DEBUG_PRINT( fmt, ...) \
        printk( "%s: " fmt, __func__, ##__VA_ARGS__)
    #else /* RTEMS_STATUS_CHECKS_USE_PRINTK */
      #include <stdio.h>
      #define RTEMS_DEBUG_PRINT( fmt, ...) \
        printf( "%s: " fmt, __func__, ##__VA_ARGS__)
    #endif /* RTEMS_STATUS_CHECKS_USE_PRINTK */
  #endif /* RTEMS_DEBUG_PRINT */
#else /* DEBUG */
  #ifdef RTEMS_DEBUG_PRINT
    #warning RTEMS_DEBUG_PRINT was defined, but DEBUG was undefined
    #undef RTEMS_DEBUG_PRINT
  #endif /* RTEMS_DEBUG_PRINT */
  #define RTEMS_DEBUG_PRINT( fmt, ...)
#endif /* DEBUG */

/**
 * @brief Macro to print debug messages for successful operations.
 */
#define RTEMS_DEBUG_OK( msg) \
  RTEMS_DEBUG_PRINT( "Ok: %s\n", msg)

/**
 * @brief General purpose system log print macro.
 */
#ifndef RTEMS_SYSLOG_PRINT
  #ifdef RTEMS_STATUS_CHECKS_USE_PRINTK
    #define RTEMS_SYSLOG_PRINT( fmt, ...) \
      printk( fmt, ##__VA_ARGS__)
  #else /* RTEMS_STATUS_CHECKS_USE_PRINTK */
    #include <stdio.h>
    #define RTEMS_SYSLOG_PRINT( fmt, ...) \
      printf( fmt, ##__VA_ARGS__)
  #endif /* RTEMS_STATUS_CHECKS_USE_PRINTK */
#endif /* RTEMS_SYSLOG_PRINT */

/**
 * @brief General purpose system log macro.
 */
#define RTEMS_SYSLOG( fmt, ...) \
  RTEMS_SYSLOG_PRINT( "%s: " fmt, __func__, ##__VA_ARGS__)

/**
 * @brief General purpose system log macro for warnings.
 */
#define RTEMS_SYSLOG_WARNING( fmt, ...) \
  RTEMS_SYSLOG( "Warning: " fmt, ##__VA_ARGS__)

/**
 * @brief Macro to generate a system log warning message if the status code @a
 * sc is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_SYSLOG_WARNING_SC( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_WARNING( "SC = %i: %s\n", (int) sc, msg); \
  }

/**
 * @brief General purpose system log macro for errors.
 */
#define RTEMS_SYSLOG_ERROR( fmt, ...) \
  RTEMS_SYSLOG( "Error: " fmt, ##__VA_ARGS__)

/**
 * @brief Macro for system log error messages with status code.
 */
#define RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg) \
  RTEMS_SYSLOG_ERROR( "SC = %i: %s\n", (int) sc, msg);

/**
 * @brief Macro for system log error messages with return value.
 */
#define RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg) \
  RTEMS_SYSLOG_ERROR( "RV = %i: %s\n", (int) rv, msg);

/**
 * @brief Macro to generate a system log error message if the status code @a
 * sc is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_SYSLOG_ERROR_SC( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
  }

/**
 * @brief Macro to generate a system log error message if the return value @a
 * rv is less than zero.
 */
#define RTEMS_SYSLOG_ERROR_RV( rv, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
  }

/** @} */

/**
 * @name Check Macros
 */
/**@{**/

/**
 * @brief Prints message @a msg and returns with status code @a sc if the status
 * code @a sc is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_CHECK_SC( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    return (rtems_status_code) sc; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and returns with a return value of negative @a sc
 * if the status code @a sc is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_CHECK_SC_RV( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    return -((int) (sc)); \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and returns if the status code @a sc is not equal
 * to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_CHECK_SC_VOID( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    return; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and delete the current task if the status code
 * @a sc is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_CHECK_SC_TASK( sc, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    rtems_task_exit(); \
    return; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and returns with a return value @a rv if the
 * return value @a rv is less than zero.
 */
#define RTEMS_CHECK_RV( rv, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    return (int) rv; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and returns with status code @ref RTEMS_IO_ERROR
 * if the return value @a rv is less than zero.
 */
#define RTEMS_CHECK_RV_SC( rv, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    return RTEMS_IO_ERROR; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and returns if the return value @a rv is less
 * than zero.
 */
#define RTEMS_CHECK_RV_VOID( rv, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    return; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and delete the current task if the return value
 * @a rv is less than zero.
 */
#define RTEMS_CHECK_RV_TASK( rv, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    rtems_task_exit(); \
    return; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/** @} */

/**
 * @name Cleanup Macros
 */
/**@{**/

/**
 * @brief Prints message @a msg and jumps to @a label if the status code @a sc
 * is not equal to @ref RTEMS_SUCCESSFUL.
 */
#define RTEMS_CLEANUP_SC( sc, label, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    goto label; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and jumps to @a label if the status code @a sc
 * is not equal to @ref RTEMS_SUCCESSFUL.  The return value variable @a rv will
 * be set to a negative @a sc in this case.
 */
#define RTEMS_CLEANUP_SC_RV( sc, rv, label, msg) \
  if ((rtems_status_code) (sc) != RTEMS_SUCCESSFUL) { \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    rv = -((int) (sc)); \
    goto label; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and jumps to @a label if the return value @a rv
 * is less than zero.
 */
#define RTEMS_CLEANUP_RV( rv, label, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    goto label; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and jumps to @a label if the return value @a rv
 * is less than zero.  The status code variable @a sc will be set to @ref
 * RTEMS_IO_ERROR in this case.
 */
#define RTEMS_CLEANUP_RV_SC( rv, sc, label, msg) \
  if ((int) (rv) < 0) { \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    sc = RTEMS_IO_ERROR; \
    goto label; \
  } else { \
    RTEMS_DEBUG_OK( msg); \
  }

/**
 * @brief Prints message @a msg and jumps to @a label.
 */
#define RTEMS_DO_CLEANUP( label, msg) \
  do { \
    RTEMS_SYSLOG_ERROR( msg); \
    goto label; \
  } while (0)

/**
 * @brief Prints message @a msg, sets the status code variable @a sc to @a val
 * and jumps to @a label.
 */
#define RTEMS_DO_CLEANUP_SC( val, sc, label, msg) \
  do { \
    sc = (rtems_status_code) val; \
    RTEMS_SYSLOG_ERROR_WITH_SC( sc, msg); \
    goto label; \
  } while (0)

/**
 * @brief Prints message @a msg, sets the return value variable @a rv to @a val
 * and jumps to @a label.
 */
#define RTEMS_DO_CLEANUP_RV( val, rv, label, msg) \
  do { \
    rv = (int) val; \
    RTEMS_SYSLOG_ERROR_WITH_RV( rv, msg); \
    goto label; \
  } while (0)

/** @} */

/** @} */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* RTEMS_STATUS_CHECKS_H */