From 0063daf8f05f96134d16d9cfab0deff37655b553 Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Fri, 16 Feb 2018 10:51:37 -0500
Subject: [PATCH] object: only print stacktraces when debugging enabled

We have a bunch of corruption right now spamming the
log.

This commit gets rid of the spam unless G_MESSAGES_DEBUG
is set.
---
 gi/object.cpp | 105 +++++++++++++++++++++++++++++---------------------
 1 file changed, 61 insertions(+), 44 deletions(-)

diff --git a/gi/object.cpp b/gi/object.cpp
index c78a0a4d..61e7625c 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -86,6 +86,29 @@ GJS_DEFINE_PRIV_FROM_JS(ObjectInstance, gjs_object_instance_class)
 
 static void            disassociate_js_gobject (GObject *gobj);
 
+static void
+gjs_log_stacktrace(const char *format,
+                   ...)
+{
+    const char *domain;
+    va_list     args;
+
+    domain = g_getenv("G_MESSAGES_DEBUG");
+
+    if (!domain)
+        return;
+
+    if (!g_str_equal(domain, "all") &&
+        !strstr(domain, G_LOG_DOMAIN))
+        return;
+
+    va_start(args, format);
+    g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
+    va_end(args);
+
+    gjs_dumpstack();
+}
+
 typedef enum {
     SOME_ERROR_OCCURRED = false,
     NO_SUCH_G_PROPERTY,
@@ -392,12 +415,11 @@ object_instance_get_prop(JSContext              *context,
         return true;
 
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already finalized. "
-                   "Impossible to get any property from it.",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already finalized. "
+                           "Impossible to get any property from it.",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
         return true;
     }
 
@@ -512,12 +534,11 @@ object_instance_set_prop(JSContext              *context,
         return result.succeed();
 
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already finalized. "
-                   "Impossible to set any property to it.",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already finalized. "
+                           "Impossible to set any property to it.",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
         return result.succeed();
     }
 
@@ -756,12 +777,11 @@ object_instance_resolve(JSContext       *context,
     }
 
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already finalized. "
-                   "Impossible to resolve it.",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already finalized. "
+                           "Impossible to resolve it.",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
 
         *resolved = false;
         return true;
@@ -1458,11 +1478,11 @@ object_instance_trace(JSTracer *tracer,
         return;
 
     if (priv->g_object_finalized) {
-        g_debug("Object %s.%s (%p), has been already finalized. "
-                "Impossible to trace it.",
-                 priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                 priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                 priv->gobj);
+        gjs_log_stacktrace("Object %s.%s (%p), has been already finalized. "
+                           "Impossible to trace it.",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
         return;
     }
 
@@ -1669,13 +1689,12 @@ real_connect_func(JSContext *context,
         return false;
     }
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already deallocated - impossible to connect to signal. "
-                   "This might be caused by the fact that the object has been destroyed from C "
-                   "code using something such as destroy(), dispose(), or remove() vfuncs",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already deallocated - impossible to connect to signal. "
+                           "This might be caused by the fact that the object has been destroyed from C "
+                           "code using something such as destroy(), dispose(), or remove() vfuncs",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
         return true;
     }
 
@@ -1762,13 +1781,12 @@ emit_func(JSContext *context,
     }
 
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already deallocated - impossible to emit signal. "
-                   "This might be caused by the fact that the object has been destroyed from C "
-                   "code using something such as destroy(), dispose(), or remove() vfuncs",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already deallocated - impossible to emit signal. "
+                           "This might be caused by the fact that the object has been destroyed from C "
+                           "code using something such as destroy(), dispose(), or remove() vfuncs",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
         return true;
     }
 
@@ -2185,13 +2203,12 @@ gjs_typecheck_object(JSContext       *context,
     }
 
     if (priv->g_object_finalized) {
-        g_critical("Object %s.%s (%p), has been already deallocated - impossible to access to it. "
-                   "This might be caused by the fact that the object has been destroyed from C "
-                   "code using something such as destroy(), dispose(), or remove() vfuncs",
-                   priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
-                   priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
-                   priv->gobj);
-        gjs_dumpstack();
+        gjs_log_stacktrace("Object %s.%s (%p), has been already deallocated - impossible to access to it. "
+                           "This might be caused by the fact that the object has been destroyed from C "
+                           "code using something such as destroy(), dispose(), or remove() vfuncs",
+                           priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
+                           priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype),
+                           priv->gobj);
 
         return true;
     }
-- 
2.21.0

