From 26c4cd8bbf096ecf23a86d3ed24940b716408f87 Mon Sep 17 00:00:00 2001 From: ailin-nemui Date: Wed, 17 Nov 2021 11:17:59 +0100 Subject: [PATCH] Merge pull request #50 from ailin-nemui/gliblog Improve handling of GLib log messages (cherry picked from commit 0e3f6aa4a25cc8f3d526c86cd985315683028c41) --- src/common.h | 2 +- src/fe-common/core/fe-common-core.c | 73 +++++++++++++++++++++++++++-- src/fe-common/core/module-formats.c | 2 +- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/common.h b/src/common.h index f7719e1f..ac09a553 100644 --- a/src/common.h +++ b/src/common.h @@ -6,7 +6,7 @@ #define IRSSI_GLOBAL_CONFIG "irssi.conf" /* config file name in /etc/ */ #define IRSSI_HOME_CONFIG "config" /* config file name in ~/.irssi/ */ -#define IRSSI_ABI_VERSION 42 +#define IRSSI_ABI_VERSION 43 #define DEFAULT_SERVER_ADD_PORT 6667 #define DEFAULT_SERVER_ADD_TLS_PORT 6697 diff --git a/src/fe-common/core/fe-common-core.c b/src/fe-common/core/fe-common-core.c index 6a929719..9724354f 100644 --- a/src/fe-common/core/fe-common-core.c +++ b/src/fe-common/core/fe-common-core.c @@ -166,6 +166,7 @@ void fe_common_core_init(void) settings_add_bool("lookandfeel", "use_msgs_window", FALSE); g_get_charset(&str); settings_add_str("lookandfeel", "term_charset", str); + settings_add_str("lookandfeel", "glib_log_domains", "all"); themes_init(); theme_register(fecommon_core_formats); @@ -257,9 +258,54 @@ void fe_common_core_deinit(void) g_log_set_default_handler(logger_old, NULL); } -void i_log_func(const char *log_domain, GLogLevelFlags log_level, const char *message) +static gboolean glib_domain_wanted(const char *domain) { - const char *reason; + const char *domains; + char *c, *cur; + int len = 0; + int print_it = 0; /* -1 for exclude, 0 for undecided, 1 for include */ + int incl; + + /* Go through each item in glib_log_domains setting to determine whether + * or not we want to print message from this domain */ + domains = settings_get_str("glib_log_domains"); + c = cur = (char *) domains; + + do { + /* Advance through the string until we hit a space or the end */ + while (*cur != '\0' && *cur != ' ') { + cur++; + len++; + } + + /* Handle '-' prefix */ + incl = 1; + if (*c == '-') { + incl = -1; + c++; + len--; + } + + /* If we got a valid item, process it */ + if (len > 0 && (!strncmp(domain, c, len) || !strncasecmp("all", c, len) || + !strncmp("*", c, len))) + print_it = incl; + + /* Go past any spaces towards the next item */ + while (*cur == ' ') + cur++; + + /* Move on beyond the item we just handled */ + c = cur; + len = 0; + } while (*c != '\0' && print_it != -1); + + return (print_it == 1); +} + +static void i_log_func(const char *log_domain, GLogLevelFlags log_level, const char *message) +{ + const char *reason, *domain; switch (log_level) { case G_LOG_LEVEL_WARNING: @@ -268,16 +314,33 @@ void i_log_func(const char *log_domain, GLogLevelFlags log_level, const char *me case G_LOG_LEVEL_CRITICAL: reason = "critical"; break; + case G_LOG_LEVEL_DEBUG: + reason = "debug"; + break; + case G_LOG_LEVEL_MESSAGE: + reason = "message"; + break; + case G_LOG_LEVEL_INFO: + reason = "info"; + break; default: reason = "error"; break; } + /* If log_domain parameter is NULL, GLib means to tell us that this is + * meant to be some nebulous "default" log domain name. */ + domain = (log_domain ? log_domain : "default"); + + /* Only print the message if we decided to */ + if (!glib_domain_wanted(domain)) + return; + if (windows == NULL) - fprintf(stderr, "GLib %s: %s\n", reason, message); + fprintf(stderr, "GLib (%s) %s: %s\n", domain, reason, message); else { - printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, - TXT_GLIB_ERROR, reason, message); + printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_GLIB_ERROR, domain, reason, + message); } } diff --git a/src/fe-common/core/module-formats.c b/src/fe-common/core/module-formats.c index e22b5e78..8c10f420 100644 --- a/src/fe-common/core/module-formats.c +++ b/src/fe-common/core/module-formats.c @@ -288,7 +288,7 @@ FORMAT_REC fecommon_core_formats[] = { { "config_saved", "Saved configuration to file $0", 1, { 0 } }, { "config_reloaded", "Reloaded configuration", 1, { 0 } }, { "config_modified", "Configuration file was modified since irssi was last started - do you want to overwrite the possible changes?", 1, { 0 } }, - { "glib_error", "{error $0} $1", 2, { 0, 0 } }, + { "glib_error", "{error ($0) $1} $2", 3, { 0, 0, 0 } }, { "overwrite_config", "Overwrite config (y/N)?", 0 }, { "set_title", "[{hilight $0}]", 1, { 0 } }, { "set_item", "$[-!32]0 %_$1", 2, { 0, 0 } },