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; } }