Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-debug.c
Go to the documentation of this file.
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 #include <string.h>
41 
42 #include "ev-debug.h"
43 
44 #ifdef EV_ENABLE_DEBUG
45 static EvDebugSection ev_debug = EV_NO_DEBUG;
46 static EvProfileSection ev_profile = EV_NO_PROFILE;
47 static EvDebugBorders ev_debug_borders = EV_DEBUG_BORDER_NONE;
48 
49 static GHashTable *timers = NULL;
50 
51 static void
52 debug_init (void)
53 {
54  const GDebugKey keys[] = {
55  { "jobs", EV_DEBUG_JOBS },
56  { "borders", EV_DEBUG_SHOW_BORDERS }
57  };
58  const GDebugKey border_keys[] = {
59  { "chars", EV_DEBUG_BORDER_CHARS },
60  { "links", EV_DEBUG_BORDER_LINKS },
61  { "forms", EV_DEBUG_BORDER_FORMS },
62  { "annots", EV_DEBUG_BORDER_ANNOTS },
63  { "images", EV_DEBUG_BORDER_IMAGES },
64  { "media", EV_DEBUG_BORDER_MEDIA },
65  { "selections", EV_DEBUG_BORDER_SELECTIONS }
66  };
67 
68  ev_debug = g_parse_debug_string (g_getenv ("EV_DEBUG"), keys, G_N_ELEMENTS (keys));
69  if (ev_debug & EV_DEBUG_SHOW_BORDERS)
70  ev_debug_borders = g_parse_debug_string (g_getenv ("EV_DEBUG_SHOW_BORDERS"),
71  border_keys, G_N_ELEMENTS (border_keys));
72 }
73 
74 static void
75 profile_init (void)
76 {
77  if (g_getenv ("EV_PROFILE") != NULL) {
78  /* enable all profiling */
79  ev_profile = ~EV_NO_PROFILE;
80  } else {
81  if (g_getenv ("EV_PROFILE_JOBS") != NULL)
82  ev_profile |= EV_PROFILE_JOBS;
83  }
84 
85  if (ev_profile) {
86  timers = g_hash_table_new_full (g_str_hash,
87  g_str_equal,
88  (GDestroyNotify) g_free,
89  (GDestroyNotify) g_timer_destroy);
90  }
91 }
92 
93 void
94 _ev_debug_init (void)
95 {
96  debug_init ();
97  profile_init ();
98 }
99 
100 void
101 _ev_debug_shutdown (void)
102 {
103  if (timers) {
104  g_hash_table_destroy (timers);
105  timers = NULL;
106  }
107 }
108 
109 void
110 ev_debug_message (EvDebugSection section,
111  const gchar *file,
112  gint line,
113  const gchar *function,
114  const gchar *format, ...)
115 {
116  if (G_UNLIKELY (ev_debug & section)) {
117  gchar *msg = NULL;
118 
119  if (format) {
120  va_list args;
121 
122  va_start (args, format);
123  msg = g_strdup_vprintf (format, args);
124  va_end (args);
125  }
126 
127  g_print ("%s:%d (%s) %s\n", file, line, function, msg ? msg : "");
128 
129  fflush (stdout);
130 
131  g_free (msg);
132  }
133 }
134 
135 void
136 ev_profiler_start (EvProfileSection section,
137  const gchar *format, ...)
138 {
139  if (G_UNLIKELY (ev_profile & section)) {
140  GTimer *timer;
141  gchar *name;
142  va_list args;
143 
144  if (!format)
145  return;
146 
147  va_start (args, format);
148  name = g_strdup_vprintf (format, args);
149  va_end (args);
150 
151  timer = g_hash_table_lookup (timers, name);
152  if (!timer) {
153  timer = g_timer_new ();
154  g_hash_table_insert (timers, g_strdup (name), timer);
155  } else {
156  g_timer_start (timer);
157  }
158  }
159 }
160 
161 void
162 ev_profiler_stop (EvProfileSection section,
163  const gchar *format, ...)
164 {
165  if (G_UNLIKELY (ev_profile & section)) {
166  GTimer *timer;
167  gchar *name;
168  va_list args;
169  gdouble seconds;
170 
171  if (!format)
172  return;
173 
174  va_start (args, format);
175  name = g_strdup_vprintf (format, args);
176  va_end (args);
177 
178  timer = g_hash_table_lookup (timers, name);
179  if (!timer)
180  return;
181 
182  g_timer_stop (timer);
183  seconds = g_timer_elapsed (timer, NULL);
184  g_print ("[ %s ] %f s elapsed\n", name, seconds);
185  fflush (stdout);
186  }
187 }
188 
189 EvDebugBorders
190 ev_debug_get_debug_borders (void)
191 {
192  return ev_debug_borders;
193 }
194 
195 #endif /* EV_ENABLE_DEBUG */