aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-03-18 12:01:18 +0100
committerThomas White <taw@physics.org>2020-07-29 18:42:57 +0200
commitf5fca86eb191f41dfc45cf5c7f17e9ffd73d0287 (patch)
treebe7773b16ed0d2708931a3c388bff34b3c9d996c
parent3022757e31b93542f332bcc1c323f63a853b7912 (diff)
libcrystfel plumbing to allow ERROR/STATUS messages in GUI
-rw-r--r--libcrystfel/src/thread-pool.c1
-rw-r--r--libcrystfel/src/utils.c46
-rw-r--r--libcrystfel/src/utils.h36
-rw-r--r--src/crystfel_gui.c9
4 files changed, 68 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 ----------------------------- */
diff --git a/src/crystfel_gui.c b/src/crystfel_gui.c
index ab9ce0e2..a92f2c2c 100644
--- a/src/crystfel_gui.c
+++ b/src/crystfel_gui.c
@@ -537,6 +537,12 @@ static void add_task_buttons(GtkWidget *vbox, struct crystfelproject *proj)
}
+static void add_gui_message(enum log_msg_type type, const char *msg)
+{
+ printf("message '%s'\n", msg);
+}
+
+
int main(int argc, char *argv[])
{
int c;
@@ -695,6 +701,9 @@ int main(int argc, char *argv[])
gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(proj.report));
gtk_paned_pack2(GTK_PANED(vpaned), GTK_WIDGET(frame), FALSE, FALSE);
+ /* Send messages to report region */
+ set_log_message_func(add_gui_message);
+
gtk_window_set_default_size(GTK_WINDOW(proj.window), 1024, 768);
gtk_paned_set_position(GTK_PANED(hpaned), 172);
gtk_paned_set_position(GTK_PANED(vpaned), 600);