[glib: 1/3] gmessages: Expose a property for enabling debug message output
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 1/3] gmessages: Expose a property for enabling debug message output
- Date: Thu, 27 Jan 2022 11:00:41 +0000 (UTC)
commit bb4bec0921fb588771eec02fbd20729f1fcf3b11
Author: Philip Withnall <philip withnall collabora co uk>
Date: Sat Aug 13 16:45:23 2016 +0200
gmessages: Expose a property for enabling debug message output
This is an API analogue of the G_MESSAGES_DEBUG environment variable. It
is intended to be exposed outside applications (for example, as a D-Bus
interface — see follow-up commits) so that there is a uniform interface
for controlling the debug output of an application.
Helps: #1190
docs/reference/glib/glib-sections.txt | 2 ++
glib/gmessages.c | 46 ++++++++++++++++++++++++++++++++++-
glib/gmessages.h | 6 +++++
3 files changed, 53 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 35b58cfb0..e2cc043ff 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1534,6 +1534,8 @@ g_log_set_always_fatal
g_log_set_fatal_mask
g_log_default_handler
g_log_set_default_handler
+g_log_get_debug_enabled
+g_log_set_debug_enabled
<SUBSECTION>
g_log_structured
diff --git a/glib/gmessages.c b/glib/gmessages.c
index 7affcd4d9..7d23d582e 100644
--- a/glib/gmessages.c
+++ b/glib/gmessages.c
@@ -525,6 +525,7 @@ static gpointer fatal_log_data;
static GLogWriterFunc log_writer_func = g_log_writer_default;
static gpointer log_writer_user_data = NULL;
static GDestroyNotify log_writer_user_data_free = NULL;
+static gboolean g_log_debug_enabled = FALSE; /* (atomic) */
/* --- functions --- */
@@ -2646,7 +2647,9 @@ should_drop_message (GLogLevelFlags log_level,
gsize n_fields)
{
/* Disable debug message output unless specified in G_MESSAGES_DEBUG. */
- if (!(log_level & DEFAULT_LEVELS) && !(log_level >> G_LOG_LEVEL_USER_SHIFT))
+ if (!(log_level & DEFAULT_LEVELS) &&
+ !(log_level >> G_LOG_LEVEL_USER_SHIFT) &&
+ !g_log_get_debug_enabled ())
{
const gchar *domains;
gsize i;
@@ -2877,6 +2880,47 @@ _g_log_writer_fallback (GLogLevelFlags log_level,
return G_LOG_WRITER_HANDLED;
}
+/**
+ * g_log_get_debug_enabled:
+ *
+ * Return whether debug output from the GLib logging system is enabled.
+ *
+ * Note that this should not be used to conditionalise calls to g_debug() or
+ * other logging functions; it should only be used from %GLogWriterFunc
+ * implementations.
+ *
+ * Note also that the value of this does not depend on `G_MESSAGES_DEBUG`, as
+ * it is domain-dependent.
+ *
+ * Returns: %TRUE if debug output is enabled, %FALSE otherwise
+ *
+ * Since: 2.72
+ */
+gboolean
+g_log_get_debug_enabled (void)
+{
+ return g_atomic_int_get (&g_log_debug_enabled);
+}
+
+/**
+ * g_log_set_debug_enabled:
+ * @enabled: %TRUE to enable debug output, %FALSE otherwise
+ *
+ * Enable or disable debug output from the GLib logging system is enabled. This
+ * value interacts disjunctively with `G_MESSAGES_DEBUG` — if either of them
+ * would allow a debug message to be outputted, it will be.
+ *
+ * Note that this should not be used from within library code to enable debug
+ * output — it is intended for external use.
+ *
+ * Since: 2.72
+ */
+void
+g_log_set_debug_enabled (gboolean enabled)
+{
+ g_atomic_int_set (&g_log_debug_enabled, enabled);
+}
+
/**
* g_return_if_fail_warning: (skip)
* @log_domain: (nullable): log domain
diff --git a/glib/gmessages.h b/glib/gmessages.h
index f0b3a8685..c7efd5376 100644
--- a/glib/gmessages.h
+++ b/glib/gmessages.h
@@ -249,6 +249,12 @@ GLIB_AVAILABLE_IN_2_68
gboolean g_log_writer_default_would_drop (GLogLevelFlags log_level,
const char *log_domain);
+/* G_MESSAGES_DEBUG enablement */
+GLIB_AVAILABLE_IN_2_72
+gboolean g_log_get_debug_enabled (void);
+GLIB_AVAILABLE_IN_2_72
+void g_log_set_debug_enabled (gboolean enabled);
+
/**
* G_DEBUG_HERE:
*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]