zephyr/modules/mbedtls/debug.c

58 lines
1.2 KiB
C
Raw Normal View History

/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2022 Marcin Niestroj
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mbedtls, CONFIG_MBEDTLS_LOG_LEVEL);
#include "zephyr_mbedtls_priv.h"
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);
if (!file || !str) {
return;
}
/* Extract basename from file */
if (IS_ENABLED(CONFIG_MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME)) {
for (p = basename = file; *p != '\0'; p++) {
if (*p == '/' || *p == '\\') {
basename = p + 1;
}
}
}
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--;
}
}
modules: mbedtls: convert mbedTLS log levels to Zephyr log levels So far LOG_DBG() was used inside debug hook for mbedTLS library. This meant that it was hard to distinct log messages by simply looking at the log level number, even though Zephyr logging subsystem supports colorful logs depending on log level. Choose an appropriate Zephyr LOG_*() macro based on log level coming from mbedTLS library. Remove log level number from formatted log messages, as it is now redundant. One controversial thing about this change is that mbedTLS' "2 State change" log level is mapped to Zephyr's "warning" log level. Those are not really warnings in real life, but rather informational messages. However, using "warning" log level for those allows to clearly distinguish between "2 State change" and "3 Informational" debug messages from mbedTLS. Additionally, mbedTLS debug message implementation does not seem to be safe to use in production, so keeping in mind MBEDTLS_DEBUG will be enabled just during debugging phase, printing "2 State change" logs as warnings should not be a big deal. Set default MBEDTLS_DEBUG_LEVEL value depending on selected Zephyr logging module level, so that only single option needs to be configured in application project. Remove prompt for MBEDTLS_DEBUG_LEVEL, so that interactively (e.g. via menuconfig) adjusting MBEDTLS_LOG_LEVEL will always result in automatically updating MBEDTLS_DEBUG_LEVEL option. This is to prevent so called "stuck symbol syndrome". Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2022-06-15 06:32:24 +08:00
switch (level) {
case 0:
case 1:
LOG_ERR("%s:%04d: %.*s", basename, line, str_len, str);
modules: mbedtls: convert mbedTLS log levels to Zephyr log levels So far LOG_DBG() was used inside debug hook for mbedTLS library. This meant that it was hard to distinct log messages by simply looking at the log level number, even though Zephyr logging subsystem supports colorful logs depending on log level. Choose an appropriate Zephyr LOG_*() macro based on log level coming from mbedTLS library. Remove log level number from formatted log messages, as it is now redundant. One controversial thing about this change is that mbedTLS' "2 State change" log level is mapped to Zephyr's "warning" log level. Those are not really warnings in real life, but rather informational messages. However, using "warning" log level for those allows to clearly distinguish between "2 State change" and "3 Informational" debug messages from mbedTLS. Additionally, mbedTLS debug message implementation does not seem to be safe to use in production, so keeping in mind MBEDTLS_DEBUG will be enabled just during debugging phase, printing "2 State change" logs as warnings should not be a big deal. Set default MBEDTLS_DEBUG_LEVEL value depending on selected Zephyr logging module level, so that only single option needs to be configured in application project. Remove prompt for MBEDTLS_DEBUG_LEVEL, so that interactively (e.g. via menuconfig) adjusting MBEDTLS_LOG_LEVEL will always result in automatically updating MBEDTLS_DEBUG_LEVEL option. This is to prevent so called "stuck symbol syndrome". Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2022-06-15 06:32:24 +08:00
break;
case 2:
LOG_WRN("%s:%04d: %.*s", basename, line, str_len, str);
modules: mbedtls: convert mbedTLS log levels to Zephyr log levels So far LOG_DBG() was used inside debug hook for mbedTLS library. This meant that it was hard to distinct log messages by simply looking at the log level number, even though Zephyr logging subsystem supports colorful logs depending on log level. Choose an appropriate Zephyr LOG_*() macro based on log level coming from mbedTLS library. Remove log level number from formatted log messages, as it is now redundant. One controversial thing about this change is that mbedTLS' "2 State change" log level is mapped to Zephyr's "warning" log level. Those are not really warnings in real life, but rather informational messages. However, using "warning" log level for those allows to clearly distinguish between "2 State change" and "3 Informational" debug messages from mbedTLS. Additionally, mbedTLS debug message implementation does not seem to be safe to use in production, so keeping in mind MBEDTLS_DEBUG will be enabled just during debugging phase, printing "2 State change" logs as warnings should not be a big deal. Set default MBEDTLS_DEBUG_LEVEL value depending on selected Zephyr logging module level, so that only single option needs to be configured in application project. Remove prompt for MBEDTLS_DEBUG_LEVEL, so that interactively (e.g. via menuconfig) adjusting MBEDTLS_LOG_LEVEL will always result in automatically updating MBEDTLS_DEBUG_LEVEL option. This is to prevent so called "stuck symbol syndrome". Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2022-06-15 06:32:24 +08:00
break;
case 3:
LOG_INF("%s:%04d: %.*s", basename, line, str_len, str);
modules: mbedtls: convert mbedTLS log levels to Zephyr log levels So far LOG_DBG() was used inside debug hook for mbedTLS library. This meant that it was hard to distinct log messages by simply looking at the log level number, even though Zephyr logging subsystem supports colorful logs depending on log level. Choose an appropriate Zephyr LOG_*() macro based on log level coming from mbedTLS library. Remove log level number from formatted log messages, as it is now redundant. One controversial thing about this change is that mbedTLS' "2 State change" log level is mapped to Zephyr's "warning" log level. Those are not really warnings in real life, but rather informational messages. However, using "warning" log level for those allows to clearly distinguish between "2 State change" and "3 Informational" debug messages from mbedTLS. Additionally, mbedTLS debug message implementation does not seem to be safe to use in production, so keeping in mind MBEDTLS_DEBUG will be enabled just during debugging phase, printing "2 State change" logs as warnings should not be a big deal. Set default MBEDTLS_DEBUG_LEVEL value depending on selected Zephyr logging module level, so that only single option needs to be configured in application project. Remove prompt for MBEDTLS_DEBUG_LEVEL, so that interactively (e.g. via menuconfig) adjusting MBEDTLS_LOG_LEVEL will always result in automatically updating MBEDTLS_DEBUG_LEVEL option. This is to prevent so called "stuck symbol syndrome". Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2022-06-15 06:32:24 +08:00
break;
default:
LOG_DBG("%s:%04d: %.*s", basename, line, str_len, str);
modules: mbedtls: convert mbedTLS log levels to Zephyr log levels So far LOG_DBG() was used inside debug hook for mbedTLS library. This meant that it was hard to distinct log messages by simply looking at the log level number, even though Zephyr logging subsystem supports colorful logs depending on log level. Choose an appropriate Zephyr LOG_*() macro based on log level coming from mbedTLS library. Remove log level number from formatted log messages, as it is now redundant. One controversial thing about this change is that mbedTLS' "2 State change" log level is mapped to Zephyr's "warning" log level. Those are not really warnings in real life, but rather informational messages. However, using "warning" log level for those allows to clearly distinguish between "2 State change" and "3 Informational" debug messages from mbedTLS. Additionally, mbedTLS debug message implementation does not seem to be safe to use in production, so keeping in mind MBEDTLS_DEBUG will be enabled just during debugging phase, printing "2 State change" logs as warnings should not be a big deal. Set default MBEDTLS_DEBUG_LEVEL value depending on selected Zephyr logging module level, so that only single option needs to be configured in application project. Remove prompt for MBEDTLS_DEBUG_LEVEL, so that interactively (e.g. via menuconfig) adjusting MBEDTLS_LOG_LEVEL will always result in automatically updating MBEDTLS_DEBUG_LEVEL option. This is to prevent so called "stuck symbol syndrome". Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2022-06-15 06:32:24 +08:00
break;
}
}