Skip to content

Commit

Permalink
[MXCHIP] mbedtls log configured & output by nanomq log
Browse files Browse the repository at this point in the history
  • Loading branch information
mxchipwanges committed Feb 22, 2024
1 parent e7d5275 commit 2be279e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/nng/supplemental/nanolib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/sp/protocol/mqtt/nmq_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
8 changes: 8 additions & 0 deletions src/supplemental/nanolib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions src/supplemental/tls/mbedtls/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 2be279e

Please sign in to comment.