diff options
author | Thomas White <taw@physics.org> | 2020-03-18 12:01:18 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2020-07-29 18:42:57 +0200 |
commit | f5fca86eb191f41dfc45cf5c7f17e9ffd73d0287 (patch) | |
tree | be7773b16ed0d2708931a3c388bff34b3c9d996c /libcrystfel/src/utils.c | |
parent | 3022757e31b93542f332bcc1c323f63a853b7912 (diff) |
libcrystfel plumbing to allow ERROR/STATUS messages in GUI
Diffstat (limited to 'libcrystfel/src/utils.c')
-rw-r--r-- | libcrystfel/src/utils.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/libcrystfel/src/utils.c b/libcrystfel/src/utils.c index 0c01c069..694e76bc 100644 --- a/libcrystfel/src/utils.c +++ b/libcrystfel/src/utils.c @@ -720,3 +720,49 @@ char *load_entire_file(const char *filename) return contents; } + + +/* ------------------------------ 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); +} |