CNDP  22.08.0
cne_log.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation
3  */
4 
5 #ifndef _CNE_LOG_H_
6 #define _CNE_LOG_H_
7 
16 #include <stdio.h> // for NULL
17 #include <stdarg.h> // for va_list
18 #include <stdint.h> // for uint32_t
19 #include <cne_common.h> // for CNDP_API
20 #include <cne_stdio.h>
21 
22 #include "cne_build_config.h" // for CNE_ENABLE_ASSERT
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define foreach_cndp_log_level \
29  _(1, EMERG, emerg) \
30  _(2, ALERT, alert) \
31  _(3, CRIT, crit) \
32  _(4, ERR, err) \
33  _(5, WARNING, warn) \
34  _(6, NOTICE, notice) \
35  _(7, INFO, info) \
36  _(8, DEBUG, debug) \
37  _(9, LAST, last)
38 
39 /* Can't use 0, as it gives compiler warnings */
40 /*
41  * The log levels are defined as follows
42  * CNE_LOG_EMERG System is unusable.
43  * CNE_LOG_ALERT Action must be taken immediately.
44  * CNE_LOG_CRIT Critical conditions.
45  * CNE_LOG_ERR Error conditions.
46  * CNE_LOG_WARNING Warning conditions.
47  * CNE_LOG_NOTICE Normal but significant condition.
48  * CNE_LOG_INFO Informational.
49  * CNE_LOG_DEBUG Debug-level messages.
50  * CNE_LOG_LAST
51  */
52 enum {
53 #define _(n, uc, lc) CNE_LOG_##uc = n,
54  foreach_cndp_log_level
55 #undef _
56 };
57 
64 CNDP_API void cne_log_set_level(uint32_t level);
65 
75 CNDP_API int cne_log_set_level_str(char *log_level);
76 
83 CNDP_API uint32_t cne_log_get_level(void);
84 
110 CNDP_API int cne_log(uint32_t level, const char *func, int line, const char *format, ...)
111 #ifdef __GNUC__
112 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
113  __attribute__((cold))
114 #endif
115 #endif
116  __attribute__((format(printf, 4, 5)));
117 
130 CNDP_API int cne_print(const char *format, ...)
131 #ifdef __GNUC__
132 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
133  __attribute__((cold))
134 #endif
135 #endif
136  __attribute__((format(printf, 1, 2)));
137 
165 CNDP_API int cne_vlog(uint32_t level, const char *func, int line, const char *format, va_list ap)
166  __attribute__((format(printf, 4, 0)));
167 
184 #define CNE_LOG(f, ...) cne_log(CNE_LOG_##f, __func__, __LINE__, #f ": " __VA_ARGS__)
185 
198 #define CNE_PRINT(f, args...) cne_print(f, ##args)
199 
203 #define CNE_EMERG(...) CNE_LOG(EMERG, __VA_ARGS__)
204 #define CNE_ALERT(...) CNE_LOG(ALERT, __VA_ARGS__)
205 #define CNE_CRIT(...) CNE_LOG(CRIT, __VA_ARGS__)
206 #define CNE_ERR(...) CNE_LOG(ERR, __VA_ARGS__)
207 #define CNE_WARN(...) CNE_LOG(WARNING, __VA_ARGS__)
208 #define CNE_NOTICE(...) CNE_LOG(NOTICE, __VA_ARGS__)
209 #define CNE_INFO(...) CNE_LOG(INFO, __VA_ARGS__)
210 #define CNE_DEBUG(...) CNE_LOG(DEBUG, __VA_ARGS__)
211 
222 #define CNE_ERR_RET_VAL(_val, ...) \
223  do { \
224  CNE_ERR(__VA_ARGS__); \
225  return _val; \
226  } while ((0))
227 
238 #define CNE_RET(...) CNE_ERR_RET_VAL(, __VA_ARGS__)
239 
250 #define CNE_ERR_RET(...) CNE_ERR_RET_VAL(-1, __VA_ARGS__)
251 
262 #define CNE_NULL_RET(...) CNE_ERR_RET_VAL(NULL, __VA_ARGS__)
263 
273 #define CNE_ERR_GOTO(lbl, ...) \
274  do { \
275  CNE_ERR(__VA_ARGS__); \
276  goto lbl; \
277  } while ((0))
278 
282 CNDP_API void cne_dump_stack(void);
283 
300 #define cne_panic(format, args...) __cne_panic(__func__, __LINE__, format "\n", ##args)
301 
315 #define cne_exit(format, args...) __cne_exit(__func__, __LINE__, format "\n", ##args)
316 
317 #if CNE_ENABLE_ASSERT
327 #define CNE_ASSERT(exp) CNE_VERIFY(exp)
328 #else
329 #define CNE_ASSERT(exp) \
330  do { \
331  } while (0)
332 #endif
333 
342 #define CNE_VERIFY(exp) \
343  do { \
344  if (unlikely(!(exp))) \
345  cne_panic("assert \"%s\" failed", #exp); \
346  } while (0)
347 
354 CNDP_API void __cne_panic(const char *funcname, int line, const char *format, ...)
355 #ifdef __GNUC__
356 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
357  __attribute__((cold))
358 #endif
359 #endif
360  __attribute__((noreturn)) __attribute__((format(printf, 3, 4)));
361 
368 CNDP_API void __cne_exit(const char *func, int line, const char *format, ...)
369 #ifdef __GNUC__
370 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
371  __attribute__((cold))
372 #endif
373 #endif
374  __attribute__((noreturn)) __attribute__((format(printf, 3, 4)));
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif /* _CNE_LOG_H_ */
CNDP_API int cne_vlog(uint32_t level, const char *func, int line, const char *format, va_list ap)
CNDP_API int cne_log(uint32_t level, const char *func, int line, const char *format,...)
CNDP_API void __cne_panic(const char *funcname, int line, const char *format,...)
CNDP_API int cne_log_set_level_str(char *log_level)
CNDP_API uint32_t cne_log_get_level(void)
CNDP_API int cne_print(const char *format,...)
CNDP_API void cne_dump_stack(void)
CNDP_API void cne_log_set_level(uint32_t level)
CNDP_API void __cne_exit(const char *func, int line, const char *format,...)