Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: add function name, line number prefix #254

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*/
kernelchuk marked this conversation as resolved.
Show resolved Hide resolved
#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.
*/
kernelchuk marked this conversation as resolved.
Show resolved Hide resolved
#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