]> www.fi.muni.cz Git - evince.git/blobdiff - libdocument/ev-debug.c
Don't overwite an error.
[evince.git] / libdocument / ev-debug.c
index 3806fd45d87cb963db001b789423db39b8cb8b42..c6918e7be6494730ff1bf83e3efd18feea7c88bc 100644 (file)
 
 #ifdef EV_ENABLE_DEBUG
 static EvDebugSection ev_debug = EV_NO_DEBUG;
+static EvProfileSection ev_profile = EV_NO_PROFILE;
 
-void
-ev_debug_init ()
+static GHashTable *timers = NULL;
+
+static void
+debug_init ()
 {
        if (g_getenv ("EV_DEBUG") != NULL) {
                /* enable all debugging */
@@ -56,6 +59,41 @@ ev_debug_init ()
                ev_debug |= EV_DEBUG_JOBS;
 }
 
+static void
+profile_init ()
+{
+       if (g_getenv ("EV_PROFILE") != NULL) {
+               /* enable all profiling */
+               ev_profile = ~EV_NO_PROFILE;
+       } else {
+               if (g_getenv ("EV_PROFILE_JOBS") != NULL)
+                       ev_profile |= EV_PROFILE_JOBS;
+       }
+
+       if (ev_profile) {
+               timers = g_hash_table_new_full (g_str_hash,
+                                               g_str_equal,
+                                               (GDestroyNotify) g_free,
+                                               (GDestroyNotify) g_timer_destroy);
+       }
+}
+
+void
+_ev_debug_init ()
+{
+       debug_init ();
+       profile_init ();
+}
+
+void
+_ev_debug_shutdown ()
+{
+       if (timers) {
+               g_hash_table_destroy (timers);
+               timers = NULL;
+       }
+}
+
 void
 ev_debug_message (EvDebugSection  section,
                  const gchar    *file,
@@ -82,4 +120,58 @@ ev_debug_message (EvDebugSection  section,
        }
 }
 
+void
+ev_profiler_start (EvProfileSection section,
+                  const gchar     *format, ...)
+{
+       if (G_UNLIKELY (ev_profile & section)) {
+               GTimer *timer;
+               gchar  *name;
+               va_list args;
+
+               if (!format)
+                       return;
+
+               va_start (args, format);
+               name = g_strdup_vprintf (format, args);
+               va_end (args);
+
+               timer = g_hash_table_lookup (timers, name);
+               if (!timer) {
+                       timer = g_timer_new ();
+                       g_hash_table_insert (timers, g_strdup (name), timer);
+               } else {
+                       g_timer_start (timer);
+               }
+       }
+}
+
+void
+ev_profiler_stop (EvProfileSection section,
+                 const gchar     *format, ...)
+{
+       if (G_UNLIKELY (ev_profile & section)) {
+               GTimer *timer;
+               gchar  *name;
+               va_list args;
+               gdouble seconds;
+
+               if (!format)
+                       return;
+
+               va_start (args, format);
+               name = g_strdup_vprintf (format, args);
+               va_end (args);
+               
+               timer = g_hash_table_lookup (timers, name);
+               if (!timer)
+                       return;
+               
+               g_timer_stop (timer);
+               seconds = g_timer_elapsed (timer, NULL);
+               g_print ("[ %s ] %f s elapsed\n", name, seconds);
+               fflush (stdout);
+       }
+}
+
 #endif /* EV_ENABLE_DEBUG */