diff --git a/doc/subsystems/logging/logger.rst b/doc/subsystems/logging/logger.rst index c2c6be13abc..b1431cc19bf 100644 --- a/doc/subsystems/logging/logger.rst +++ b/doc/subsystems/logging/logger.rst @@ -100,7 +100,17 @@ it is not set or set lower than the override value. :option:`CONFIG_LOG_MAX_LEVEL`: Maximal (lowest severity) level which is compiled in. -:option:`CONFIG_LOG_FUNCTION_NAME`: Prepend log message with function name. +:option:`CONFIG_LOG_FUNC_NAME_PREFIX_ERR`: Prepend ERROR log messages with +function name. + +:option:`CONFIG_LOG_FUNC_NAME_PREFIX_WRN`: Prepend WARNING log messages with +function name. + +:option:`CONFIG_LOG_FUNC_NAME_PREFIX_INF`: Prepend INFO log messages with +function name. + +:option:`CONFIG_LOG_FUNC_NAME_PREFIX_DBG`: Prepend DEBUG log messages with +function name. :option:`CONFIG_LOG_PRINTK`: Redirect printk calls to the logger. diff --git a/include/logging/log.h b/include/logging/log.h index 85d76d95577..657da82f284 100644 --- a/include/logging/log.h +++ b/include/logging/log.h @@ -28,11 +28,11 @@ extern "C" { * @{ */ -#define LOG_LEVEL_NONE 0 -#define LOG_LEVEL_ERR 1 -#define LOG_LEVEL_WRN 2 -#define LOG_LEVEL_INF 3 -#define LOG_LEVEL_DBG 4 +#define LOG_LEVEL_NONE 0 +#define LOG_LEVEL_ERR 1 +#define LOG_LEVEL_WRN 2 +#define LOG_LEVEL_INF 3 +#define LOG_LEVEL_DBG 4 /** * @brief Writes an ERROR level message to the log. diff --git a/include/logging/log_core.h b/include/logging/log_core.h index 5c509c5d9b8..d78b91f0206 100644 --- a/include/logging/log_core.h +++ b/include/logging/log_core.h @@ -25,6 +25,12 @@ extern "C" { #define CONFIG_LOG_MAX_LEVEL 0 #endif +#define LOG_FUNCTION_PREFIX_MASK \ + ((IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_ERR) << LOG_LEVEL_ERR) | \ + (IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_WRN) << LOG_LEVEL_WRN) | \ + (IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_INF) << LOG_LEVEL_INF) | \ + (IS_ENABLED(CONFIG_LOG_FUNC_NAME_PREFIX_DBG) << LOG_LEVEL_DBG)) + /** @brief Macro for returning local level value if defined or default. * * Check @ref IS_ENABLED macro for detailed explanation of the trick. @@ -158,15 +164,13 @@ extern "C" { * argument. In order to handle string with no arguments _LOG_Z_EVAL is * used. */ -#if CONFIG_LOG_FUNCTION_NAME + #define _LOG_STR(...) "%s: " __LOG_ARG_1(__VA_ARGS__), __func__\ _LOG_Z_EVAL(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\ (),\ (, __LOG_ARGS_LESS1(__VA_ARGS__))\ ) -#else -#define _LOG_STR(...) __VA_ARGS__ -#endif + /******************************************************************************/ /****************** Internal macros for log frontend **************************/ @@ -243,7 +247,13 @@ extern "C" { .domain_id = CONFIG_LOG_DOMAIN_ID, \ .source_id = _id \ }; \ - __LOG_INTERNAL(src_level, _LOG_STR(__VA_ARGS__)); \ + \ + if ((1 << _level) & LOG_FUNCTION_PREFIX_MASK) { \ + __LOG_INTERNAL(src_level, \ + _LOG_STR(__VA_ARGS__)); \ + } else { \ + __LOG_INTERNAL(src_level, __VA_ARGS__); \ + } \ } else if (0) { \ /* Arguments checker present but never evaluated.*/ \ /* Placed here to ensure that __VA_ARGS__ are*/ \ diff --git a/subsys/logging/Kconfig b/subsys/logging/Kconfig index 21780c928d5..52e2cef42c0 100644 --- a/subsys/logging/Kconfig +++ b/subsys/logging/Kconfig @@ -183,11 +183,22 @@ config LOG_MAX_LEVEL - 3 INFO, maximal level set to LOG_LEVEL_INFO - 4 DEBUG, maximal level set to LOG_LEVEL_DBG -config LOG_FUNCTION_NAME - bool "Prepend log message with function name" +menu "Prepend log message with function name" + +config LOG_FUNC_NAME_PREFIX_ERR + bool "Error messages prepended" + +config LOG_FUNC_NAME_PREFIX_WRN + bool "Warning messages prepended" + +config LOG_FUNC_NAME_PREFIX_INF + bool "Info messages prepended" + +config LOG_FUNC_NAME_PREFIX_DBG + bool "Debug messages prepended" default y - help - Prepends log message with function name. + +endmenu config LOG_PRINTK bool "Enable processing of printk messages." diff --git a/subsys/logging/log_output.c b/subsys/logging/log_output.c index f72c1e7fe7c..96f218f4b8d 100644 --- a/subsys/logging/log_output.c +++ b/subsys/logging/log_output.c @@ -225,8 +225,8 @@ static void color_postfix(struct log_msg *msg, } -static int ids_print(struct log_msg *msg, - const struct log_output *log_output, bool level_on) +static int ids_print(struct log_msg *msg, const struct log_output *log_output, + bool level_on, bool func_on) { u32_t domain_id = log_msg_domain_id_get(msg); u32_t source_id = log_msg_source_id_get(msg); @@ -238,7 +238,8 @@ static int ids_print(struct log_msg *msg, } total += print_formatted(log_output, - IS_ENABLED(CONFIG_LOG_FUNCTION_NAME) ? + (func_on && + ((1 << level) & LOG_FUNCTION_PREFIX_MASK)) ? "%s." : "%s: ", log_source_name_get(domain_id, source_id)); @@ -445,7 +446,7 @@ static void raw_string_print(struct log_msg *msg, static int prefix_print(struct log_msg *msg, const struct log_output *log_output, - u32_t flags) + u32_t flags, bool func_on) { int length = 0; @@ -485,7 +486,7 @@ static int prefix_print(struct log_msg *msg, } else { color_prefix(msg, log_output, colors_on); - length += ids_print(msg, log_output, level_on); + length += ids_print(msg, log_output, level_on, func_on); } } @@ -507,7 +508,8 @@ void log_output_msg_process(const struct log_output *log_output, struct log_msg *msg, u32_t flags) { - int prefix_offset = prefix_print(msg, log_output, flags); + int prefix_offset = prefix_print(msg, log_output, flags, + log_msg_is_std(msg)); if (log_msg_is_std(msg)) { std_print(msg, log_output); diff --git a/tests/subsys/logging/log_core/prj.conf b/tests/subsys/logging/log_core/prj.conf index f13ed327b9c..5f4686b58a1 100644 --- a/tests/subsys/logging/log_core/prj.conf +++ b/tests/subsys/logging/log_core/prj.conf @@ -11,5 +11,5 @@ CONFIG_LOG_STRDUP_MAX_STRING=8 CONFIG_KERNEL_LOG_LEVEL_OFF=y CONFIG_SOC_LOG_LEVEL_OFF=y CONFIG_ARCH_LOG_LEVEL_OFF=y -CONFIG_LOG_FUNCTION_NAME=n +CONFIG_LOG_FUNC_NAME_PREFIX_DBG=n CONFIG_LOG_PROCESS_THREAD=n