gedit-debug.c 6.06 KB
Newer Older
1
/*
2 3
 * gedit-debug.c
 * This file is part of gedit
4
 *
5
 * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
Paolo Borelli's avatar
Paolo Borelli committed
6
 * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
Paolo Borelli's avatar
Paolo Borelli committed
7
 * Copyright (C) 2002 - 2005 Paolo Maggi
8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21
 */
Paolo Borelli's avatar
Paolo Borelli committed
22

23
#include "gedit-debug.h"
24 25
#include <stdio.h>

Paolo Borelli's avatar
Paolo Borelli committed
26 27 28 29
#define ENABLE_PROFILING

#ifdef ENABLE_PROFILING
static GTimer *timer = NULL;
30
static gdouble last_time = 0.0;
Paolo Borelli's avatar
Paolo Borelli committed
31 32
#endif

33
static GeditDebugSection enabled_sections = GEDIT_NO_DEBUG;
34

35
#define DEBUG_IS_ENABLED(section) (enabled_sections & (section))
36

37 38 39
/**
 * gedit_debug_init:
 *
40
 * Initializes the debugging subsystem of gedit.
41 42 43 44
 *
 * 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;
Paolo Borelli's avatar
Paolo Borelli committed
45
 * e.g. to enable output for the %GEDIT_DEBUG_PLUGINS section, set a
46 47 48 49 50 51 52
 * <code>GEDIT_DEBUG_PLUGINS</code> environment variable. To enable output
 * for all debug sections, set the <code>GEDIT_DEBUG</code> environment
 * variable.
 *
 * This function must be called before any of the other debug functions are
 * called. It must only be called once.
 */
53
void
Paolo Borelli's avatar
Paolo Borelli committed
54
gedit_debug_init (void)
55
{
56 57 58
	if (g_getenv ("GEDIT_DEBUG") != NULL)
	{
		/* enable all debugging */
59
		enabled_sections = ~GEDIT_NO_DEBUG;
Paolo Borelli's avatar
Paolo Borelli committed
60
		goto out;
61 62 63
	}

	if (g_getenv ("GEDIT_DEBUG_VIEW") != NULL)
64 65 66
	{
		enabled_sections |= GEDIT_DEBUG_VIEW;
	}
67
	if (g_getenv ("GEDIT_DEBUG_PREFS") != NULL)
68 69 70 71 72 73 74 75 76 77 78
	{
		enabled_sections |= GEDIT_DEBUG_PREFS;
	}
	if (g_getenv ("GEDIT_DEBUG_WINDOW") != NULL)
	{
		enabled_sections |= GEDIT_DEBUG_WINDOW;
	}
	if (g_getenv ("GEDIT_DEBUG_PANEL") != NULL)
	{
		enabled_sections |= GEDIT_DEBUG_PANEL;
	}
79
	if (g_getenv ("GEDIT_DEBUG_PLUGINS") != NULL)
80 81 82
	{
		enabled_sections |= GEDIT_DEBUG_PLUGINS;
	}
83
	if (g_getenv ("GEDIT_DEBUG_TAB") != NULL)
84 85 86
	{
		enabled_sections |= GEDIT_DEBUG_TAB;
	}
87
	if (g_getenv ("GEDIT_DEBUG_DOCUMENT") != NULL)
88 89 90
	{
		enabled_sections |= GEDIT_DEBUG_DOCUMENT;
	}
91
	if (g_getenv ("GEDIT_DEBUG_COMMANDS") != NULL)
92 93 94
	{
		enabled_sections |= GEDIT_DEBUG_COMMANDS;
	}
Paolo Borelli's avatar
Paolo Borelli committed
95
	if (g_getenv ("GEDIT_DEBUG_APP") != NULL)
96 97 98
	{
		enabled_sections |= GEDIT_DEBUG_APP;
	}
99
	if (g_getenv ("GEDIT_DEBUG_UTILS") != NULL)
100 101 102
	{
		enabled_sections |= GEDIT_DEBUG_UTILS;
	}
103
	if (g_getenv ("GEDIT_DEBUG_METADATA") != NULL)
104 105 106 107
	{
		enabled_sections |= GEDIT_DEBUG_METADATA;
	}

108
out:
Paolo Borelli's avatar
Paolo Borelli committed
109 110

#ifdef ENABLE_PROFILING
111 112
	if (enabled_sections != GEDIT_NO_DEBUG)
	{
Paolo Borelli's avatar
Paolo Borelli committed
113
		timer = g_timer_new ();
114
	}
Paolo Borelli's avatar
Paolo Borelli committed
115
#endif
116
}
Paolo Maggi's avatar
Paolo Maggi committed
117

118 119
/**
 * gedit_debug:
120 121 122 123
 * @section: debug section.
 * @file: file name.
 * @line: line number.
 * @function: name of the function that is calling gedit_debug().
124
 *
125 126
 * If @section is enabled, then logs the trace information @file, @line, and
 * @function.
127
 */
128 129 130 131 132
void
gedit_debug (GeditDebugSection  section,
	     const gchar       *file,
	     gint               line,
	     const gchar       *function)
133
{
134
	gedit_debug_message (section, file, line, function, "%s", "");
135 136
}

137 138
/**
 * gedit_debug_message:
139 140 141 142
 * @section: debug section.
 * @file: file name.
 * @line: line number.
 * @function: name of the function that is calling gedit_debug_message().
143 144 145
 * @format: A g_vprintf() format string.
 * @...: The format string arguments.
 *
146 147 148
 * 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.
149
 */
150
void
Paolo Borelli's avatar
Paolo Borelli committed
151 152 153 154
gedit_debug_message (GeditDebugSection  section,
		     const gchar       *file,
		     gint               line,
		     const gchar       *function,
155 156
		     const gchar       *format,
		     ...)
157
{
158
	if (G_UNLIKELY (DEBUG_IS_ENABLED (section)))
Paolo Borelli's avatar
Paolo Borelli committed
159
	{
160 161 162
		va_list args;
		gchar *msg;

Paolo Borelli's avatar
Paolo Borelli committed
163 164 165
#ifdef ENABLE_PROFILING
		gdouble seconds;

166
		g_return_if_fail (timer != NULL);
167 168

		seconds = g_timer_elapsed (timer, NULL);
169
#endif
Paolo Maggi's avatar
Paolo Maggi committed
170 171 172 173 174 175

		g_return_if_fail (format != NULL);

		va_start (args, format);
		msg = g_strdup_vprintf (format, args);
		va_end (args);
176

Paolo Borelli's avatar
Paolo Borelli committed
177
#ifdef ENABLE_PROFILING
Paolo Borelli's avatar
Paolo Borelli committed
178
		g_print ("[%f (%f)] %s:%d (%s) %s\n",
179 180 181 182 183 184 185 186
			 seconds,
			 seconds - last_time,
			 file,
			 line,
			 function,
			 msg);

		last_time = seconds;
Paolo Borelli's avatar
Paolo Borelli committed
187
#else
Paolo Borelli's avatar
Paolo Borelli committed
188
		g_print ("%s:%d (%s) %s\n", file, line, function, msg);
Paolo Borelli's avatar
Paolo Borelli committed
189
#endif
190 191

		fflush (stdout);
Paolo Borelli's avatar
Paolo Borelli committed
192 193

		g_free (msg);
Paolo Maggi's avatar
Paolo Maggi committed
194
	}
195
}
196

197 198
/**
 * gedit_debug_plugin_message:
199 200 201 202
 * @file: file name.
 * @line: line number.
 * @function: name of the function that is calling gedit_debug_plugin_message().
 * @message: a message.
203
 *
204 205
 * If the section %GEDIT_DEBUG_PLUGINS is enabled, then logs the trace
 * information @file, @line, and @function along with @message.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
 *
 * This function may be overridden by GObject Introspection language bindings
 * to be more language-specific.
 *
 * <emphasis>Python</emphasis>
 *
 * A PyGObject override is provided that has the following signature:
 * <informalexample>
 *   <programlisting>
 *     def debug_plugin_message(format_str, *format_args):
 *         #...
 *   </programlisting>
 * </informalexample>
 *
 * It automatically supplies parameters @file, @line, and @function, and it
 * formats <code>format_str</code> with the given format arguments. The syntax
 * of the format string is the usual Python string formatting syntax described
 * by <ulink url="http://docs.python.org/library/stdtypes.html#string-formatting">5.6.2. String Formatting Operations</ulink>.
 *
 * Since: 3.4
 */
void
gedit_debug_plugin_message (const gchar       *file,
			    gint               line,
			    const gchar       *function,
			    const gchar       *message)
{
233
	gedit_debug_message (GEDIT_DEBUG_PLUGINS, file, line, function, "%s", message);
234 235
}

236
/* ex:set ts=8 noet: */