Skip to content

Commit

Permalink
log: add function name, line number prefix
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kernelchuk committed Oct 11, 2023
1 parent d7c605c commit 79fc230
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 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
28 changes: 24 additions & 4 deletions lib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/** @} */

Expand Down

0 comments on commit 79fc230

Please sign in to comment.