From 2be279e67cc5571e6ccd4e46a130889e2e93f556 Mon Sep 17 00:00:00 2001 From: wanges Date: Thu, 22 Feb 2024 18:42:50 +0800 Subject: [PATCH] [MXCHIP] mbedtls log configured & output by nanomq log --- include/nng/supplemental/nanolib/log.h | 3 ++ src/sp/protocol/mqtt/nmq_mqtt.c | 1 + src/supplemental/nanolib/log.c | 8 ++++ src/supplemental/tls/mbedtls/tls.c | 63 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/include/nng/supplemental/nanolib/log.h b/include/nng/supplemental/nanolib/log.h index 2800c6b44..1686de07c 100644 --- a/include/nng/supplemental/nanolib/log.h +++ b/include/nng/supplemental/nanolib/log.h @@ -63,6 +63,9 @@ enum { NNG_DECL const char *log_level_string(int level); NNG_DECL int log_level_num(const char *level); NNG_DECL void log_set_level(int level); +#ifdef CONFIG_MXCHIP_DEBUG +NNG_DECL int log_get_level(void); +#endif NNG_DECL int log_add_callback( log_func fn, void *udata, int level, void *mtx, conf_log *config); NNG_DECL void log_add_console(int level, void *mtx); diff --git a/src/sp/protocol/mqtt/nmq_mqtt.c b/src/sp/protocol/mqtt/nmq_mqtt.c index 4fa4bb0cf..ce9c36c28 100644 --- a/src/sp/protocol/mqtt/nmq_mqtt.c +++ b/src/sp/protocol/mqtt/nmq_mqtt.c @@ -881,6 +881,7 @@ nano_pipe_close(void *arg) // create disconnect event msg #ifdef CONFIG_MXCHIP send_disconnect_msg: + log_debug("-----> nano_msg_notify_disconnect(%s).", p->event ? "yes" : "no"); #endif if (p->event) { msg = diff --git a/src/supplemental/nanolib/log.c b/src/supplemental/nanolib/log.c index 6e01e541b..559f66667 100644 --- a/src/supplemental/nanolib/log.c +++ b/src/supplemental/nanolib/log.c @@ -172,6 +172,14 @@ log_set_level(int level) L.level = level; } +#ifdef CONFIG_MXCHIP_DEBUG +int +log_get_level(void) +{ + return L.level; +} +#endif + int log_add_callback( log_func fn, void *udata, int level, void *mtx, conf_log *config) diff --git a/src/supplemental/tls/mbedtls/tls.c b/src/supplemental/tls/mbedtls/tls.c index 03fd231bc..0fc206c92 100644 --- a/src/supplemental/tls/mbedtls/tls.c +++ b/src/supplemental/tls/mbedtls/tls.c @@ -61,11 +61,43 @@ struct nng_tls_engine_config { static void tls_dbg(void *ctx, int level, const char *file, int line, const char *s) { +#ifdef CONFIG_MXCHIP_DEBUG + char buf[256]; + int nanomq_log_level; +#else char buf[128]; +#endif NNI_ARG_UNUSED(ctx); NNI_ARG_UNUSED(level); snprintf(buf, sizeof(buf), "%s:%04d: %s", file, line, s); + +#ifdef CONFIG_MXCHIP_DEBUG + /* mbedtls log level to nanomq log level */ + switch(level) { + case 0: // note: no log, // should not happen + nanomq_log_level = NNG_LOG_FATAL; + break; + case 1: + nanomq_log_level = NNG_LOG_ERROR; + break; + case 2: + nanomq_log_level = NNG_LOG_INFO; + break; + case 3: + nanomq_log_level = NNG_LOG_DEBUG; + break; + case 4: + nanomq_log_level = NNG_LOG_TRACE; + break; + default: + nanomq_log_level = NNG_LOG_WARN; // should not happen + break; + } + /* mbedtls log output to nanomq log */ + log_log(nanomq_log_level, "tls.c", __LINE__, __FUNCTION__, "%s", buf); +#else nni_plat_println(buf); +#endif } static int @@ -622,6 +654,9 @@ int nng_tls_engine_init_mbed(void) { int rv; +#ifdef CONFIG_MXCHIP_DEBUG + int log_level; +#endif #ifdef NNG_TLS_USE_CTR_DRBG nni_mtx_init(&rng_lock); @@ -633,9 +668,37 @@ nng_tls_engine_init_mbed(void) return (rv); } #endif + +#ifdef CONFIG_MXCHIP_DEBUG + // mbedtls log level read from nanomq conf + log_level = log_get_level(); + switch(log_level) { + case NNG_LOG_FATAL: + case NNG_LOG_ERROR: + case NNG_LOG_WARN: + log_level = 1; // Error + break; + case NNG_LOG_INFO: + log_level = 2; // State change + break; + case NNG_LOG_DEBUG: + log_level = 3: // Informational + break; + case NNG_LOG_TRACE: + log_level = 4; // Verbose + break; + default: + log_level = 0; // No debug + break; + } + printf("mbedtls log level: %d\n", log_level); + mbedtls_debug_set_threshold(log_level); + +#else // Uncomment the following to have noisy debug from mbedTLS. // This may be useful when trying to debug failures. // mbedtls_debug_set_threshold(3); +#endif rv = nng_tls_engine_register(&tls_engine_mbed);