From fa5937aaed0268286faecf193ebdaa862118f207 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Wed, 15 Jun 2022 00:22:03 +0200 Subject: [PATCH] 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 --- modules/mbedtls/Kconfig | 7 +++++++ modules/mbedtls/debug.c | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/mbedtls/Kconfig b/modules/mbedtls/Kconfig index 9474941cbf3..ced8519ba2d 100644 --- a/modules/mbedtls/Kconfig +++ b/modules/mbedtls/Kconfig @@ -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 diff --git a/modules/mbedtls/debug.c b/modules/mbedtls/debug.c index 170400264af..69bafc9c6d6 100644 --- a/modules/mbedtls/debug.c +++ b/modules/mbedtls/debug.c @@ -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; } }