[gedit/wip/gcode/debug: 1/3] Create gcode



commit 7b2e5702075a3570fdea7bd4b6852dcce6980149
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat May 2 18:36:38 2015 +0200

    Create gcode
    
    With gcode-debug, a copy of gedit-debug.
    
    The goal of gcode is to create a library to create text editors easily.
    gcode will remain internal to gedit. The long-term goal is to move the
    code to GtkSourceView. With gcode we don't need to pay attention to API
    stability, we can do refactorings later to improve the APIs. But ideally
    the gedit API (for plugins) must not be broken.

 Makefile.am         |    1 +
 gcode/Makefile.am   |   23 ++++++++
 gcode/gcode-debug.c |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gcode/gcode-debug.h |   61 ++++++++++++++++++++
 po/POTFILES.in      |    1 +
 5 files changed, 243 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index d5725e6..5df5a13 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -95,6 +95,7 @@ include win32/Makefile.am
 include osx/bundle/data/Makefile.am
 include tests/Makefile.am
 include plugins/Makefile.am
+include gcode/Makefile.am
 include gedit/Makefile.am
 
 @GSETTINGS_RULES@
diff --git a/gcode/Makefile.am b/gcode/Makefile.am
new file mode 100644
index 0000000..d492a91
--- /dev/null
+++ b/gcode/Makefile.am
@@ -0,0 +1,23 @@
+noinst_LTLIBRARIES += gcode/libgcode.la
+
+gcode_libgcode_la_CPPFLAGS =           \
+       -I$(top_srcdir)/gcode           \
+       -I$(top_builddir)/gcode
+
+gcode_libgcode_la_CFLAGS =             \
+       $(GEDIT_CFLAGS)                 \
+       $(GTK_MAC_CFLAGS)               \
+       $(WARN_CFLAGS)                  \
+       $(DISABLE_DEPRECATED_CFLAGS)    \
+       $(INTROSPECTION_CFLAGS)
+
+gcode_libgcode_la_LIBADD =             \
+       $(GEDIT_LIBS)                   \
+       $(GTK_MAC_LIBS)                 \
+       $(INTROSPECTION_LIBS)
+
+gcode_libgcode_la_LDFLAGS = -no-undefined
+
+gcode_libgcode_la_SOURCES =                    \
+       gcode/gcode-debug.c                     \
+       gcode/gcode-debug.h
diff --git a/gcode/gcode-debug.c b/gcode/gcode-debug.c
new file mode 100644
index 0000000..c18f1df
--- /dev/null
+++ b/gcode/gcode-debug.c
@@ -0,0 +1,157 @@
+/*
+ * gcode-debug.c
+ * This file is part of gcode
+ *
+ * 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 "gcode-debug.h"
+#include <stdio.h>
+
+#define ENABLE_PROFILING
+
+#ifdef ENABLE_PROFILING
+static GTimer *timer = NULL;
+static gdouble last_time = 0.0;
+#endif
+
+static GcodeDebugSection enabled_sections = GCODE_NO_DEBUG;
+
+#define DEBUG_IS_ENABLED(section) (enabled_sections & (section))
+
+/**
+ * gcode_debug_init:
+ *
+ * Initializes the debugging subsystem of gcode.
+ *
+ * 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 %GCODE_DEBUG_PLUGINS section, set a
+ * <code>GCODE_DEBUG_PLUGINS</code> environment variable. To enable output
+ * for all debug sections, set the <code>GCODE_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
+gcode_debug_init (void)
+{
+       if (g_getenv ("GCODE_DEBUG") != NULL)
+       {
+               /* enable all debugging */
+               enabled_sections = ~GCODE_NO_DEBUG;
+               goto out;
+       }
+
+       if (g_getenv ("GCODE_DEBUG_UTILS") != NULL)
+       {
+               enabled_sections |= GCODE_DEBUG_UTILS;
+       }
+
+out:
+
+#ifdef ENABLE_PROFILING
+       if (enabled_sections != GCODE_NO_DEBUG)
+       {
+               timer = g_timer_new ();
+       }
+#endif
+}
+
+/**
+ * gcode_debug:
+ * @section: debug section.
+ * @file: file name.
+ * @line: line number.
+ * @function: name of the function that is calling gcode_debug().
+ *
+ * If @section is enabled, then logs the trace information @file, @line, and
+ * @function.
+ */
+void
+gcode_debug (GcodeDebugSection  section,
+            const gchar       *file,
+            gint               line,
+            const gchar       *function)
+{
+       gcode_debug_message (section, file, line, function, "%s", "");
+}
+
+/**
+ * gcode_debug_message:
+ * @section: debug section.
+ * @file: file name.
+ * @line: line number.
+ * @function: name of the function that is calling gcode_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
+gcode_debug_message (GcodeDebugSection  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/gcode/gcode-debug.h b/gcode/gcode-debug.h
new file mode 100644
index 0000000..b74121a
--- /dev/null
+++ b/gcode/gcode-debug.h
@@ -0,0 +1,61 @@
+/*
+ * gcode-debug.h
+ * This file is part of gcode
+ *
+ * Copyright (C) 1998, 1999 - Alex Roberts, Evan Lawrence
+ * Copyright (C) 2000, 2001 - Chema Celorio, Paolo Maggi
+ * Copyright (C) 2002 - 2005 - Paolo Maggi
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * 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 __GCODE_DEBUG_H__
+#define __GCODE_DEBUG_H__
+
+#include <glib.h>
+
+/**
+ * GcodeDebugSection:
+ *
+ * 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>GCODE_DEBUG_PLUGINS</code>
+ * environment variable enables all debugging output for the %GCODE_DEBUG_PLUGINS
+ * section. Setting the special environment variable <code>GCODE_DEBUG</code>
+ * enables output for all sections.
+ */
+typedef enum {
+       GCODE_NO_DEBUG       = 0,
+       GCODE_DEBUG_UTILS    = 1 << 0,
+} GcodeDebugSection;
+
+#define        DEBUG_UTILS     GCODE_DEBUG_UTILS,   __FILE__, __LINE__, G_STRFUNC
+
+void gcode_debug_init (void);
+
+void gcode_debug (GcodeDebugSection  section,
+                 const gchar       *file,
+                 gint               line,
+                 const gchar       *function);
+
+void gcode_debug_message (GcodeDebugSection  section,
+                         const gchar       *file,
+                         gint               line,
+                         const gchar       *function,
+                         const gchar       *format, ...) G_GNUC_PRINTF(5, 6);
+
+#endif /* __GCODE_DEBUG_H__ */
+/* ex:set ts=8 noet: */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 822f2fb..4c49e64 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -4,6 +4,7 @@
 data/org.gnome.gedit.appdata.xml.in
 data/org.gnome.gedit.desktop.in.in
 [type: gettext/gsettings]data/org.gnome.gedit.gschema.xml.in
+gcode/gcode-debug.c
 gedit/gedit-app.c
 gedit/gedit-app-osx.c
 gedit/gedit.c


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