modules: mbedtls: move debug log hook implementation to modules/mbedtls/
So far there was a debug log hook installed in TLS socket implementation.
However, mbedTLS (with debug enabled) might be used outside from TLS socket
and even outside from networking context.
Add new module, which implements debug log hook and makes it available
whenever CONFIG_MBEDTLS_DEBUG is enabled.
Note that debug hook needs to be installed for each mbedTLS context
separately, which means that this requires action from mbedTLS users, such
as TLS sockets implementation.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
diff --git a/modules/mbedtls/debug.c b/modules/mbedtls/debug.c
new file mode 100644
index 0000000..3a1a478
--- /dev/null
+++ b/modules/mbedtls/debug.c
@@ -0,0 +1,31 @@
+/*
+ * 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;
+
+ ARG_UNUSED(ctx);
+
+ if (!file || !str) {
+ return;
+ }
+
+ /* Extract basename from file */
+ for (p = basename = file; *p != '\0'; p++) {
+ if (*p == '/' || *p == '\\') {
+ basename = p + 1;
+ }
+ }
+
+ LOG_DBG("%s:%04d: |%d| %s", basename, line, level, str);
+}