[libvirt] [PATCH libvirt-glib] Ensure debug defaults to off for all glib versions

Daniel P. Berrange berrange at redhat.com
Tue Nov 29 13:35:11 UTC 2011


From: "Daniel P. Berrange" <berrange at redhat.com>

The previous patch changed all code to directly call g_debug().
This relies on the fact that glib >= 2.31 defaults to not
printing debug messages. The result is that for older glib we
now pollute the stderr with debug messages.

This introduces 3 env vars for controlling libvirt debug,
though on glib >= 2.31 the standard G_LOG_MESSAGES env can
be used instead.

* libvirt-glib/libvirt-glib-main.c: Check for LIBVIRT_GLIB_DEBUG
  env var
* libvirt-gobject/libvirt-gobject-main.c: Check for
  LIBVIRT_GOBJECT_DEBUG env var
* libvirt-gconfig/libvirt-gconfig-main.c: Check for
  LIBVIRT_GCONFIG_DEBUG env var
* libvirt-gconfig/libvirt-gconfig-main.h, libvirt-gconfig/libvirt-gconfig.h,
  libvirt-gconfig/libvirt-gconfig.sym: Add new files
* libvirt-gconfig/Makefile.am, libvirt-glib/Makefile.am,
  libvirt-gobject/Makefile.am: Set a default G_LOG_DOMAIN to
  allow filtering the library messages
---
 libvirt-gconfig/Makefile.am            |    3 +
 libvirt-gconfig/libvirt-gconfig-main.c |   85 ++++++++++++++++++++++++++++++++
 libvirt-gconfig/libvirt-gconfig-main.h |   41 +++++++++++++++
 libvirt-gconfig/libvirt-gconfig.h      |    1 +
 libvirt-gconfig/libvirt-gconfig.sym    |    5 ++-
 libvirt-glib/Makefile.am               |    1 +
 libvirt-glib/libvirt-glib-main.c       |   25 +++++++++
 libvirt-gobject/Makefile.am            |    1 +
 libvirt-gobject/libvirt-gobject-main.c |   27 ++++++++++
 9 files changed, 188 insertions(+), 1 deletions(-)
 create mode 100644 libvirt-gconfig/libvirt-gconfig-main.c
 create mode 100644 libvirt-gconfig/libvirt-gconfig-main.h

diff --git a/libvirt-gconfig/Makefile.am b/libvirt-gconfig/Makefile.am
index 22259c3..55f0aed 100644
--- a/libvirt-gconfig/Makefile.am
+++ b/libvirt-gconfig/Makefile.am
@@ -9,6 +9,7 @@ lib_LTLIBRARIES = libvirt-gconfig-1.0.la
 
 GCONFIG_HEADER_FILES = \
 			libvirt-gconfig.h \
+			libvirt-gconfig-main.h \
 			libvirt-gconfig-object.h \
 			libvirt-gconfig-capabilities.h \
 			libvirt-gconfig-domain.h \
@@ -37,6 +38,7 @@ noinst_HEADERS = \
 			libvirt-gconfig-object-private.h
 GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-object.c \
+			libvirt-gconfig-main.c \
 			libvirt-gconfig-capabilities.c \
 			libvirt-gconfig-domain.c \
 			libvirt-gconfig-domain-clock.c \
@@ -69,6 +71,7 @@ libvirt_gconfig_1_0_la_SOURCES = \
 			$(GCONFIG_SOURCE_FILES) \
 			$(builddir)/libvirt-gconfig-enum-types.c
 libvirt_gconfig_1_0_la_CFLAGS = \
+			-DG_LOG_DOMAIN="\"Libvirt.GConfig\"" \
 			-DDATADIR="\"$(datadir)\"" \
 			-DLIBVIRT_GCONFIG_BUILD \
 			$(COVERAGE_CFLAGS) \
diff --git a/libvirt-gconfig/libvirt-gconfig-main.c b/libvirt-gconfig/libvirt-gconfig-main.c
new file mode 100644
index 0000000..eb3732f
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-main.c
@@ -0,0 +1,85 @@
+/*
+ * libvirt-gconfig-main.c: libvirt gconfig integration
+ *
+ * Copyright (C) 2008 Daniel P. Berrange
+ * Copyright (C) 2010 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libvirt-glib/libvirt-glib.h"
+#include "libvirt-gconfig/libvirt-gconfig.h"
+
+/**
+ * gvir_init_config:
+ * @argc: (inout): pointer to application's argc
+ * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
+ */
+void gvir_init_config(int *argc,
+                      char ***argv)
+{
+    GError *err = NULL;
+    if (!gvir_init_config_check(argc, argv, &err)) {
+        g_error("Could not initialize libvirt-gconfig: %s\n",
+                err->message);
+    }
+}
+
+static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
+                             GLogLevelFlags log_level G_GNUC_UNUSED,
+                             const gchar *message,
+                             gpointer user_data)
+{
+    if (user_data)
+        fprintf(stderr, "%s\n", message);
+}
+
+
+/**
+ * gvir_init_config_check:
+ * @argc: (inout): pointer to application's argc
+ * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
+ * @err: pointer to a #GError to which a message will be posted on error
+ */
+gboolean gvir_init_config_check(int *argc G_GNUC_UNUSED,
+                                char ***argv G_GNUC_UNUSED,
+                                GError **err G_GNUC_UNUSED)
+{
+    g_type_init();
+
+    /* GLib >= 2.31.0 debug is off by default, so we need to
+     * enable it. Older versions are on by default, so we need
+     * to disable it.
+     */
+#if GLIB_CHECK_VERSION(2, 31, 0)
+    if (getenv("LIBVIRT_GCONFIG_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, (void*)0x1);
+#else
+    if (!getenv("LIBVIRT_GCONFIG_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, NULL);
+#endif
+
+    return TRUE;
+}
+
diff --git a/libvirt-gconfig/libvirt-gconfig-main.h b/libvirt-gconfig/libvirt-gconfig-main.h
new file mode 100644
index 0000000..bcd1ded
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-main.h
@@ -0,0 +1,41 @@
+/*
+ * libvirt-gconfig-main.c: libvirt gconfig integration
+ *
+ * Copyright (C) 2008 Daniel P. Berrange
+ * Copyright (C) 2010 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#if !defined(__LIBVIRT_GCONFIG_H__) && !defined(LIBVIRT_GCONFIG_BUILD)
+#error "Only <libvirt-gconfig/libvirt-gconfig.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_GCONFIG_MAIN_H__
+#define __LIBVIRT_GCONFIG_MAIN_H__
+
+G_BEGIN_DECLS
+
+void gvir_init_config(int *argc,
+                      char ***argv);
+gboolean gvir_init_config_check(int *argc,
+                                char ***argv,
+                                GError **err);
+
+G_END_DECLS
+
+#endif /* __LIBVIRT_GCONFIG_MAIN_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig.h b/libvirt-gconfig/libvirt-gconfig.h
index 064bae6..ad9f59a 100644
--- a/libvirt-gconfig/libvirt-gconfig.h
+++ b/libvirt-gconfig/libvirt-gconfig.h
@@ -26,6 +26,7 @@
 #include <glib-object.h>
 #include <libxml/tree.h>
 
+#include <libvirt-gconfig/libvirt-gconfig-main.h>
 #include <libvirt-gconfig/libvirt-gconfig-object.h>
 #include <libvirt-gconfig/libvirt-gconfig-capabilities.h>
 #include <libvirt-gconfig/libvirt-gconfig-domain.h>
diff --git a/libvirt-gconfig/libvirt-gconfig.sym b/libvirt-gconfig/libvirt-gconfig.sym
index 56e35a0..10830a6 100644
--- a/libvirt-gconfig/libvirt-gconfig.sym
+++ b/libvirt-gconfig/libvirt-gconfig.sym
@@ -1,5 +1,8 @@
-LIBVIRT_GOBJECT_0.0.1 {
+LIBVIRT_GCONFIG_0.0.1 {
   global:
+	gvir_init_config_check;
+	gvir_init_config;
+
 	gvir_config_capabilities_get_type;
 	gvir_config_capabilities_new;
 	gvir_config_capabilities_new_from_xml;
diff --git a/libvirt-glib/Makefile.am b/libvirt-glib/Makefile.am
index 0638a64..7898ba4 100644
--- a/libvirt-glib/Makefile.am
+++ b/libvirt-glib/Makefile.am
@@ -16,6 +16,7 @@ libvirt_glib_1_0_la_SOURCES = \
 			libvirt-glib-main.c
 libvirt_glib_1_0_la_CFLAGS = \
 			-DLIBVIRT_GLIB_BUILD \
+			-DG_LOG_DOMAIN="\"Libvirt.GLib\"" \
 			$(COVERAGE_CFLAGS) \
 			-I$(top_srcdir) \
 			$(LIBVIRT_CFLAGS) \
diff --git a/libvirt-glib/libvirt-glib-main.c b/libvirt-glib/libvirt-glib-main.c
index 8d2f216..f2a66d5 100644
--- a/libvirt-glib/libvirt-glib-main.c
+++ b/libvirt-glib/libvirt-glib-main.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <libvirt/virterror.h>
 
 #include "libvirt-glib-main.h"
@@ -47,6 +48,16 @@ void gvir_init(int *argc,
 }
 
 
+static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
+                             GLogLevelFlags log_level G_GNUC_UNUSED,
+                             const gchar *message,
+                             gpointer user_data)
+{
+    if (user_data)
+        fprintf(stderr, "%s\n", message);
+}
+
+
 gboolean gvir_init_check(int *argc G_GNUC_UNUSED,
                          char ***argv G_GNUC_UNUSED,
                          GError **err G_GNUC_UNUSED)
@@ -57,6 +68,20 @@ gboolean gvir_init_check(int *argc G_GNUC_UNUSED,
 
     virInitialize();
 
+    /* GLib >= 2.31.0 debug is off by default, so we need to
+     * enable it. Older versions are on by default, so we need
+     * to disable it.
+     */
+#if GLIB_CHECK_VERSION(2, 31, 0)
+    if (getenv("LIBVIRT_GLIB_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, (void*)0x1);
+#else
+    if (!getenv("LIBVIRT_GLIB_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, NULL);
+#endif
+
     return TRUE;
 }
 
diff --git a/libvirt-gobject/Makefile.am b/libvirt-gobject/Makefile.am
index ec7b454..afa0d85 100644
--- a/libvirt-gobject/Makefile.am
+++ b/libvirt-gobject/Makefile.am
@@ -60,6 +60,7 @@ libvirt_gobject_1_0_la_SOURCES = \
 nodist_libvirt_gobject_1_0_la_SOURCES = \
 			$(GOBJECT_GENERATED_FILES)
 libvirt_gobject_1_0_la_CFLAGS = \
+			-DG_LOG_DOMAIN="\"Libvirt.GObject\"" \
 			-DDATADIR="\"$(datadir)\"" \
 			-DLIBVIRT_GOBJECT_BUILD \
 			$(COVERAGE_CFLAGS) \
diff --git a/libvirt-gobject/libvirt-gobject-main.c b/libvirt-gobject/libvirt-gobject-main.c
index 0b90b2a..e601457 100644
--- a/libvirt-gobject/libvirt-gobject-main.c
+++ b/libvirt-gobject/libvirt-gobject-main.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <stdlib.h>
+#include <stdio.h>
 
 #include "libvirt-glib/libvirt-glib.h"
 #include "libvirt-gobject/libvirt-gobject.h"
@@ -43,6 +44,15 @@ void gvir_init_object(int *argc,
     }
 }
 
+static void gvir_log_handler(const gchar *log_domain G_GNUC_UNUSED,
+                             GLogLevelFlags log_level G_GNUC_UNUSED,
+                             const gchar *message,
+                             gpointer user_data)
+{
+    if (user_data)
+        fprintf(stderr, "%s\n", message);
+}
+
 
 /**
  * gvir_init_object_check:
@@ -61,6 +71,23 @@ gboolean gvir_init_object_check(int *argc,
     if (!gvir_init_check(argc, argv, err))
         return FALSE;
 
+    if (!gvir_init_config_check(argc, argv, err))
+        return FALSE;
+
+    /* GLib >= 2.31.0 debug is off by default, so we need to
+     * enable it. Older versions are on by default, so we need
+     * to disable it.
+     */
+#if GLIB_CHECK_VERSION(2, 31, 0)
+    if (getenv("LIBVIRT_GOBJECT_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, (void*)0x1);
+#else
+    if (!getenv("LIBVIRT_GOBJECT_DEBUG"))
+        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
+                          gvir_log_handler, NULL);
+#endif
+
     return TRUE;
 }
 
-- 
1.7.6.4




More information about the libvir-list mailing list