Skip to content

Commit

Permalink
[MXCHIP] mbedtls log config & output by nanomq log
Browse files Browse the repository at this point in the history
  • Loading branch information
mxchipwanges committed Feb 23, 2024
1 parent e7d5275 commit 2bb5d3d
Show file tree
Hide file tree
Showing 5 changed files with 99 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
1 change: 1 addition & 0 deletions src/supplemental/nanolib/conf_ver2.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ conf_log_parse_ver2(conf *config, cJSON *jso)
int rv = log_level_num(cJSON_GetStringValue(jso_log_level));
if (-1 != rv) {
log->level = rv;
printf("*** conf log level %d\n", log->level);
} else {
log->level = NNG_LOG_ERROR;
}
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
86 changes: 86 additions & 0 deletions src/supplemental/tls/mbedtls/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,93 @@ struct nng_tls_engine_config {
nni_list pairs;
};

#ifdef CONFIG_MXCHIP_DEBUG
static int
nanomq_log_level_to_mbedtls(int log_level)
{
int mbedtls_log_level;

switch(log_level) {
case NNG_LOG_FATAL:
case NNG_LOG_ERROR:
case NNG_LOG_WARN:
mbedtls_log_level = 1; // Error
break;
case NNG_LOG_INFO:
mbedtls_log_level = 2; // State change
break;
case NNG_LOG_DEBUG:
mbedtls_log_level = 3; // Informational
break;
case NNG_LOG_TRACE:
mbedtls_log_level = 4; // Verbose
break;
default:
mbedtls_log_level = 0; // No debug
break;
}

// log_info("nanomq log level(%d -> %d)mbedtls log.", log_level, mbedtls_log_level);
return mbedtls_log_level;
}

static int
mbedtls_log_level_to_nanomq(int log_level)
{
int nanomq_log_level;

/* mbedtls log level to nanomq log level */
switch(log_level) {
case 0: // note: no log, // should not happen
nanomq_log_level = NNG_LOG_FATAL;
break;
case 1: // Error
nanomq_log_level = NNG_LOG_ERROR;
break;
case 2: // State change
nanomq_log_level = NNG_LOG_INFO;
break;
case 3: // Informational
nanomq_log_level = NNG_LOG_DEBUG;
break;
case 4: // Verbose
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_info("**** mbedtls log(%d -> %d)nanomq log", log_level, nanomq_log_level);
return nanomq_log_level;
}

void mbedtls_log_level_update(int log_level)
{
mbedtls_debug_set_threshold(nanomq_log_level_to_mbedtls(log_level));
return;
}
#endif

static void
tls_dbg(void *ctx, int level, const char *file, int line, const char *s)
{
#ifdef CONFIG_MXCHIP_DEBUG
char buf[256];
#else
char buf[128];
#endif
NNI_ARG_UNUSED(ctx);
NNI_ARG_UNUSED(level);
snprintf(buf, sizeof(buf), "%s:%04d: %s", file, line, s);
// log_debug("****[mbedtls log(%d)] %s", level, buf);

#ifdef CONFIG_MXCHIP_DEBUG
log_log(mbedtls_log_level_to_nanomq(level), "tls.c", __LINE__, __FUNCTION__, "%s", buf);
#else
nni_plat_println(buf);
#endif
}

static int
Expand Down Expand Up @@ -633,9 +712,16 @@ nng_tls_engine_init_mbed(void)
return (rv);
}
#endif

#ifdef CONFIG_MXCHIP_DEBUG
// mbedtls log level read from nanomq conf
mbedtls_debug_set_threshold(nanomq_log_level_to_mbedtls(log_get_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 2bb5d3d

Please sign in to comment.