1 /*
2  * xed-debug.c
3  * This file is part of xed
4  *
5  * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
6  * Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
7  * Copyright (C) 2002 - 2005 Paolo Maggi
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 /*
26  * Modified by the xed Team, 1998-2005. See the AUTHORS file for a
27  * list of people on the xed Team.
28  * See the ChangeLog files for a list of changes.
29  *
30  * $Id$
31  */
32 
33 #include <config.h>
34 #include <stdio.h>
35 
36 #include "xed-debug.h"
37 
38 #define ENABLE_PROFILING
39 
40 #ifdef ENABLE_PROFILING
41 static GTimer *timer = NULL;
42 static gdouble last = 0.0;
43 #endif
44 
45 static XedDebugSection debug = XED_NO_DEBUG;
46 
47 void
xed_debug_init(void)48 xed_debug_init (void)
49 {
50 	if (g_getenv ("XED_DEBUG") != NULL)
51 	{
52 		/* enable all debugging */
53 		debug = ~XED_NO_DEBUG;
54 		goto out;
55 	}
56 
57 	if (g_getenv ("XED_DEBUG_VIEW") != NULL)
58 		debug = debug | XED_DEBUG_VIEW;
59 	if (g_getenv ("XED_DEBUG_SEARCH") != NULL)
60 		debug = debug | XED_DEBUG_SEARCH;
61 	if (g_getenv ("XED_DEBUG_PREFS") != NULL)
62 		debug = debug | XED_DEBUG_PREFS;
63 	if (g_getenv ("XED_DEBUG_PRINT") != NULL)
64 		debug = debug | XED_DEBUG_PRINT;
65 	if (g_getenv ("XED_DEBUG_PLUGINS") != NULL)
66 		debug = debug | XED_DEBUG_PLUGINS;
67 	if (g_getenv ("XED_DEBUG_TAB") != NULL)
68 		debug = debug | XED_DEBUG_TAB;
69 	if (g_getenv ("XED_DEBUG_DOCUMENT") != NULL)
70 		debug = debug | XED_DEBUG_DOCUMENT;
71 	if (g_getenv ("XED_DEBUG_COMMANDS") != NULL)
72 		debug = debug | XED_DEBUG_COMMANDS;
73 	if (g_getenv ("XED_DEBUG_APP") != NULL)
74 		debug = debug | XED_DEBUG_APP;
75 	if (g_getenv ("XED_DEBUG_SESSION") != NULL)
76 		debug = debug | XED_DEBUG_SESSION;
77 	if (g_getenv ("XED_DEBUG_UTILS") != NULL)
78 		debug = debug | XED_DEBUG_UTILS;
79 	if (g_getenv ("XED_DEBUG_METADATA") != NULL)
80 		debug = debug | XED_DEBUG_METADATA;
81 	if (g_getenv ("XED_DEBUG_WINDOW") != NULL)
82 		debug = debug | XED_DEBUG_WINDOW;
83 	if (g_getenv ("XED_DEBUG_LOADER") != NULL)
84 		debug = debug | XED_DEBUG_LOADER;
85 	if (g_getenv ("XED_DEBUG_SAVER") != NULL)
86 		debug = debug | XED_DEBUG_SAVER;
87 
88 out:
89 
90 #ifdef ENABLE_PROFILING
91 	if (debug != XED_NO_DEBUG)
92 		timer = g_timer_new ();
93 #endif
94 	return;
95 }
96 
97 void
xed_debug_message(XedDebugSection section,const gchar * file,gint line,const gchar * function,const gchar * format,...)98 xed_debug_message (XedDebugSection  section,
99 		     const gchar       *file,
100 		     gint               line,
101 		     const gchar       *function,
102 		     const gchar       *format, ...)
103 {
104 	if (G_UNLIKELY (debug & section))
105 	{
106 #ifdef ENABLE_PROFILING
107 		gdouble seconds;
108 		g_return_if_fail (timer != NULL);
109 #endif
110 
111 		va_list args;
112 		gchar *msg;
113 
114 		g_return_if_fail (format != NULL);
115 
116 		va_start (args, format);
117 		msg = g_strdup_vprintf (format, args);
118 		va_end (args);
119 
120 #ifdef ENABLE_PROFILING
121 		seconds = g_timer_elapsed (timer, NULL);
122 		g_print ("[%f (%f)] %s:%d (%s) %s\n",
123 			 seconds, seconds - last,  file, line, function, msg);
124 		last = seconds;
125 #else
126 		g_print ("%s:%d (%s) %s\n", file, line, function, msg);
127 #endif
128 
129 		fflush (stdout);
130 
131 		g_free (msg);
132 	}
133 }
134 
xed_debug(XedDebugSection section,const gchar * file,gint line,const gchar * function)135 void xed_debug (XedDebugSection  section,
136 		  const gchar       *file,
137 		  gint               line,
138 		  const gchar       *function)
139 {
140 	if (G_UNLIKELY (debug & section))
141 	{
142 #ifdef ENABLE_PROFILING
143 		gdouble seconds;
144 
145 		g_return_if_fail (timer != NULL);
146 
147 		seconds = g_timer_elapsed (timer, NULL);
148 		g_print ("[%f (%f)] %s:%d (%s)\n",
149 			 seconds, seconds - last, file, line, function);
150 		last = seconds;
151 #else
152 		g_print ("%s:%d (%s)\n", file, line, function);
153 #endif
154 		fflush (stdout);
155 	}
156 }
157