diff --git a/src/log.c b/src/log.c index 52dc58f4..7b2c5a01 100644 --- a/src/log.c +++ b/src/log.c @@ -40,7 +40,12 @@ */ ATOMIC_T verbose_level = 0; -void (*print_clb)(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg); +typedef void (*nc_print_clb)(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg); + +/** + * @brief libnetconf print callback, can be changed while other threads are logging + */ +ATOMIC_PTR_T print_clb; API void nc_verbosity(NC_VERB_LEVEL level) @@ -103,6 +108,7 @@ nc_log_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char char *msg; void *mem; int req_len; + nc_print_clb clb; msg = malloc(NC_MSG_SIZE); if (!msg) { @@ -130,8 +136,9 @@ nc_log_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char } } - if (print_clb) { - print_clb(session, level, msg); + clb = (nc_print_clb)ATOMIC_PTR_LOAD_RELAXED(print_clb); + if (clb) { + clb(session, level, msg); } else if (session && session->id) { fprintf(stderr, "Session %" PRIu32 " %s: %s\n", session->id, verb[level].label, msg); } else { @@ -156,5 +163,5 @@ nc_log_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char API void nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *)) { - print_clb = clb; + ATOMIC_PTR_STORE_RELAXED(print_clb, clb); } diff --git a/tests/test_ch.c b/tests/test_ch.c index f0df4a49..704c4304 100644 --- a/tests/test_ch.c +++ b/tests/test_ch.c @@ -100,8 +100,10 @@ server_thread_ssh(void *arg) /* set print clb so we get access to messages */ nc_set_print_clb_session(test_msg_callback); + pthread_mutex_lock(&msg_lock); buffer[0] = '\0'; strcpy(expected, "reconnecting in"); + pthread_mutex_unlock(&msg_lock); /* prepare data for deleting the call-home client */ ret = nc_server_config_del_ch_client("ch_ssh", &test_data->tree); @@ -443,8 +445,10 @@ server_thread_delete_while_session(void *arg) struct test_ch_data *test_data = test_ctx->test_data; nc_set_print_clb_session(test_msg_callback); + pthread_mutex_lock(&msg_lock); buffer[0] = '\0'; strcpy(expected, "thread exit"); + pthread_mutex_unlock(&msg_lock); ps = nc_ps_new(); assert_non_null(ps);