Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
4 changes: 4 additions & 0 deletions tests/test_ch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading