From d24a96f6f100a63694c22ff8ebc41a871dfd7ff5 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 16 Jul 2022 10:10:24 +0300 Subject: [PATCH 45/45] gtk: Add g_log callbacks based on freelog See osdn #44995 Signed-off-by: Marko Lindqvist --- client/gui-gtk-3.22/gui_main.c | 79 +++++++++++++++++++++++++++++++++- client/gui-gtk-4.0/gui_main.c | 75 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 2 deletions(-) diff --git a/client/gui-gtk-3.22/gui_main.c b/client/gui-gtk-3.22/gui_main.c index 1d2b8541e6..9e5e9d7892 100644 --- a/client/gui-gtk-3.22/gui_main.c +++ b/client/gui-gtk-3.22/gui_main.c @@ -190,6 +190,8 @@ static bool client_focus = TRUE; static struct video_mode vmode = { -1, -1 }; +static void set_g_log_callbacks(void); + static gboolean show_info_button_release(GtkWidget *w, GdkEventButton *ev, gpointer data); static gboolean show_info_popup(GtkWidget *w, GdkEventButton *ev, gpointer data); @@ -234,8 +236,8 @@ static void log_callback_utf8(enum log_level level, const char *message, } /**********************************************************************//** - Called while in gtk_main() (which is all of the time) - TIMER_INTERVAL is now set by real_timer_callback() + Called while in gtk_main() (which is all of the time) + TIMER_INTERVAL is now set by real_timer_callback() **************************************************************************/ static gboolean timer_callback(gpointer data) { @@ -1702,12 +1704,85 @@ static void setup_widgets(void) } } +/**********************************************************************//** + g_log callback to log with freelog +**************************************************************************/ +static void g_log_to_freelog_cb(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + enum log_level fllvl = LOG_ERROR; + + switch (log_level) { + case G_LOG_LEVEL_DEBUG: + fllvl = LOG_DEBUG; + break; + case G_LOG_LEVEL_WARNING: + fllvl = LOG_WARN; + break; + default: + break; + } + + if (log_domain != NULL) { + log_base(fllvl, "%s: %s", log_domain, message); + } else { + log_base(fllvl, "%s", message); + } +} + +/**********************************************************************//** + g_log callback to log with freelog +**************************************************************************/ +static GLogWriterOutput g_log_writer_to_freelog_cb(GLogLevelFlags log_level, + const GLogField *fields, + gsize n_fields, + gpointer user_data) +{ + /* No need to have formatter of our own - let's use glib's default one. */ + gchar *out = g_log_writer_format_fields(log_level, fields, n_fields, FALSE); + + g_log_to_freelog_cb(NULL, log_level, out, NULL); + + return G_LOG_WRITER_HANDLED; +} + +/**********************************************************************//** + Set up g_log callback for a single domain. +**************************************************************************/ +static void set_g_log_callback_domain(const char *domain) +{ + g_log_set_handler(domain, + G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING + | G_LOG_LEVEL_MASK + | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, + g_log_to_freelog_cb, NULL); +} + +/**********************************************************************//** + Set up g_log callbacks. +**************************************************************************/ +static void set_g_log_callbacks(void) +{ + /* Old API, still used by many log producers */ + g_log_set_default_handler(g_log_to_freelog_cb, NULL); + + set_g_log_callback_domain("Gtk"); + set_g_log_callback_domain("Gdk"); + set_g_log_callback_domain("Glib"); + + /* glib >= 2.50 API */ + g_log_set_writer_func(g_log_writer_to_freelog_cb, NULL, NULL); +} + /**********************************************************************//** Called from main(). **************************************************************************/ void ui_init(void) { log_set_callback(log_callback_utf8); + set_g_log_callbacks(); set_frame_by_frame_animation(); } diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index 9582582d86..2d7d69cfbe 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -185,6 +185,8 @@ static GtkApplication *fc_app; static struct video_mode vmode = { -1, -1 }; +static void set_g_log_callbacks(void); + static gboolean show_info_popup(GtkWidget *w, GdkEvent *ev, gpointer data); static void end_turn_callback(GtkWidget *w, gpointer data); @@ -1682,12 +1684,85 @@ static void setup_widgets(void) } } +/**********************************************************************//** + g_log callback to log with freelog +**************************************************************************/ +static void g_log_to_freelog_cb(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + enum log_level fllvl = LOG_ERROR; + + switch (log_level) { + case G_LOG_LEVEL_DEBUG: + fllvl = LOG_DEBUG; + break; + case G_LOG_LEVEL_WARNING: + fllvl = LOG_WARN; + break; + default: + break; + } + + if (log_domain != NULL) { + log_base(fllvl, "%s: %s", log_domain, message); + } else { + log_base(fllvl, "%s", message); + } +} + +/**********************************************************************//** + g_log callback to log with freelog +**************************************************************************/ +static GLogWriterOutput g_log_writer_to_freelog_cb(GLogLevelFlags log_level, + const GLogField *fields, + gsize n_fields, + gpointer user_data) +{ + /* No need to have formatter of our own - let's use glib's default one. */ + gchar *out = g_log_writer_format_fields(log_level, fields, n_fields, FALSE); + + g_log_to_freelog_cb(NULL, log_level, out, NULL); + + return G_LOG_WRITER_HANDLED; +} + +/**********************************************************************//** + Set up g_log callback for a single domain. +**************************************************************************/ +static void set_g_log_callback_domain(const char *domain) +{ + g_log_set_handler(domain, + G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING + | G_LOG_LEVEL_MASK + | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, + g_log_to_freelog_cb, NULL); +} + +/**********************************************************************//** + Set up g_log callbacks. +**************************************************************************/ +static void set_g_log_callbacks(void) +{ + /* Old API, still used by many log producers */ + g_log_set_default_handler(g_log_to_freelog_cb, NULL); + + set_g_log_callback_domain("Gtk"); + set_g_log_callback_domain("Gdk"); + set_g_log_callback_domain("Glib"); + + /* glib >= 2.50 API */ + g_log_set_writer_func(g_log_writer_to_freelog_cb, NULL, NULL); +} + /**********************************************************************//** Called from main(). **************************************************************************/ void ui_init(void) { log_set_callback(log_callback_utf8); + set_g_log_callbacks(); set_frame_by_frame_animation(); } -- 2.35.1