[gtk/matthiasc/for-master: 2/4] builder-tool: Install a log writer




commit 4253aebaf76b8d96d48ac3771dc443223fe066a0
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Nov 1 15:48:37 2020 -0500

    builder-tool: Install a log writer
    
    Install a log writer function that is a bit simpler than
    the default GLib log writer, and does not put PIDs and
    timestamps in the output, so we can compare easily in
    our testsuite.

 gtk/tools/gtk-builder-tool.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
---
diff --git a/gtk/tools/gtk-builder-tool.c b/gtk/tools/gtk-builder-tool.c
index e07cfb6218..dee2ecca28 100644
--- a/gtk/tools/gtk-builder-tool.c
+++ b/gtk/tools/gtk-builder-tool.c
@@ -52,11 +52,66 @@ usage (void)
   exit (1);
 }
 
+#if !GLIB_CHECK_VERSION (2,68,0)
+static gboolean
+g_log_writer_default_would_drop (GLogLevelFlags  level,
+                                 const char     *domain)
+{
+  return (level & (G_LOG_LEVEL_ERROR |
+                   G_LOG_LEVEL_CRITICAL |
+                   G_LOG_LEVEL_WARNING)) == 0;
+}
+#endif
+
+static GLogWriterOutput
+log_writer_func (GLogLevelFlags   level,
+                 const GLogField *fields,
+                 gsize            n_fields,
+                 gpointer         user_data)
+{
+  gsize i;
+  const char *domain = NULL;
+  const char *message = NULL;
+
+  for (i = 0; i < n_fields; i++)
+    {
+      if (g_strcmp0 (fields[i].key, "GLIB_DOMAIN") == 0)
+        domain = fields[i].value;
+      else if (g_strcmp0 (fields[i].key, "MESSAGE") == 0)
+        message = fields[i].value;
+    }
+
+  if (message != NULL && !g_log_writer_default_would_drop (level, domain))
+    {
+      const char *prefix;
+      switch (level & G_LOG_LEVEL_MASK)
+        {
+        case G_LOG_LEVEL_ERROR:
+          prefix = "ERROR";
+          break;
+        case G_LOG_LEVEL_CRITICAL:
+          prefix = "CRITICAL";
+          break;
+        case G_LOG_LEVEL_WARNING:
+          prefix = "WARNING";
+          break;
+        default:
+          prefix = "INFO";
+          break;
+        }
+      g_printerr ("%s-%s: %s\n", domain, prefix, message);
+    }
+
+  return G_LOG_WRITER_HANDLED;
+}
+
 int
 main (int argc, const char *argv[])
 {
   g_set_prgname ("gtk-builder-tool");
 
+  g_log_set_writer_func (log_writer_func, NULL, NULL);
+
   gtk_init ();
 
   gtk_test_register_all_types ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]