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>
32 #ifdef HAVE_EXECINFO_H
36 static GHashTable *ev_profilers_hash = NULL;
37 static const char *ev_profile_modules = NULL;
38 static const char *ev_debug_break = NULL;
42 #ifndef DISABLE_LOGGING
44 static const char *ev_log_modules;
47 log_module (const gchar *log_domain,
48 GLogLevelFlags log_level,
52 gboolean should_log = FALSE;
54 if (!ev_log_modules) return;
56 if (strcmp (ev_log_modules, "all") != 0)
61 modules = g_strsplit (ev_log_modules, ":", 100);
63 for (i = 0; modules[i] != NULL; i++)
65 if (strstr (message, modules [i]) != NULL)
81 g_print ("%s\n", message);
88 trap_handler (const char *log_domain,
89 GLogLevelFlags log_level,
93 g_log_default_handler (log_domain, log_level, message, user_data);
95 if (ev_debug_break != NULL &&
96 (log_level & (G_LOG_LEVEL_WARNING |
98 G_LOG_LEVEL_CRITICAL |
101 if (strcmp (ev_debug_break, "stack") == 0)
103 #ifdef HAVE_EXECINFO_H
104 void *array[MAX_DEPTH];
107 size = backtrace (array, MAX_DEPTH);
108 backtrace_symbols_fd (array, size, 2);
110 g_on_error_stack_trace (g_get_prgname ());
113 else if (strcmp (ev_debug_break, "trap") == 0)
117 else if (strcmp (ev_debug_break, "suspend") == 0)
119 g_print ("Suspending program; attach with the debugger.\n");
131 #ifndef DISABLE_LOGGING
132 ev_log_modules = g_getenv ("EV_LOG_MODULES");
133 ev_debug_break = g_getenv ("EV_DEBUG_BREAK");
135 g_log_set_default_handler (trap_handler, NULL);
137 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, log_module, NULL);
140 #ifndef DISABLE_PROFILING
141 ev_profile_modules = g_getenv ("EV_PROFILE_MODULES");
145 #ifndef DISABLE_PROFILING
148 ev_profiler_new (const char *name, const char *module)
150 EvProfiler *profiler;
152 profiler = g_new0 (EvProfiler, 1);
153 profiler->timer = g_timer_new ();
154 profiler->name = g_strdup (name);
155 profiler->module = g_strdup (module);
157 g_timer_start (profiler->timer);
163 ev_should_profile (const char *module)
167 gboolean res = FALSE;
169 if (!ev_profile_modules) return FALSE;
170 if (strcmp (ev_profile_modules, "all") == 0) return TRUE;
172 modules = g_strsplit (ev_profile_modules, ":", 100);
174 for (i = 0; modules[i] != NULL; i++)
176 if (strcmp (module, modules [i]) == 0)
183 g_strfreev (modules);
189 ev_profiler_dump (EvProfiler *profiler)
193 g_return_if_fail (profiler != NULL);
195 seconds = g_timer_elapsed (profiler->timer, NULL);
197 g_print ("[ %s ] %s %f s elapsed\n",
198 profiler->module, profiler->name,
203 ev_profiler_free (EvProfiler *profiler)
205 g_return_if_fail (profiler != NULL);
207 g_timer_destroy (profiler->timer);
208 g_free (profiler->name);
209 g_free (profiler->module);
214 ev_profiler_start (const char *name, const char *module)
216 EvProfiler *profiler;
218 if (ev_profilers_hash == NULL)
221 g_hash_table_new_full (g_str_hash, g_str_equal,
225 if (!ev_should_profile (module)) return;
227 profiler = ev_profiler_new (name, module);
229 g_hash_table_insert (ev_profilers_hash, g_strdup (name), profiler);
233 ev_profiler_stop (const char *name)
235 EvProfiler *profiler;
237 profiler = g_hash_table_lookup (ev_profilers_hash, name);
238 if (profiler == NULL) return;
239 g_hash_table_remove (ev_profilers_hash, name);
241 ev_profiler_dump (profiler);
242 ev_profiler_free (profiler);