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 | |
parent | 3022757e31b93542f332bcc1c323f63a853b7912 (diff) |
libcrystfel plumbing to allow ERROR/STATUS messages in GUI
Diffstat (limited to 'libcrystfel')
-rw-r--r-- | libcrystfel/src/thread-pool.c | 1 | ||||
-rw-r--r-- | libcrystfel/src/utils.c | 46 | ||||
-rw-r--r-- | libcrystfel/src/utils.h | 36 |
3 files changed, 59 insertions, 24 deletions
diff --git a/libcrystfel/src/thread-pool.c b/libcrystfel/src/thread-pool.c index 67645b1e..1d85e0be 100644 --- a/libcrystfel/src/thread-pool.c +++ b/libcrystfel/src/thread-pool.c @@ -47,7 +47,6 @@ static int use_status_labels = 0; static pthread_key_t status_label_key; -pthread_mutex_t stderr_lock = PTHREAD_MUTEX_INITIALIZER; struct worker_args { 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); +} diff --git a/libcrystfel/src/utils.h b/libcrystfel/src/utils.h index f406ef6c..fcdde453 100644 --- a/libcrystfel/src/utils.h +++ b/libcrystfel/src/utils.h @@ -193,29 +193,19 @@ static inline int within_tolerance(double a, double b, double percent) #define ph_eV_to_k(a) ((a)*ELECTRON_CHARGE/PLANCK/C_VACUO) -/* ------------------------------ Message macros ---------------------------- */ - -extern pthread_mutex_t stderr_lock; - -#define ERROR(...) { \ - 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, __VA_ARGS__); \ - pthread_mutex_unlock(&stderr_lock); \ - } - -#define STATUS(...) { \ - int status_print_val = get_status_label(); \ - pthread_mutex_lock(&stderr_lock); \ - if ( status_print_val >= 0 ) { \ - fprintf(stderr, "%3i: ", status_print_val); \ - } \ - fprintf(stderr, __VA_ARGS__); \ - pthread_mutex_unlock(&stderr_lock); \ - } +/* ------------------------------ Message logging ---------------------------- */ + + +enum log_msg_type { + LOG_MSG_STATUS, + LOG_MSG_ERROR +}; + + +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 *)); /* ------------------------------ File handling ----------------------------- */ |