]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-debug.c
Add a profile mode available when debug is enabled. Add profilers in
[evince.git] / libdocument / ev-debug.c
1 /*
2  * ev-debug.c
3  * This file is part of Evince
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., 59 Temple Place, Suite 330, 
22  * Boston, MA 02111-1307, USA.
23  */
24  
25 /*
26  * Modified by the gedit Team, 1998-2005. See the AUTHORS file for a 
27  * list of people on the gedit Team.  
28  * See the ChangeLog files for a list of changes.
29  *
30  * $Id: gedit-debug.c 4809 2006-04-08 14:46:31Z pborelli $
31  */
32
33 /* Modified by Evince Team */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <stdio.h>
40
41 #include "ev-debug.h"
42
43 #ifdef EV_ENABLE_DEBUG
44 static EvDebugSection ev_debug = EV_NO_DEBUG;
45 static EvProfileSection ev_profile = EV_NO_PROFILE;
46
47 static GHashTable *timers = NULL;
48
49 static void
50 debug_init ()
51 {
52         if (g_getenv ("EV_DEBUG") != NULL) {
53                 /* enable all debugging */
54                 ev_debug = ~EV_NO_DEBUG;
55                 return;
56         }
57
58         if (g_getenv ("EV_DEBUG_JOBS") != NULL)
59                 ev_debug |= EV_DEBUG_JOBS;
60 }
61
62 static void
63 profile_init ()
64 {
65         if (g_getenv ("EV_PROFILE") != NULL) {
66                 /* enable all profiling */
67                 ev_profile = ~EV_NO_PROFILE;
68                 return;
69         }       
70
71         if (g_getenv ("EV_PROFILE_JOBS") != NULL)
72                 ev_profile |= EV_PROFILE_JOBS;
73
74         if (ev_profile) {
75                 timers = g_hash_table_new_full (g_str_hash,
76                                                 g_str_equal,
77                                                 (GDestroyNotify) g_free,
78                                                 (GDestroyNotify) g_timer_destroy);
79         }
80 }
81
82 void
83 ev_debug_init ()
84 {
85         debug_init ();
86         profile_init ();
87 }
88
89 void
90 ev_debug_shutdown ()
91 {
92         if (timers) {
93                 g_hash_table_destroy (timers);
94                 timers = NULL;
95         }
96 }
97
98 void
99 ev_debug_message (EvDebugSection  section,
100                   const gchar    *file,
101                   gint            line,
102                   const gchar    *function,
103                   const gchar    *format, ...)
104 {
105         if (G_UNLIKELY (ev_debug & section)) {
106                 gchar *msg = NULL;
107
108                 if (format) {
109                         va_list args;
110                         
111                         va_start (args, format);
112                         msg = g_strdup_vprintf (format, args);
113                         va_end (args);
114                 }
115
116                 g_print ("%s:%d (%s) %s\n", file, line, function, msg ? msg : "");      
117
118                 fflush (stdout);
119
120                 g_free (msg);
121         }
122 }
123
124 void
125 ev_profiler_start (EvProfileSection section,
126                    const gchar     *format, ...)
127 {
128         if (G_UNLIKELY (ev_profile & section)) {
129                 GTimer *timer;
130                 gchar  *name;
131                 va_list args;
132
133                 if (!format)
134                         return;
135
136                 va_start (args, format);
137                 name = g_strdup_vprintf (format, args);
138                 va_end (args);
139
140                 timer = g_hash_table_lookup (timers, name);
141                 if (!timer) {
142                         timer = g_timer_new ();
143                         g_hash_table_insert (timers, g_strdup (name), timer);
144                 } else {
145                         g_timer_start (timer);
146                 }
147         }
148 }
149
150 void
151 ev_profiler_stop (EvProfileSection section,
152                   const gchar     *format, ...)
153 {
154         if (G_UNLIKELY (ev_profile & section)) {
155                 GTimer *timer;
156                 gchar  *name;
157                 va_list args;
158                 gdouble seconds;
159
160                 if (!format)
161                         return;
162
163                 va_start (args, format);
164                 name = g_strdup_vprintf (format, args);
165                 va_end (args);
166                 
167                 timer = g_hash_table_lookup (timers, name);
168                 if (!timer)
169                         return;
170                 
171                 g_timer_stop (timer);
172                 seconds = g_timer_elapsed (timer, NULL);
173                 g_print ("[ %s ] %f s elapsed\n", name, seconds);
174                 fflush (stdout);
175         }
176 }
177
178 #endif /* EV_ENABLE_DEBUG */