diff options
author | Thomas White <taw@physics.org> | 2020-03-18 14:19:19 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2020-07-29 18:42:57 +0200 |
commit | 3674c67e06e47ebabe42f9277b7e9212e6a2f87a (patch) | |
tree | 35dad64133b7994387001d49a618ac016f79148d /libcrystfel/src | |
parent | fd2c06ea86984102361f0ae3444c0ae8c178069a (diff) |
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.
Diffstat (limited to 'libcrystfel/src')
-rw-r--r-- | libcrystfel/src/utils.c | 95 | ||||
-rw-r--r-- | libcrystfel/src/utils.h | 5 |
2 files changed, 55 insertions, 45 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 ----------------------------- */ |