1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2022 Marcin Niestroj
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_REGISTER(mbedtls, CONFIG_MBEDTLS_LOG_LEVEL);
10
11 #include "zephyr_mbedtls_priv.h"
12
zephyr_mbedtls_debug(void * ctx,int level,const char * file,int line,const char * str)13 void zephyr_mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str)
14 {
15 const char *p, *basename = file;
16 int str_len;
17
18 ARG_UNUSED(ctx);
19
20 if (!file || !str) {
21 return;
22 }
23
24 /* Extract basename from file */
25 if (IS_ENABLED(CONFIG_MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME)) {
26 for (p = basename = file; *p != '\0'; p++) {
27 if (*p == '/' || *p == '\\') {
28 basename = p + 1;
29 }
30 }
31 }
32
33 str_len = strlen(str);
34
35 if (IS_ENABLED(CONFIG_MBEDTLS_DEBUG_STRIP_NEWLINE)) {
36 /* Remove newline only when it exists */
37 if (str_len > 0 && str[str_len - 1] == '\n') {
38 str_len--;
39 }
40 }
41
42 switch (level) {
43 case 0:
44 case 1:
45 LOG_ERR("%s:%04d: %.*s", basename, line, str_len, str);
46 break;
47 case 2:
48 LOG_WRN("%s:%04d: %.*s", basename, line, str_len, str);
49 break;
50 case 3:
51 LOG_INF("%s:%04d: %.*s", basename, line, str_len, str);
52 break;
53 default:
54 LOG_DBG("%s:%04d: %.*s", basename, line, str_len, str);
55 break;
56 }
57 }
58