From 06ed5e513e77238021156c03c83a74c494b56f73 Mon Sep 17 00:00:00 2001 From: Sergei Korneichuk Date: Thu, 28 Sep 2023 21:34:20 -0700 Subject: [PATCH] log: add function name, line number prefix Use do-while(0) to define the metal_log macro. Add convenience macros ml_err, ml_warn, ml_info, ml_dbg to avoid using excessively long and redundant "metal_log(METAL_LOG_*". Add "function-name:line-number" prefix to all messages if the option WITH_FUNC_LINE_LOG is set ON during the configuration phase. Signed-off-by: Sergei Korneichuk --- cmake/options.cmake | 1 + lib/CMakeLists.txt | 4 ++++ lib/log.h | 35 +++++++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/cmake/options.cmake b/cmake/options.cmake index a7b4ef8b..7c3d9edc 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -69,6 +69,7 @@ if (WITH_ZEPHYR) endif (WITH_ZEPHYR) option (WITH_DEFAULT_LOGGER "Build with default logger" ON) +option (WITH_FUNC_LINE_LOG "Log with function name, line number prefix" OFF) option (WITH_DOC "Build with documentation" ON) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e9d52767..00055392 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -65,6 +65,10 @@ if (WITH_DEFAULT_LOGGER) add_definitions (-DDEFAULT_LOGGER_ON) endif (WITH_DEFAULT_LOGGER) +if (WITH_FUNC_LINE_LOG) + add_definitions (-DML_FUNC_LINE) +endif (WITH_FUNC_LINE_LOG) + get_property (_ec_flgs GLOBAL PROPERTY "PROJECT_EC_FLAGS") if (WITH_ZEPHYR) diff --git a/lib/log.h b/lib/log.h index c1c91e58..df5d555d 100644 --- a/lib/log.h +++ b/lib/log.h @@ -72,15 +72,38 @@ extern void metal_default_log_handler(enum metal_log_level level, const char *format, ...); /** - * Emit a log message if the log level permits. + * @internal + * + * @brief used by the metal_log() macro to update the format string + * + * If ML_FUNC_LINE is defined this macro generates a unified format + * string for metal_log() and its convenience ml_*() macros, i.e. it + * adds function-name:line-number prefix to all log messages. + * + * @param[in] fmt format string passed from the metal_log() macro + */ +#if defined(ML_FUNC_LINE) +#define ml_fmt(fmt) "%s:%u " fmt, __func__, __LINE__ +#else /* ML_FUNC_LINE */ +#define ml_fmt(fmt) fmt +#endif /* ML_FUNC_LINE */ + +/** + * @brief Emit a log message if the log level permits. * * @param level Log level. - * @param ... Format string and arguments. + * @param fmt Format string. + * @param args... Variable number of arguments. */ -#define metal_log(level, ...) \ - ((level <= _metal.common.log_level && _metal.common.log_handler) \ - ? (void)_metal.common.log_handler(level, __VA_ARGS__) \ - : (void)0) +#define metal_log(level, fmt, args...) ({ \ + if (_metal.common.log_handler && level <= _metal.common.log_level) \ + _metal.common.log_handler(level, ml_fmt(fmt), ##args); \ +}) + +#define ml_err(fmt, args...) metal_log(METAL_LOG_ERROR, fmt, ##args) +#define ml_warn(fmt, args...) metal_log(METAL_LOG_WARNING, fmt, ##args) +#define ml_info(fmt, args...) metal_log(METAL_LOG_INFO, fmt, ##args) +#define ml_dbg(fmt, args...) metal_log(METAL_LOG_DEBUG, fmt, ##args) /** @} */