]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-debug.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[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., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, 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         } else {
69                 if (g_getenv ("EV_PROFILE_JOBS") != NULL)
70                         ev_profile |= EV_PROFILE_JOBS;
71         }
72
73         if (ev_profile) {
74                 timers = g_hash_table_new_full (g_str_hash,
75                                                 g_str_equal,
76                                                 (GDestroyNotify) g_free,
77                                                 (GDestroyNotify) g_timer_destroy);
78         }
79 }
80
81 void
82 _ev_debug_init ()
83 {
84         debug_init ();
85         profile_init ();
86 }
87
88 void
89 _ev_debug_shutdown ()
90 {
91         if (timers) {
92                 g_hash_table_destroy (timers);
93                 timers = NULL;
94         }
95 }
96
97 void
98 ev_debug_message (EvDebugSection  section,
99                   const gchar    *file,
100                   gint            line,
101                   const gchar    *function,
102                   const gchar    *format, ...)
103 {
104         if (G_UNLIKELY (ev_debug & section)) {
105                 gchar *msg = NULL;
106
107                 if (format) {
108                         va_list args;
109                         
110                         va_start (args, format);
111                         msg = g_strdup_vprintf (format, args);
112                         va_end (args);
113                 }
114
115                 g_print ("%s:%d (%s) %s\n", file, line, function, msg ? msg : "");      
116
117                 fflush (stdout);
118
119                 g_free (msg);
120         }
121 }
122
123 void
124 ev_profiler_start (EvProfileSection section,
125                    const gchar     *format, ...)
126 {
127         if (G_UNLIKELY (ev_profile & section)) {
128                 GTimer *timer;
129                 gchar  *name;
130                 va_list args;
131
132                 if (!format)
133                         return;
134
135                 va_start (args, format);
136                 name = g_strdup_vprintf (format, args);
137                 va_end (args);
138
139                 timer = g_hash_table_lookup (timers, name);
140                 if (!timer) {
141                         timer = g_timer_new ();
142                         g_hash_table_insert (timers, g_strdup (name), timer);
143                 } else {
144                         g_timer_start (timer);
145                 }
146         }
147 }
148
149 void
150 ev_profiler_stop (EvProfileSection section,
151                   const gchar     *format, ...)
152 {
153         if (G_UNLIKELY (ev_profile & section)) {
154                 GTimer *timer;
155                 gchar  *name;
156                 va_list args;
157                 gdouble seconds;
158
159                 if (!format)
160                         return;
161
162                 va_start (args, format);
163                 name = g_strdup_vprintf (format, args);
164                 va_end (args);
165                 
166                 timer = g_hash_table_lookup (timers, name);
167                 if (!timer)
168                         return;
169                 
170                 g_timer_stop (timer);
171                 seconds = g_timer_elapsed (timer, NULL);
172                 g_print ("[ %s ] %f s elapsed\n", name, seconds);
173                 fflush (stdout);
174         }
175 }
176
177 #endif /* EV_ENABLE_DEBUG */