Skip to content

Commit

Permalink
CMake: Add option to specify the maximum log level
Browse files Browse the repository at this point in the history
This makes sense for embedded, as compiling with a lower maximum log
level means that all strings that correspond to the higher log levels
will be automatically discarded by the compiler, resulting in a smaller
binary.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Dec 4, 2023
1 parent d7772bb commit cc65317
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,26 @@ option(WITH_EXAMPLES "Build examples" OFF)
option(WITH_UTILS "Build the Libiio utility programs" ON)

if (NOT LOG_LEVEL)
set(LOG_LEVEL Info CACHE STRING "Log level" FORCE)
set(LOG_LEVEL Info CACHE STRING "Default log level" FORCE)
set_property(CACHE LOG_LEVEL PROPERTY STRINGS NoLog Error Warning Info Debug)
endif()

if (NOT MAX_LOG_LEVEL)
set(MAX_LOG_LEVEL Debug CACHE STRING "Maximum log level supported" FORCE)
set_property(CACHE MAX_LOG_LEVEL PROPERTY STRINGS NoLog Error Warning Info Debug)
endif()

set(LEVEL_NoLog 1)
set(LEVEL_Error 2)
set(LEVEL_Warning 3)
set(LEVEL_Info 4)
set(LEVEL_Debug 5)
set(DEFAULT_LOG_LEVEL ${LEVEL_${LOG_LEVEL}})
set(MAX_LOG_LEVEL_VALUE ${LEVEL_${MAX_LOG_LEVEL}})

if (DEFAULT_LOG_LEVEL GREATER MAX_LOG_LEVEL_VALUE)
message(SEND_ERROR "Default log level cannot be more than the maximum log level.")
endif()

if (MSVC)
target_compile_options(iio PRIVATE /Zi /W4 /wd4200 /wd4127 /wd4100)
Expand Down
1 change: 1 addition & 0 deletions iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#define LOG_LEVEL @LOG_LEVEL@_L
#define DEFAULT_LOG_LEVEL @DEFAULT_LOG_LEVEL@
#define MAX_LOG_LEVEL @MAX_LOG_LEVEL_VALUE@

#define IIO_MODULES_DIR "@IIO_MODULES_DIR@"
#define IIO_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@"
Expand Down
43 changes: 34 additions & 9 deletions include/iio/iio-debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
# define __iio_printf
#endif

#ifdef LIBIIO_EXPORTS
#include "iio-config.h"
#define LIBIIO_MAX_LOG_LEVEL MAX_LOG_LEVEL
#else
#define LIBIIO_MAX_LOG_LEVEL LEVEL_DEBUG
#endif

#define __FIRST(a, ...) a
#define ___OTHERS(a, ...) a, __VA_ARGS__
#define __OTHERS(a, b, ...) ___OTHERS(a, __VA_ARGS__)
Expand All @@ -44,10 +51,26 @@ iio_prm_printf(const struct iio_context_params *params,
#define __dev_id_or_null(dev) ((dev) ? iio_device_get_id(dev) : NULL)
#define __chn_id_or_null(chn) ((chn) ? iio_channel_get_id(chn) : NULL)

#define prm_err(prm, ...) iio_prm_printf((prm), LEVEL_ERROR, "ERROR: " __VA_ARGS__)
#define prm_warn(prm, ...) iio_prm_printf((prm), LEVEL_WARNING, "WARNING: " __VA_ARGS__)
#define prm_info(prm, ...) iio_prm_printf((prm), LEVEL_INFO, __VA_ARGS__)
#define prm_dbg(prm, ...) iio_prm_printf((prm), LEVEL_DEBUG, "DEBUG: " __VA_ARGS__)
#define prm_err(prm, ...) \
do { \
if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_ERROR) \
iio_prm_printf((prm), LEVEL_ERROR, "ERROR: " __VA_ARGS__); \
} while (0)
#define prm_warn(prm, ...) \
do { \
if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_WARNING) \
iio_prm_printf((prm), LEVEL_WARNING, "WARNING: " __VA_ARGS__); \
} while (0)
#define prm_info(prm, ...) \
do { \
if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_INFO) \
iio_prm_printf((prm), LEVEL_INFO, __VA_ARGS__); \
} while (0)
#define prm_dbg(prm, ...) \
do { \
if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_DEBUG) \
iio_prm_printf((prm), LEVEL_DEBUG, "DEBUG: "__VA_ARGS__); \
} while (0)

#define ctx_err(ctx, ...) prm_err(__ctx_params_or_null(ctx), __VA_ARGS__)
#define ctx_warn(ctx, ...) prm_warn(__ctx_params_or_null(ctx), __VA_ARGS__)
Expand Down Expand Up @@ -89,11 +112,13 @@ iio_prm_printf(const struct iio_context_params *params,
__SKIPFIRST(__VA_ARGS__, ""))

#define prm_perror(params, err, ...) do { \
char _buf[1024]; \
int _err = -(err); \
iio_strerror(_err, _buf, sizeof(_buf)); \
prm_err(params, __FIRST(__VA_ARGS__, 0) \
__OTHERS(": %s\n",__VA_ARGS__, _buf)); \
if (LIBIIO_MAX_LOG_LEVEL >= LEVEL_ERROR) { \
char _buf[1024]; \
int _err = -(err); \
iio_strerror(_err, _buf, sizeof(_buf)); \
prm_err(params, __FIRST(__VA_ARGS__, 0) \
__OTHERS(": %s\n",__VA_ARGS__, _buf)); \
} \
} while (0)
#define ctx_perror(ctx, err, ...) prm_perror(__ctx_params_or_null(ctx), err, __VA_ARGS__)
#define dev_perror(dev, err, ...) ctx_perror(__dev_ctx_or_null(dev), err, \
Expand Down

0 comments on commit cc65317

Please sign in to comment.