[gnome-builder/wip/gtk4-port: 978/1774] main: clear G_MESSAGES_DEBUG at startup




commit 6e5829633e7cdc787834a8a701a07d2924971e76
Author: Christian Hergert <chergert redhat com>
Date:   Wed May 11 12:10:25 2022 -0700

    main: clear G_MESSAGES_DEBUG at startup
    
    Since we already have our own internal logging handler, we can clear the
    value of G_MESSAGES_DEBUG very early on so that we never pass it along to
    any of our children processes.
    
    This also removes some of our hacks in other places which were needed
    precisely because we weren't clearing this immediately at startup.
    
    Furthermore, most of our log functions are made private as they have no
    business being in our exported API.

 src/libide/core/ide-log-private.h              | 34 ++++++++++++++++++++++++++
 src/libide/core/ide-log.c                      | 32 ++++++------------------
 src/libide/core/ide-log.h                      | 13 ++--------
 src/libide/threading/ide-subprocess-launcher.c |  6 -----
 src/main.c                                     | 11 ++++++++-
 src/plugins/flatpak/gbp-flatpak-runner.c       |  5 ----
 6 files changed, 53 insertions(+), 48 deletions(-)
---
diff --git a/src/libide/core/ide-log-private.h b/src/libide/core/ide-log-private.h
new file mode 100644
index 000000000..331d33264
--- /dev/null
+++ b/src/libide/core/ide-log-private.h
@@ -0,0 +1,34 @@
+/* ide-log-private.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include "ide-log.h"
+
+G_BEGIN_DECLS
+
+void ide_log_init               (gboolean    stdout_,
+                                 const char *filename,
+                                 const char *messages_debug);
+void ide_log_increase_verbosity (void);
+void ide_log_set_verbosity      (int         level);
+void ide_log_shutdown           (void);
+
+G_END_DECLS
diff --git a/src/libide/core/ide-log.c b/src/libide/core/ide-log.c
index 16dd3a3d3..a867eb3da 100644
--- a/src/libide/core/ide-log.c
+++ b/src/libide/core/ide-log.c
@@ -38,7 +38,7 @@
 #include <unistd.h>
 
 #include "ide-debug.h"
-#include "ide-log.h"
+#include "ide-log-private.h"
 #include "ide-macros.h"
 #include "ide-private.h"
 
@@ -84,8 +84,6 @@
  * Ide.debug("This is a debug")
  * Ide.error("This is a fatal error")
  * ]|
- *
- * Since: 3.32
  */
 
 typedef const gchar *(*IdeLogLevelStrFunc) (GLogLevelFlags log_level);
@@ -106,8 +104,6 @@ G_LOCK_DEFINE (channels_lock);
  * On other platforms, the current thread pointer is retrieved.
  *
  * Returns: The task id.
- *
- * Since: 3.32
  */
 static inline gint
 ide_log_get_thread (void)
@@ -127,8 +123,6 @@ ide_log_get_thread (void)
  *
  * Returns: A string which shouldn't be modified or freed.
  * Side effects: None.
- *
- * Since: 3.32
  */
 static const gchar *
 ide_log_level_str (GLogLevelFlags log_level)
@@ -172,8 +166,6 @@ ide_log_level_str_with_color (GLogLevelFlags log_level)
  * @message: A string log message.
  *
  * Writes @message to @channel and flushes the channel.
- *
- * Since: 3.32
  */
 static void
 ide_log_write_to_channel (GIOChannel  *channel,
@@ -192,8 +184,6 @@ ide_log_write_to_channel (GIOChannel  *channel,
  *
  * Default log handler that will dispatch log messages to configured logging
  * destinations.
- *
- * Since: 3.32
  */
 static void
 ide_log_handler (const gchar    *log_domain,
@@ -274,16 +264,16 @@ ide_log_handler (const gchar    *log_domain,
  * ide_log_init:
  * @stdout_: Indicates logging should be written to stdout.
  * @filename: An optional file in which to store logs.
+ * @messages_debug: the value of G_MESSAGES_DEBUG environment variable
  *
  * Initializes the logging subsystem. This should be called from
  * the application entry point only. Secondary calls to this function
  * will do nothing.
- *
- * Since: 3.32
  */
 void
-ide_log_init (gboolean     stdout_,
-              const gchar *filename)
+ide_log_init (gboolean    stdout_,
+              const char *filename,
+              const char *messages_debug)
 {
   static gsize initialized = FALSE;
   GIOChannel *channel;
@@ -305,7 +295,7 @@ ide_log_init (gboolean     stdout_,
             log_level_str_func = ide_log_level_str_with_color;
         }
 
-      domains = g_strdup (g_getenv ("G_MESSAGES_DEBUG"));
+      domains = g_strdup (messages_debug);
       if (!ide_str_empty0 (domains) && strcmp (domains, "all") != 0)
         has_domains = TRUE;
 
@@ -319,8 +309,6 @@ ide_log_init (gboolean     stdout_,
  *
  * Cleans up after the logging subsystem and restores the original
  * log handler.
- *
- * Since: 3.32
  */
 void
 ide_log_shutdown (void)
@@ -352,8 +340,6 @@ ide_log_shutdown (void)
  * line.
  *
  * Calling this method more than four times is acceptable.
- *
- * Since: 3.32
  */
 void
 ide_log_increase_verbosity (void)
@@ -366,10 +352,8 @@ ide_log_increase_verbosity (void)
  *
  * Retrieves the log verbosity, which is the number of times -v was
  * provided on the command line.
- *
- * Since: 3.32
  */
-gint
+int
 ide_log_get_verbosity (void)
 {
   return log_verbosity;
@@ -380,8 +364,6 @@ ide_log_get_verbosity (void)
  *
  * Sets the explicit verbosity. Generally you want to use
  * ide_log_increase_verbosity() instead of this function.
- *
- * Since: 3.32
  */
 void
 ide_log_set_verbosity (gint level)
diff --git a/src/libide/core/ide-log.h b/src/libide/core/ide-log.h
index c8052dca4..7c80faf6f 100644
--- a/src/libide/core/ide-log.h
+++ b/src/libide/core/ide-log.h
@@ -30,16 +30,7 @@
 
 G_BEGIN_DECLS
 
-IDE_AVAILABLE_IN_3_32
-void ide_log_init               (gboolean     stdout_,
-                                 const gchar *filename);
-IDE_AVAILABLE_IN_3_32
-void ide_log_increase_verbosity (void);
-IDE_AVAILABLE_IN_3_32
-gint ide_log_get_verbosity      (void);
-IDE_AVAILABLE_IN_3_32
-void ide_log_set_verbosity      (gint         level);
-IDE_AVAILABLE_IN_3_32
-void ide_log_shutdown           (void);
+IDE_AVAILABLE_IN_ALL
+int ide_log_get_verbosity (void);
 
 G_END_DECLS
diff --git a/src/libide/threading/ide-subprocess-launcher.c b/src/libide/threading/ide-subprocess-launcher.c
index 497c54590..e8e25aa45 100644
--- a/src/libide/threading/ide-subprocess-launcher.c
+++ b/src/libide/threading/ide-subprocess-launcher.c
@@ -622,12 +622,6 @@ ide_subprocess_launcher_init (IdeSubprocessLauncher *self)
   g_ptr_array_add (priv->argv, NULL);
 
   priv->cwd = g_strdup (".");
-  /* Prevent inheritance of G_MESSAGES_DEBUG because it brings a lot of problems with IPC
-   * over stdout/stdin because all the debug messages would go to stdout, which means
-   * that the connection would be closed because of invalid data. If needed it can still
-   * be set back if needed, but at least it's a good default.
-   */
-  ide_subprocess_launcher_setenv (self, "G_MESSAGES_DEBUG", "", TRUE);
 }
 
 void
diff --git a/src/main.c b/src/main.c
index 7627cc3be..771610f6c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,12 +34,14 @@
 # include <sysprof-capture.h>
 #endif
 #include <sched.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 #include "ide-application-private.h"
 #include "ide-build-ident.h"
 #include "ide-editor-private.h"
 #include "ide-gtk-private.h"
+#include "ide-log-private.h"
 #include "ide-search-private.h"
 #include "ide-shell-private.h"
 #include "ide-terminal-private.h"
@@ -205,11 +207,18 @@ gint
 main (gint   argc,
       gchar *argv[])
 {
+  g_autofree char *messages_debug = NULL;
   IdeApplication *app;
   const gchar *desktop;
   gboolean standalone = FALSE;
   int ret;
 
+  /* Get environment variable early and clear it from GLib. We want to be
+   * certain we don't pass this on to child processes so we clear it upfront.
+   */
+  messages_debug = g_strdup (getenv ("G_MESSAGES_DEBUG"));
+  unsetenv ("G_MESSAGES_DEBUG");
+
   /* Setup our gdb fork()/exec() helper if we're in a terminal */
   if (is_running_in_shell ())
     bug_buddy_init ();
@@ -235,7 +244,7 @@ main (gint   argc,
    * format. If we deferred this to GApplication, we'd get them in
    * multiple formats.
    */
-  ide_log_init (TRUE, NULL);
+  ide_log_init (TRUE, NULL, messages_debug);
 
   /* Extract options like -vvvv */
   early_params_check (&argc, &argv, &standalone);
diff --git a/src/plugins/flatpak/gbp-flatpak-runner.c b/src/plugins/flatpak/gbp-flatpak-runner.c
index 90796a71e..f639cc891 100644
--- a/src/plugins/flatpak/gbp-flatpak-runner.c
+++ b/src/plugins/flatpak/gbp-flatpak-runner.c
@@ -181,11 +181,6 @@ gbp_flatpak_runner_fixup_launcher (IdeRunner             *runner,
         }
     }
 
-  /* Disable G_MESSAGES_DEBUG as it could cause 'flatpak build' to spew info
-   * and mess up systems that need a clean stdin/stdout/stderr.
-   */
-  ide_subprocess_launcher_setenv (launcher, "G_MESSAGES_DEBUG", NULL, TRUE);
-
   ide_subprocess_launcher_insert_argv (launcher, i++, self->build_path);
 }
 


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