[gnome-builder] util: add helper to log to widgets IdeContext



commit 253afaf7637bef0426a01d4b838f747a98efca45
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jan 5 23:33:02 2018 -0800

    util: add helper to log to widgets IdeContext
    
    If we can get an IdeContext for a widget, we can simplify the
    logging process for plugins and internal apps.

 src/libide/util/ide-gtk.c |   49 +++++++++++++++++++++++++++++++++++++++++++++
 src/libide/util/ide-gtk.h |    8 +++++++
 2 files changed, 57 insertions(+), 0 deletions(-)
---
diff --git a/src/libide/util/ide-gtk.c b/src/libide/util/ide-gtk.c
index c5fa49b..aa0e087 100644
--- a/src/libide/util/ide-gtk.c
+++ b/src/libide/util/ide-gtk.c
@@ -18,6 +18,7 @@
 
 #define G_LOG_DOMAIN "ide-gtk"
 
+#include "application/ide-application.h"
 #include "util/ide-gtk.h"
 
 static void
@@ -138,3 +139,51 @@ ide_widget_get_context (GtkWidget *widget)
 
   return ide_workbench_get_context (workbench);
 }
+
+void
+ide_widget_message (gpointer     instance,
+                    const gchar *format,
+                    ...)
+{
+  g_autofree gchar *str = NULL;
+  IdeContext *context;
+  va_list args;
+
+  g_return_if_fail (IDE_IS_MAIN_THREAD ());
+  g_return_if_fail (GTK_IS_WIDGET (instance));
+
+  va_start (args, format);
+  str = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  context = ide_widget_get_context (instance);
+
+  if (context != NULL)
+    ide_context_emit_log (context, G_LOG_LEVEL_MESSAGE, str, -1);
+  else
+    g_message ("%s", str);
+}
+
+void
+ide_widget_warning (gpointer     instance,
+                    const gchar *format,
+                    ...)
+{
+  g_autofree gchar *str = NULL;
+  IdeContext *context;
+  va_list args;
+
+  g_return_if_fail (IDE_IS_MAIN_THREAD ());
+  g_return_if_fail (GTK_IS_WIDGET (instance));
+
+  va_start (args, format);
+  str = g_strdup_vprintf (format, args);
+  va_end (args);
+
+  context = ide_widget_get_context (instance);
+
+  if (context != NULL)
+    ide_context_emit_log (context, G_LOG_LEVEL_WARNING, str, -1);
+  else
+    g_warning ("%s", str);
+}
diff --git a/src/libide/util/ide-gtk.h b/src/libide/util/ide-gtk.h
index d26e030..d0b0694 100644
--- a/src/libide/util/ide-gtk.h
+++ b/src/libide/util/ide-gtk.h
@@ -38,5 +38,13 @@ IDE_AVAILABLE_IN_ALL
 IdeContext   *ide_widget_get_context         (GtkWidget               *widget);
 IDE_AVAILABLE_IN_ALL
 IdeWorkbench *ide_widget_get_workbench       (GtkWidget               *widget);
+IDE_AVAILABLE_IN_3_28
+void          ide_widget_message             (gpointer                 instance,
+                                              const gchar             *format,
+                                              ...) G_GNUC_PRINTF (2, 3);
+IDE_AVAILABLE_IN_3_28
+void          ide_widget_warning             (gpointer                 instance,
+                                              const gchar             *format,
+                                              ...) G_GNUC_PRINTF (2, 3);
 
 G_END_DECLS


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