main: Define a custom log writer to dump on structured messages

Even though GNOME Shell is not explicitly using structured logging via
G_LOG_USE_STRUCTURED, GLib uses it as default since 2016 [1], and so
we're de facto using it.

As per this, if backtrace on warnings is enabled, it is ignored since the
log handler isn't used anymore, and no dump is printed.

Thus, replace the default log handlers with writer functions instead, honoring
backtrace-warnings debug string.

[1] https://gitlab.gnome.org/GNOME/glib/-/commit/fce7cfaf40b6e1e50c9140aa0397f5

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/697>
This commit is contained in:
Marco Trevisan (Treviño) 2019-09-02 17:05:27 +02:00 committed by Marge Bot
parent 9abf3508fd
commit 1bd9076590

View File

@ -287,39 +287,55 @@ shell_init_debug (const char *debug_env)
G_N_ELEMENTS (keys)); G_N_ELEMENTS (keys));
} }
static void static GLogWriterOutput
default_log_handler (const char *log_domain, default_log_writer (GLogLevelFlags log_level,
GLogLevelFlags log_level, const GLogField *fields,
const char *message, gsize n_fields,
gpointer data) gpointer user_data)
{ {
if (!log_domain || !g_str_has_prefix (log_domain, "tp-glib")) GLogWriterOutput output;
g_log_default_handler (log_domain, log_level, message, data); int i;
/* Filter out Gjs logs, those already have the stack */ output = g_log_writer_default (log_level, fields, n_fields, user_data);
if (log_domain && strcmp (log_domain, "Gjs") == 0)
return;
if ((_shell_debug & SHELL_DEBUG_BACKTRACE_WARNINGS) && if ((_shell_debug & SHELL_DEBUG_BACKTRACE_WARNINGS) &&
((log_level & G_LOG_LEVEL_CRITICAL) || ((log_level & G_LOG_LEVEL_CRITICAL) ||
(log_level & G_LOG_LEVEL_WARNING))) (log_level & G_LOG_LEVEL_WARNING)))
{
const char *log_domain = NULL;
for (i = 0; i < n_fields; i++)
{
if (g_strcmp0 (fields[i].key, "GLIB_DOMAIN") == 0)
{
log_domain = fields[i].value;
break;
}
}
/* Filter out Gjs logs, those already have the stack */
if (g_strcmp0 (log_domain, "Gjs") != 0)
gjs_dumpstack (); gjs_dumpstack ();
} }
static void return output;
shut_up (const char *domain, }
GLogLevelFlags level,
const char *message, static GLogWriterOutput
shut_up (GLogLevelFlags log_level,
const GLogField *fields,
gsize n_fields,
gpointer user_data) gpointer user_data)
{ {
return (GLogWriterOutput) {0};
} }
static void static void
dump_gjs_stack_alarm_sigaction (int signo) dump_gjs_stack_alarm_sigaction (int signo)
{ {
g_log_set_default_handler (g_log_default_handler, NULL); g_log_set_writer_func (g_log_writer_default, NULL, NULL);
g_warning ("Failed to dump Javascript stack, got stuck"); g_warning ("Failed to dump Javascript stack, got stuck");
g_log_set_default_handler (default_log_handler, NULL); g_log_set_writer_func (default_log_writer, NULL, NULL);
raise (caught_signal); raise (caught_signal);
} }
@ -384,7 +400,7 @@ list_modes (const char *option_name,
* ShellGlobal has some GTK+ dependencies, so initialize GTK+; we * ShellGlobal has some GTK+ dependencies, so initialize GTK+; we
* don't really care if it fails though (e.g. when running from a tty), * don't really care if it fails though (e.g. when running from a tty),
* so we mute all warnings */ * so we mute all warnings */
g_log_set_default_handler (shut_up, NULL); g_log_set_writer_func (shut_up, NULL, NULL);
gtk_init_check (NULL, NULL); gtk_init_check (NULL, NULL);
_shell_global_init (NULL); _shell_global_init (NULL);
@ -533,7 +549,7 @@ main (int argc, char **argv)
shell_introspection_init (); shell_introspection_init ();
shell_fonts_init (); shell_fonts_init ();
g_log_set_default_handler (default_log_handler, NULL); g_log_set_writer_func (default_log_writer, NULL, NULL);
/* Initialize the global object */ /* Initialize the global object */
if (session_mode == NULL) if (session_mode == NULL)