logging: severity-wise filtering of function name prefix presence

Extended logger configuration to allow function name prefix for
messages with certain severity levels. By default only debug
messages are prefixed with function name.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-11-08 12:02:02 +01:00 committed by Carles Cufí
commit 0899a6c0b7
6 changed files with 55 additions and 22 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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*/ \

View file

@ -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."

View file

@ -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);

View file

@ -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