From cc65317e85af70253a5754b9c6d36d998440e59a Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Thu, 23 Nov 2023 12:08:47 +0100 Subject: [PATCH] CMake: Add option to specify the maximum log level 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 --- CMakeLists.txt | 12 +++++++++++- iio-config.h.cmakein | 1 + include/iio/iio-debug.h | 43 ++++++++++++++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d6e78cb1..9f57a5fbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/iio-config.h.cmakein b/iio-config.h.cmakein index 6f2f84d3d..9b617105b 100644 --- a/iio-config.h.cmakein +++ b/iio-config.h.cmakein @@ -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@" diff --git a/include/iio/iio-debug.h b/include/iio/iio-debug.h index ad6aa6639..c1218fe8d 100644 --- a/include/iio/iio-debug.h +++ b/include/iio/iio-debug.h @@ -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__) @@ -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__) @@ -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, \