modules: mbedtls: support stripping newline from debug log messages

Debug log messages generated by mbedTLS library contain newline at the end
of log string. Remove this newline, if it exists, so that log output is
much more user friendly.

Add a dedicated Kconfig option for that, so it can be disabled on request.

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
This commit is contained in:
Marcin Niestroj 2022-06-15 00:22:03 +02:00 committed by Carles Cufí
parent af37c09440
commit fa5937aaed
2 changed files with 21 additions and 4 deletions

View file

@ -146,6 +146,13 @@ config MBEDTLS_DEBUG_EXTRACT_BASENAME_DISABLED
endchoice
config MBEDTLS_DEBUG_STRIP_NEWLINE
bool "Strip newlines"
default y
help
Attempt to strip last character from logged string when it is a
newline.
endif # MBEDTLS_DEBUG
config MBEDTLS_MEMORY_DEBUG

View file

@ -13,6 +13,7 @@ LOG_MODULE_REGISTER(mbedtls, CONFIG_MBEDTLS_LOG_LEVEL);
void zephyr_mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str)
{
const char *p, *basename = file;
int str_len;
ARG_UNUSED(ctx);
@ -29,19 +30,28 @@ void zephyr_mbedtls_debug(void *ctx, int level, const char *file, int line, cons
}
}
str_len = strlen(str);
if (IS_ENABLED(CONFIG_MBEDTLS_DEBUG_STRIP_NEWLINE)) {
/* Remove newline only when it exists */
if (str_len > 0 && str[str_len - 1] == '\n') {
str_len--;
}
}
switch (level) {
case 0:
case 1:
LOG_ERR("%s:%04d: %s", basename, line, str);
LOG_ERR("%s:%04d: %.*s", basename, line, str_len, str);
break;
case 2:
LOG_WRN("%s:%04d: %s", basename, line, str);
LOG_WRN("%s:%04d: %.*s", basename, line, str_len, str);
break;
case 3:
LOG_INF("%s:%04d: %s", basename, line, str);
LOG_INF("%s:%04d: %.*s", basename, line, str_len, str);
break;
default:
LOG_DBG("%s:%04d: %s", basename, line, str);
LOG_DBG("%s:%04d: %.*s", basename, line, str_len, str);
break;
}
}