diff options
author | Sébastien Wilmet <swilmet@gnome.org> | 2015-05-13 15:37:38 +0200 |
---|---|---|
committer | Sébastien Wilmet <swilmet@gnome.org> | 2015-05-13 17:54:20 +0200 |
commit | dfcd5d7cfe4afc1c33edcdef24061f38ccc71fcc (patch) | |
tree | fe6ff9e0e16a7d9e3d3e94e78843e28cfabbfa13 | |
parent | d88af78cce19517b8bceb14aac768db87c99adf7 (diff) | |
download | gedit-wip/debug-utility.tar.gz |
GeditDebugUtility: a utility class for debuggingwip/debug-utility
-rw-r--r-- | gedit/Makefile.am | 2 | ||||
-rw-r--r-- | gedit/gedit-debug-utility.c | 191 | ||||
-rw-r--r-- | gedit/gedit-debug-utility.h | 53 | ||||
-rw-r--r-- | po/POTFILES.in | 1 |
4 files changed, 247 insertions, 0 deletions
diff --git a/gedit/Makefile.am b/gedit/Makefile.am index 15adf688e..1698345f3 100644 --- a/gedit/Makefile.am +++ b/gedit/Makefile.am @@ -101,6 +101,7 @@ gedit_built_sources = \ gedit_NOINST_H_FILES = \ gedit/gedit-close-confirmation-dialog.h \ gedit/gedit-commands-private.h \ + gedit/gedit-debug-utility.h \ gedit/gedit-dirs.h \ gedit/gedit-document-private.h \ gedit/gedit-documents-panel.h \ @@ -170,6 +171,7 @@ gedit_libgedit_c_files = \ gedit/gedit-commands-search.c \ gedit/gedit-commands-view.c \ gedit/gedit-debug.c \ + gedit/gedit-debug-utility.c \ gedit/gedit-dirs.c \ gedit/gedit-document.c \ gedit/gedit-documents-panel.c \ diff --git a/gedit/gedit-debug-utility.c b/gedit/gedit-debug-utility.c new file mode 100644 index 000000000..12e829fc7 --- /dev/null +++ b/gedit/gedit-debug-utility.c @@ -0,0 +1,191 @@ +/* + * gedit-debug-utility.c + * This file is part of gedit + * + * 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/>. + */ + +#include "gedit-debug-utility.h" +#include <stdio.h> + +#define ENABLE_PROFILING + +#ifdef ENABLE_PROFILING +static GTimer *timer = NULL; +static gdouble last_time = 0.0; +#endif + +struct _GeditDebugUtility +{ + GObject parent; + + guint64 enabled_sections; + guint64 all_sections; +}; + +G_DEFINE_TYPE (GeditDebugUtility, _gedit_debug_utility, G_TYPE_OBJECT) + +static gboolean +section_is_enabled (GeditDebugUtility *debug, + guint64 section) +{ + return (debug->enabled_sections & section) != 0; +} + +/* Checks that @section_flag contains only one flag (one bit at 1, all other + * bits at 0). + */ +static gboolean +is_valid_flag (guint64 section_flag) +{ + guint64 value = section_flag; + + while (value > 1) + { + if ((value % 2) == 1) + { + return FALSE; + } + + value >>= 1; + } + + return value == 1; +} + +static void +_gedit_debug_utility_class_init (GeditDebugUtilityClass *klass) +{ +} + +static void +_gedit_debug_utility_init (GeditDebugUtility *debug) +{ +#ifdef ENABLE_PROFILING + if (timer == NULL) + { + timer = g_timer_new (); + } +#endif +} + +GeditDebugUtility * +_gedit_debug_utility_new (void) +{ + return g_object_new (GEDIT_TYPE_DEBUG_UTILITY, NULL); +} + +void +_gedit_debug_utility_add_section (GeditDebugUtility *debug, + const gchar *section_name, + guint64 section_flag) +{ + guint64 all_sections_prev; + + g_return_if_fail (GEDIT_IS_DEBUG_UTILITY (debug)); + g_return_if_fail (is_valid_flag (section_flag)); + + all_sections_prev = debug->all_sections; + debug->all_sections |= section_flag; + + if (debug->all_sections == all_sections_prev) + { + g_warning ("Debug section '%s' uses the same flag value (%" G_GUINT64_FORMAT ") " + "as another section.", + section_name, + section_flag); + return; + } + + if (g_getenv (section_name) != NULL) + { + debug->enabled_sections |= section_flag; + } +} + +void +_gedit_debug_utility_add_all_section (GeditDebugUtility *debug, + const gchar *all_section_name) +{ + if (g_getenv (all_section_name) != NULL) + { + debug->enabled_sections = G_MAXUINT64; + } +} + +/** + * _gedit_debug_utility_message: + * @debug: a #GeditDebugUtility instance. + * @section: debug section. + * @file: file name. + * @line: line number. + * @function: name of the function that is calling gedit_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 +_gedit_debug_utility_message (GeditDebugUtility *debug, + guint64 section, + const gchar *file, + gint line, + const gchar *function, + const gchar *format, + ...) +{ + va_list args; + gchar *msg; +#ifdef ENABLE_PROFILING + gdouble cur_time; +#endif + + g_return_if_fail (GEDIT_IS_DEBUG_UTILITY (debug)); + g_return_if_fail (format != NULL); + + if (G_LIKELY (!section_is_enabled (debug, section))) + { + return; + } + + va_start (args, format); + msg = g_strdup_vprintf (format, args); + va_end (args); + +#ifdef ENABLE_PROFILING + cur_time = g_timer_elapsed (timer, NULL); + + g_print ("[%f (%f)] ", + cur_time, + cur_time - last_time); + + last_time = cur_time; +#endif + + g_print ("%s:%d (%s)%s%s\n", + file, + line, + function, + msg[0] != '\0' ? " " : "", /* avoid trailing space */ + msg); + + fflush (stdout); + g_free (msg); +} + +/* ex:set ts=8 noet: */ diff --git a/gedit/gedit-debug-utility.h b/gedit/gedit-debug-utility.h new file mode 100644 index 000000000..7cbfef38b --- /dev/null +++ b/gedit/gedit-debug-utility.h @@ -0,0 +1,53 @@ +/* + * gedit-debug-utility.h + * This file is part of gedit + * + * 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 __GEDIT_DEBUG_UTILITY_H__ +#define __GEDIT_DEBUG_UTILITY_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define GEDIT_TYPE_DEBUG_UTILITY _gedit_debug_utility_get_type () +G_DECLARE_FINAL_TYPE (GeditDebugUtility, _gedit_debug_utility, GEDIT, DEBUG_UTILITY, GObject) + +GeditDebugUtility * + _gedit_debug_utility_new (void); + +void _gedit_debug_utility_add_section (GeditDebugUtility *debug, + const gchar *section_name, + guint64 section_flag); + +void _gedit_debug_utility_add_all_section (GeditDebugUtility *debug, + const gchar *all_section_name); + +void _gedit_debug_utility_message (GeditDebugUtility *debug, + guint64 section, + const gchar *file, + gint line, + const gchar *function, + const gchar *format, + ...); + +G_END_DECLS + +#endif /* __GEDIT_DEBUG_UTILITY_H__ */ + +/* ex:set ts=8 noet: */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 822f2fb22..3e1f35199 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -12,6 +12,7 @@ gedit/gedit-commands-file.c gedit/gedit-commands-help.c gedit/gedit-commands-search.c gedit/gedit-debug.c +gedit/gedit-debug-utility.c gedit/gedit-document.c gedit/gedit-documents-panel.c gedit/gedit-encodings-combo-box.c |