Skip to content

Commit

Permalink
log: add function name, line number prefix
Browse files Browse the repository at this point in the history
Add convenience macros metal_err, metal_warn, metal_info, metal_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 <sergei.korneichuk@amd.com>
  • Loading branch information
kernelchuk authored and arnopo committed Oct 13, 2023
1 parent fb8b04f commit 37f1660
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 29 additions & 6 deletions lib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 metal_*() 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 metal_fmt(fmt) "%s:%u " fmt, __func__, __LINE__
#else /* ML_FUNC_LINE */
#define metal_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, metal_fmt(fmt), ##args); \
})

#define metal_err(fmt, args...) metal_log(METAL_LOG_ERROR, fmt, ##args)
#define metal_warn(fmt, args...) metal_log(METAL_LOG_WARNING, fmt, ##args)
#define metal_info(fmt, args...) metal_log(METAL_LOG_INFO, fmt, ##args)
#define metal_dbg(fmt, args...) metal_log(METAL_LOG_DEBUG, fmt, ##args)

/** @} */

Expand Down

0 comments on commit 37f1660

Please sign in to comment.