From 3674c67e06e47ebabe42f9277b7e9212e6a2f87a Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 18 Mar 2020 14:19:19 +0100 Subject: Add void pointer to log message functions Also add LogMsgFunc typedef and move functions up in utils.c, because progress_bar() also needs the stderr_lock. --- libcrystfel/src/utils.c | 95 ++++++++++++++++++++++++++----------------------- libcrystfel/src/utils.h | 5 ++- src/crystfel_gui.c | 5 +-- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/libcrystfel/src/utils.c b/libcrystfel/src/utils.c index 694e76bc..afbcbf78 100644 --- a/libcrystfel/src/utils.c +++ b/libcrystfel/src/utils.c @@ -251,6 +251,57 @@ gsl_vector *solve_svd(gsl_vector *v, gsl_matrix *M, int *pn_filt, int verbose) } +/* ------------------------------ Message logging ---------------------------- */ + +/* Lock to keep lines serialised on the terminal */ +pthread_mutex_t stderr_lock = PTHREAD_MUTEX_INITIALIZER; + + +static void log_to_stderr(enum log_msg_type type, const char *msg, + void *vp) +{ + int error_print_val = get_status_label(); + pthread_mutex_lock(&stderr_lock); + if ( error_print_val >= 0 ) { + fprintf(stderr, "%3i: ", error_print_val); + } + fprintf(stderr, "%s", msg); + pthread_mutex_unlock(&stderr_lock); +} + + +/* Function to call with ERROR/STATUS messages */ +LogMsgFunc log_msg_func = log_to_stderr; +void *log_msg_vp = NULL; + + +void set_log_message_func(LogMsgFunc new_log_msg_func, void *vp) +{ + log_msg_func = new_log_msg_func; + log_msg_vp = vp; +} + + +void STATUS(const char *format, ...) +{ + va_list args; + char tmp[1024]; + vsnprintf(tmp, 1024, format, args); + log_msg_func(LOG_MSG_STATUS, tmp, log_msg_vp); +} + + +void ERROR(const char *format, ...) +{ + va_list args; + char tmp[1024]; + vsnprintf(tmp, 1024, format, args); + log_msg_func(LOG_MSG_ERROR, tmp, log_msg_vp); +} + + +/* ------------------------------ Useful functions ---------------------------- */ + size_t notrail(char *s) { ssize_t i; @@ -722,47 +773,3 @@ char *load_entire_file(const char *filename) } -/* ------------------------------ Message logging ---------------------------- */ - -/* Lock to keep lines serialised on the terminal */ -pthread_mutex_t stderr_lock = PTHREAD_MUTEX_INITIALIZER; - - -static void log_to_stderr(enum log_msg_type type, const char *msg) -{ - int error_print_val = get_status_label(); - pthread_mutex_lock(&stderr_lock); - if ( error_print_val >= 0 ) { - fprintf(stderr, "%3i: ", error_print_val); - } - fprintf(stderr, "%s", msg); - pthread_mutex_unlock(&stderr_lock); -} - - -/* Function to call with ERROR/STATUS messages */ -void (*log_msg_func)(enum log_msg_type type, const char *) = log_to_stderr; - - -void set_log_message_func(void (*new_log_msg_func)(enum log_msg_type type, const char *)) -{ - log_msg_func = new_log_msg_func; -} - - -void STATUS(const char *format, ...) -{ - va_list args; - char tmp[1024]; - vsnprintf(tmp, 1024, format, args); - log_msg_func(LOG_MSG_STATUS, tmp); -} - - -void ERROR(const char *format, ...) -{ - va_list args; - char tmp[1024]; - vsnprintf(tmp, 1024, format, args); - log_msg_func(LOG_MSG_ERROR, tmp); -} diff --git a/libcrystfel/src/utils.h b/libcrystfel/src/utils.h index b5f07f26..39ee8848 100644 --- a/libcrystfel/src/utils.h +++ b/libcrystfel/src/utils.h @@ -206,7 +206,10 @@ enum log_msg_type { extern void STATUS(const char *format, ...); extern void ERROR(const char *format, ...); -extern void set_log_message_func(void (*new_log_msg_func)(enum log_msg_type type, const char *)); +typedef void (*LogMsgFunc)(enum log_msg_type type, const char *msg, void *vp); + +extern void set_log_message_func(LogMsgFunc new_log_msg_func, + void *vp); /* ------------------------------ File handling ----------------------------- */ diff --git a/src/crystfel_gui.c b/src/crystfel_gui.c index a92f2c2c..87a34936 100644 --- a/src/crystfel_gui.c +++ b/src/crystfel_gui.c @@ -537,7 +537,8 @@ static void add_task_buttons(GtkWidget *vbox, struct crystfelproject *proj) } -static void add_gui_message(enum log_msg_type type, const char *msg) +static void add_gui_message(enum log_msg_type type, const char *msg, + void *vp) { printf("message '%s'\n", msg); } @@ -702,7 +703,7 @@ int main(int argc, char *argv[]) gtk_paned_pack2(GTK_PANED(vpaned), GTK_WIDGET(frame), FALSE, FALSE); /* Send messages to report region */ - set_log_message_func(add_gui_message); + set_log_message_func(add_gui_message, &proj); gtk_window_set_default_size(GTK_WINDOW(proj.window), 1024, 768); gtk_paned_set_position(GTK_PANED(hpaned), 172); -- cgit v1.2.3