mbedtls_debug.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <strings.h>
  7. #include "esp_log.h"
  8. #include "mbedtls/platform.h"
  9. #include "mbedtls/debug.h"
  10. #include "mbedtls/ssl.h"
  11. #include "mbedtls/esp_debug.h"
  12. #ifdef CONFIG_MBEDTLS_DEBUG
  13. static const char *TAG = "mbedtls";
  14. static void mbedtls_esp_debug(void *ctx, int level,
  15. const char *file, int line,
  16. const char *str);
  17. void mbedtls_esp_enable_debug_log(mbedtls_ssl_config *conf, int threshold)
  18. {
  19. esp_log_level_t level = ESP_LOG_NONE;
  20. mbedtls_debug_set_threshold(threshold);
  21. mbedtls_ssl_conf_dbg(conf, mbedtls_esp_debug, NULL);
  22. switch(threshold) {
  23. case 1:
  24. level = ESP_LOG_WARN;
  25. break;
  26. case 2:
  27. level = ESP_LOG_INFO;
  28. break;
  29. case 3:
  30. level = ESP_LOG_DEBUG;
  31. break;
  32. case 4:
  33. level = ESP_LOG_VERBOSE;
  34. break;
  35. }
  36. esp_log_level_set(TAG, level);
  37. }
  38. void mbedtls_esp_disable_debug_log(mbedtls_ssl_config *conf)
  39. {
  40. mbedtls_ssl_conf_dbg(conf, NULL, NULL);
  41. }
  42. /* Default mbedtls debug function that translates mbedTLS debug output
  43. to ESP_LOGx debug output.
  44. */
  45. static void mbedtls_esp_debug(void *ctx, int level,
  46. const char *file, int line,
  47. const char *str)
  48. {
  49. char *file_sep;
  50. /* Shorten 'file' from the whole file path to just the filename
  51. This is a bit wasteful because the macros are compiled in with
  52. the full _FILE_ path in each case.
  53. */
  54. file_sep = rindex(file, '/');
  55. if(file_sep)
  56. file = file_sep+1;
  57. switch(level) {
  58. case 1:
  59. ESP_LOGW(TAG, "%s:%d %s", file, line, str);
  60. break;
  61. case 2:
  62. ESP_LOGI(TAG, "%s:%d %s", file, line, str);
  63. break;
  64. case 3:
  65. ESP_LOGD(TAG, "%s:%d %s", file, line, str);
  66. break;
  67. case 4:
  68. ESP_LOGV(TAG, "%s:%d %s", file, line, str);
  69. break;
  70. default:
  71. ESP_LOGE(TAG, "Unexpected log level %d: %s", level, str);
  72. break;
  73. }
  74. }
  75. #endif