Helper Reference Manual  1.7.1
debug.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2018 Linaro Limited
3  * Copyright (c) 2019-2024 Nokia
4  */
5 
12 #ifndef ODPH_DEBUG_H_
13 #define ODPH_DEBUG_H_
14 
15 #include <odp/helper/autoheader_external.h>
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #pragma GCC diagnostic push
25 
26 #ifdef __clang__
27 #pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
28 #endif
29 
40 typedef enum odph_log_level {
41  ODPH_LOG_DBG,
42  ODPH_LOG_ERR,
43  ODPH_LOG_ABORT
45 
57 #define ODPH_LOG(level, fmt, ...) \
58 do { \
59  if (level != ODPH_LOG_DBG || ODPH_DEBUG_PRINT == 1) { \
60  const odp_log_func_t fn = odp_log_fn_get(); \
61  if (fn) { \
62  const odp_log_level_t lv = level == ODPH_LOG_ABORT ? ODP_LOG_ABORT : \
63  level == ODPH_LOG_ERR ? ODP_LOG_ERR : \
64  ODP_LOG_DBG; \
65  fn(lv, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
66  } else { \
67  fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, \
68  ##__VA_ARGS__); \
69  } \
70  } \
71  if (level == ODPH_LOG_ABORT) { \
72  odp_abort_func_t fn = odp_abort_fn_get(); \
73  if (fn) \
74  fn(); \
75  else \
76  abort(); \
77  } \
78 } while (0)
79 
85 #define ODPH_ASSERT(cond) \
86  do { \
87  if ((ODPH_DEBUG == 1) && (!(cond))) \
88  ODPH_LOG(ODPH_LOG_ABORT, "%s\n", #cond); \
89  } while (0)
90 
95 #define ODPH_DBG(...) \
96  do { \
97  __extension__ ({ \
98  ODPH_LOG(ODPH_LOG_DBG, ##__VA_ARGS__); \
99  }); \
100  } while (0)
101 
106 #define ODPH_ERR(...) \
107  do { \
108  __extension__ ({ \
109  ODPH_LOG(ODPH_LOG_ERR, ##__VA_ARGS__); \
110  }); \
111  } while (0)
112 
117 #define ODPH_ABORT(...) \
118  do { \
119  __extension__ ({ \
120  ODPH_LOG(ODPH_LOG_ABORT, ##__VA_ARGS__); \
121  }); \
122  } while (0)
123 
128 #pragma GCC diagnostic pop
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif
enum odph_log_level odph_log_level_e
Log level.
odph_log_level
Log level.
Definition: debug.h:40