2 * Copyright (C) 2003 Marco Pesenti Gritti
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #ifndef DISABLE_PROFILING
27 #include <glib/gbacktrace.h>
34 static GHashTable *ev_profilers_hash = NULL;
35 static const char *ev_profile_modules = NULL;
36 static const char *ev_debug_break = NULL;
40 #ifndef DISABLE_LOGGING
42 static const char *ev_log_modules;
45 log_module (const gchar *log_domain,
46 GLogLevelFlags log_level,
50 gboolean should_log = FALSE;
52 if (!ev_log_modules) return;
54 if (strcmp (ev_log_modules, "all") != 0)
59 modules = g_strsplit (ev_log_modules, ":", 100);
61 for (i = 0; modules[i] != NULL; i++)
63 if (strstr (message, modules [i]) != NULL)
79 g_print ("%s\n", message);
86 trap_handler (const char *log_domain,
87 GLogLevelFlags log_level,
91 g_log_default_handler (log_domain, log_level, message, user_data);
93 if (ev_debug_break != NULL &&
94 (log_level & (G_LOG_LEVEL_WARNING |
96 G_LOG_LEVEL_CRITICAL |
99 if (strcmp (ev_debug_break, "stack") == 0)
101 void *array[MAX_DEPTH];
104 size = backtrace (array, MAX_DEPTH);
105 backtrace_symbols_fd (array, size, 2);
107 else if (strcmp (ev_debug_break, "trap") == 0)
111 else if (strcmp (ev_debug_break, "suspend") == 0)
113 g_print ("Suspending program; attach with the debugger.\n");
125 #ifndef DISABLE_LOGGING
126 ev_log_modules = g_getenv ("EV_LOG_MODULES");
127 ev_debug_break = g_getenv ("EV_DEBUG_BREAK");
129 g_log_set_default_handler (trap_handler, NULL);
131 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, log_module, NULL);
134 #ifndef DISABLE_PROFILING
135 ev_profile_modules = g_getenv ("EV_PROFILE_MODULES");
139 #ifndef DISABLE_PROFILING
142 ev_profiler_new (const char *name, const char *module)
144 EvProfiler *profiler;
146 profiler = g_new0 (EvProfiler, 1);
147 profiler->timer = g_timer_new ();
148 profiler->name = g_strdup (name);
149 profiler->module = g_strdup (module);
151 g_timer_start (profiler->timer);
157 ev_should_profile (const char *module)
161 gboolean res = FALSE;
163 if (!ev_profile_modules) return FALSE;
164 if (strcmp (ev_profile_modules, "all") == 0) return TRUE;
166 modules = g_strsplit (ev_profile_modules, ":", 100);
168 for (i = 0; modules[i] != NULL; i++)
170 if (strcmp (module, modules [i]) == 0)
177 g_strfreev (modules);
183 ev_profiler_dump (EvProfiler *profiler)
187 g_return_if_fail (profiler != NULL);
189 seconds = g_timer_elapsed (profiler->timer, NULL);
191 g_print ("[ %s ] %s %f s elapsed\n",
192 profiler->module, profiler->name,
197 ev_profiler_free (EvProfiler *profiler)
199 g_return_if_fail (profiler != NULL);
201 g_timer_destroy (profiler->timer);
202 g_free (profiler->name);
203 g_free (profiler->module);
208 ev_profiler_start (const char *name, const char *module)
210 EvProfiler *profiler;
212 if (ev_profilers_hash == NULL)
215 g_hash_table_new_full (g_str_hash, g_str_equal,
219 if (!ev_should_profile (module)) return;
221 profiler = ev_profiler_new (name, module);
223 g_hash_table_insert (ev_profilers_hash, g_strdup (name), profiler);
227 ev_profiler_stop (const char *name)
229 EvProfiler *profiler;
231 profiler = g_hash_table_lookup (ev_profilers_hash, name);
232 if (profiler == NULL) return;
233 g_hash_table_remove (ev_profilers_hash, name);
235 ev_profiler_dump (profiler);
236 ev_profiler_free (profiler);