From 79fc230bfb4e26a0bc4254c8262cfad643a4ab76 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 | 28 ++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 4 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..24d30d89 100644 --- a/lib/log.h +++ b/lib/log.h @@ -71,16 +71,36 @@ extern enum metal_log_level metal_get_log_level(void); extern void metal_default_log_handler(enum metal_log_level level, const char *format, ...); +/** + * @brief used by the metal_log() macro to update the format string + * @fmt format string passed from the metal_log() macro + * + * 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. + */ +#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 */ + /** * Emit a log message if the log level permits. * * @param level Log level. * @param ... Format string and 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 ...) \ +do { \ + if (_metal.common.log_handler && level <= _metal.common.log_level) \ + _metal.common.log_handler(level, ml_fmt(fmt), ##args); \ +} while (0) + +#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) /** @} */