[gedit/wip/gtef: 4/8] gtef: add debug functions
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/gtef: 4/8] gtef: add debug functions
- Date: Mon, 25 Apr 2016 18:22:38 +0000 (UTC)
commit 8f6fd49248fdf2405490ba2a33f3b5f05a7106ea
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon Apr 25 16:53:05 2016 +0200
gtef: add debug functions
- copy gedit-debug
- rename gedit -> gtef
- remove all sections except METADATA. The other sections will be added
one at a time, when needed.
- remove gtef_debug_plugin_message(), since the PLUGINS section has been
removed, and plugins are written against gedit, not Gtef (yet).
gedit/gtef/Makefile.am | 2 +
gedit/gtef/gtef-debug.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++
gedit/gtef/gtef-debug.h | 60 ++++++++++++++++++
3 files changed, 219 insertions(+), 0 deletions(-)
---
diff --git a/gedit/gtef/Makefile.am b/gedit/gtef/Makefile.am
index 1ab9807..f63da28 100644
--- a/gedit/gtef/Makefile.am
+++ b/gedit/gtef/Makefile.am
@@ -20,5 +20,7 @@ gedit_gtef_libgtef_la_LDFLAGS = \
-no-undefined
gedit_gtef_libgtef_la_SOURCES = \
+ gedit/gtef/gtef-debug.c \
+ gedit/gtef/gtef-debug.h \
gedit/gtef/gtef-utils.c \
gedit/gtef/gtef-utils.h
diff --git a/gedit/gtef/gtef-debug.c b/gedit/gtef/gtef-debug.c
new file mode 100644
index 0000000..e249c39
--- /dev/null
+++ b/gedit/gtef/gtef-debug.c
@@ -0,0 +1,157 @@
+/*
+ * gtef-debug.c
+ * This file is part of gtef
+ *
+ * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
+ * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
+ * Copyright (C) 2002 - 2005 Paolo Maggi
+ *
+ * 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 2 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/>.
+ */
+
+#include "gtef-debug.h"
+#include <stdio.h>
+
+#define ENABLE_PROFILING
+
+#ifdef ENABLE_PROFILING
+static GTimer *timer = NULL;
+static gdouble last_time = 0.0;
+#endif
+
+static GtefDebugSection enabled_sections = GTEF_NO_DEBUG;
+
+#define DEBUG_IS_ENABLED(section) (enabled_sections & (section))
+
+/**
+ * gtef_debug_init:
+ *
+ * Initializes the debugging subsystem of gtef.
+ *
+ * The function checks for the existence of certain environment variables to
+ * determine whether to enable output for a debug section. To enable output
+ * for a specific debug section, set an environment variable of the same name;
+ * e.g. to enable output for the %GTEF_DEBUG_METADATA section, set a
+ * <code>GTEF_DEBUG_METADATA</code> environment variable. To enable output
+ * for all debug sections, set the <code>GTEF_DEBUG</code> environment
+ * variable.
+ *
+ * This function must be called before any of the other debug functions are
+ * called. It must only be called once.
+ */
+void
+gtef_debug_init (void)
+{
+ if (g_getenv ("GTEF_DEBUG") != NULL)
+ {
+ /* enable all debugging */
+ enabled_sections = ~GTEF_NO_DEBUG;
+ goto out;
+ }
+
+ if (g_getenv ("GTEF_DEBUG_METADATA") != NULL)
+ {
+ enabled_sections |= GTEF_DEBUG_METADATA;
+ }
+
+out:
+
+#ifdef ENABLE_PROFILING
+ if (enabled_sections != GTEF_NO_DEBUG)
+ {
+ timer = g_timer_new ();
+ }
+#endif
+}
+
+/**
+ * gtef_debug:
+ * @section: debug section.
+ * @file: file name.
+ * @line: line number.
+ * @function: name of the function that is calling gtef_debug().
+ *
+ * If @section is enabled, then logs the trace information @file, @line, and
+ * @function.
+ */
+void
+gtef_debug (GtefDebugSection section,
+ const gchar *file,
+ gint line,
+ const gchar *function)
+{
+ gtef_debug_message (section, file, line, function, "%s", "");
+}
+
+/**
+ * gtef_debug_message:
+ * @section: debug section.
+ * @file: file name.
+ * @line: line number.
+ * @function: name of the function that is calling gtef_debug_message().
+ * @format: A g_vprintf() format string.
+ * @...: The format string arguments.
+ *
+ * If @section is enabled, then logs the trace information @file, @line, and
+ * @function along with the message obtained by formatting @format with the
+ * given format string arguments.
+ */
+void
+gtef_debug_message (GtefDebugSection section,
+ const gchar *file,
+ gint line,
+ const gchar *function,
+ const gchar *format,
+ ...)
+{
+ if (G_UNLIKELY (DEBUG_IS_ENABLED (section)))
+ {
+ va_list args;
+ gchar *msg;
+
+#ifdef ENABLE_PROFILING
+ gdouble seconds;
+
+ g_return_if_fail (timer != NULL);
+
+ seconds = g_timer_elapsed (timer, NULL);
+#endif
+
+ g_return_if_fail (format != NULL);
+
+ va_start (args, format);
+ msg = g_strdup_vprintf (format, args);
+ va_end (args);
+
+#ifdef ENABLE_PROFILING
+ g_print ("[%f (%f)] %s:%d (%s) %s\n",
+ seconds,
+ seconds - last_time,
+ file,
+ line,
+ function,
+ msg);
+
+ last_time = seconds;
+#else
+ g_print ("%s:%d (%s) %s\n", file, line, function, msg);
+#endif
+
+ fflush (stdout);
+
+ g_free (msg);
+ }
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gedit/gtef/gtef-debug.h b/gedit/gtef/gtef-debug.h
new file mode 100644
index 0000000..10cf86c
--- /dev/null
+++ b/gedit/gtef/gtef-debug.h
@@ -0,0 +1,60 @@
+/*
+ * gtef-debug.h
+ * This file is part of gtef
+ *
+ * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
+ * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
+ * Copyright (C) 2002 - 2005 Paolo Maggi
+ *
+ * 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 2 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/>.
+ */
+
+#ifndef __GTEF_DEBUG_H__
+#define __GTEF_DEBUG_H__
+
+#include <glib.h>
+
+/**
+ * GtefDebugSection:
+ *
+ * Enumeration of debug sections.
+ *
+ * Debugging output for a section is enabled by setting an environment variable
+ * of the same name. For example, setting the <code>GTEF_DEBUG_PLUGINS</code>
+ * environment variable enables all debugging output for the %GTEF_DEBUG_PLUGINS
+ * section. Setting the special environment variable <code>GTEF_DEBUG</code>
+ * enables output for all sections.
+ */
+typedef enum {
+ GTEF_NO_DEBUG = 0,
+ GTEF_DEBUG_METADATA = 1 << 0,
+} GtefDebugSection;
+
+#define DEBUG_METADATA GTEF_DEBUG_METADATA,__FILE__, __LINE__, G_STRFUNC
+
+void gtef_debug_init (void);
+
+void gtef_debug (GtefDebugSection section,
+ const gchar *file,
+ gint line,
+ const gchar *function);
+
+void gtef_debug_message (GtefDebugSection section,
+ const gchar *file,
+ gint line,
+ const gchar *function,
+ const gchar *format, ...) G_GNUC_PRINTF(5, 6);
+
+#endif /* __GTEF_DEBUG_H__ */
+/* ex:set ts=8 noet: */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]