My Project
Loading...
Searching...
No Matches
iio-debug.h
1/* SPDX-License-Identifier: MIT */
2/*
3 * libiio - Library for interfacing industrial I/O (IIO) devices
4 *
5 * Copyright (C) 2021-2023 Analog Devices, Inc.
6 * Author: Paul Cercueil <paul.cercueil@analog.com>
7 */
8
9#ifndef __IIO_DEBUG_H__
10#define __IIO_DEBUG_H__
11
12#include <iio/iio.h>
13
14#define __api __iio_api
15
16#if defined(__MINGW32__)
17#define __iio_printf __attribute__((__format__(gnu_printf, 3, 4)))
18#elif defined(__GNUC__)
19#define __iio_printf __attribute__((__format__(printf, 3, 4)))
20#else
21#define __iio_printf
22#endif
23
24#ifdef LIBIIO_EXPORTS
25#include "iio-config.h"
26#define LIBIIO_MAX_LOG_LEVEL MAX_LOG_LEVEL
27#else
28#define LIBIIO_MAX_LOG_LEVEL LEVEL_DEBUG
29#endif
30
31#define __FIRST(a, ...) a
32#define ___OTHERS(a, ...) a, __VA_ARGS__
33#define __OTHERS(a, b, ...) ___OTHERS(a, __VA_ARGS__)
34#define __SKIPFIRST(a, ...) __VA_ARGS__
35
41__api __iio_printf void iio_prm_printf(const struct iio_context_params *params,
42 enum iio_log_level msg_level, const char *fmt, ...);
43
44#define __ctx_params_or_null(ctx) ((ctx) ? iio_context_get_params(ctx) : NULL)
45#define __dev_ctx_or_null(dev) ((dev) ? iio_device_get_context(dev) : NULL)
46#define __chn_dev_or_null(chn) ((chn) ? iio_channel_get_device(chn) : NULL)
47
48/* Passing a null pointer for a "%s" directive is undefined behaviour; glibc
49 * prints "(null)" but other C libraries are free to crash. Substitute the
50 * string itself so that the device/channel ID is always printable. */
51#define __dev_id_str(dev) ((dev) ? iio_device_get_id(dev) : "(null)")
52#define __chn_id_str(chn) ((chn) ? iio_channel_get_id(chn) : "(null)")
53
54#define prm_err(prm, ...) \
55 do { \
56 if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_ERROR) \
57 iio_prm_printf((prm), LEVEL_ERROR, "ERROR: " __VA_ARGS__); \
58 } while (0)
59#define prm_warn(prm, ...) \
60 do { \
61 if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_WARNING) \
62 iio_prm_printf((prm), LEVEL_WARNING, "WARNING: " __VA_ARGS__); \
63 } while (0)
64#define prm_info(prm, ...) \
65 do { \
66 if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_INFO) \
67 iio_prm_printf((prm), LEVEL_INFO, __VA_ARGS__); \
68 } while (0)
69#define prm_dbg(prm, ...) \
70 do { \
71 if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_DEBUG) \
72 iio_prm_printf((prm), LEVEL_DEBUG, "DEBUG: " __VA_ARGS__); \
73 } while (0)
74
75#define ctx_err(ctx, ...) prm_err(__ctx_params_or_null(ctx), __VA_ARGS__)
76#define ctx_warn(ctx, ...) prm_warn(__ctx_params_or_null(ctx), __VA_ARGS__)
77#define ctx_info(ctx, ...) prm_info(__ctx_params_or_null(ctx), __VA_ARGS__)
78#define ctx_dbg(ctx, ...) prm_dbg(__ctx_params_or_null(ctx), __VA_ARGS__)
79
80#define dev_err(dev, ...) \
81 ctx_err(__dev_ctx_or_null(dev), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __dev_id_str(dev), \
82 __SKIPFIRST(__VA_ARGS__, ""))
83#define dev_warn(dev, ...) \
84 ctx_warn(__dev_ctx_or_null(dev), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __dev_id_str(dev), \
85 __SKIPFIRST(__VA_ARGS__, ""))
86#define dev_info(dev, ...) \
87 ctx_info(__dev_ctx_or_null(dev), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __dev_id_str(dev), \
88 __SKIPFIRST(__VA_ARGS__, ""))
89#define dev_dbg(dev, ...) \
90 ctx_dbg(__dev_ctx_or_null(dev), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __dev_id_str(dev), \
91 __SKIPFIRST(__VA_ARGS__, ""))
92
93#define chn_err(dev, ...) \
94 dev_err(__chn_dev_or_null(chn), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __chn_id_str(chn), \
95 __SKIPFIRST(__VA_ARGS__, ""))
96#define chn_warn(dev, ...) \
97 dev_warn(__chn_dev_or_null(chn), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __chn_id_str(chn), \
98 __SKIPFIRST(__VA_ARGS__, ""))
99#define chn_info(dev, ...) \
100 dev_info(__chn_dev_or_null(chn), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __chn_id_str(chn), \
101 __SKIPFIRST(__VA_ARGS__, ""))
102#define chn_dbg(dev, ...) \
103 dev_dbg(__chn_dev_or_null(chn), "%s: " __FIRST(__VA_ARGS__, 0) "%s", __chn_id_str(chn), \
104 __SKIPFIRST(__VA_ARGS__, ""))
105
106#define prm_perror(params, err, ...) \
107 do { \
108 if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_ERROR) { \
109 char _buf[1024]; \
110 int _err = -(err); \
111 iio_strerror(_err, _buf, sizeof(_buf)); \
112 prm_err(params, __FIRST(__VA_ARGS__, 0) \
113 __OTHERS(": %s\n", __VA_ARGS__, _buf)); \
114 } \
115 } while (0)
116#define ctx_perror(ctx, err, ...) prm_perror(__ctx_params_or_null(ctx), err, __VA_ARGS__)
117#define dev_perror(dev, err, ...) \
118 ctx_perror(__dev_ctx_or_null(dev), err, "%s: " __FIRST(__VA_ARGS__, 0) "%s", \
119 __dev_id_str(dev), __SKIPFIRST(__VA_ARGS__, ""))
120#define chn_perror(dev, err, ...) \
121 dev_perror(__chn_dev_or_null(chn), err, "%s: " __FIRST(__VA_ARGS__, 0) "%s", \
122 __chn_id_str(chn), __SKIPFIRST(__VA_ARGS__, ""))
123
124#undef __api
125
126#endif /* __IIO_DEBUG_H__ */
Public interface.
iio_log_level
Level of verbosity of libiio's log output.
Definition iio.h:131
IIO context creation information.
Definition iio.h:173