2007-05-04 Federico Mena Quintero * gtk/gtkiconcache.c (log_cache_mapped): New function; logs when a cache file is mapped. (_gtk_icon_cache_new_for_path): Log the mapping of a cache. (log_get_icon): New function; logs when an icon is accessed. (_gtk_icon_cache_get_icon): Log when an icon is accessed. Index: gtk/gtkiconcache.c =================================================================== --- gtk/gtkiconcache.c (revision 17792) +++ gtk/gtkiconcache.c (working copy) @@ -34,6 +34,9 @@ #include #include #include +#include +#include +#include #ifndef _O_BINARY @@ -75,6 +78,88 @@ _gtk_icon_cache_unref (GtkIconCache *cac } } +static FILE *log_file; + +static gboolean +log_logging (void) +{ + static gboolean initialized; + static gboolean logging; + char *basename; + char *filename; + + if (initialized) + { + if (logging) + g_assert (log_file != NULL); + + return logging; + } + + initialized = TRUE; + + basename = g_strdup_printf ("%d-%s.icon-log", + getpid (), + g_get_prgname () ? g_get_prgname () : "unknown"); + + filename = g_build_filename (g_get_home_dir (), "gtk-icon-cache-logs", basename, NULL); + g_free (basename); + + log_file = g_fopen (filename, "wb"); + + if (log_file) + logging = TRUE; + else + g_warning ("Could not create %s - will not log activity in the icon cache: %s", filename, g_strerror (errno)); + + g_free (filename); + + return logging; +} + +static const char * +get_timestamp_str () +{ + struct timeval tv; + static char buf[100]; + + gettimeofday (&tv, NULL); + g_snprintf (buf, sizeof (buf), "%ld.%06d", (long) tv.tv_sec, (int) tv.tv_usec); + + return buf; +} + +static void +log_cache_mapped (const char *path, + GtkIconCache *cache) +{ + if (!log_logging ()) + return; + + fprintf (log_file, "%s cache_mapped: %p %s\n", get_timestamp_str (), cache, path); + fflush (log_file); +} + +static void +log_get_icon (GtkIconCache *cache, + const char *directory, + const char *icon_name, + gsize offset, + gsize length) +{ + if (!log_logging ()) + return; + + fprintf (log_file, "%s get_icon: cache:%p directory:'%s' icon_name:'%s' offset:%" G_GSIZE_FORMAT " length:%" G_GSIZE_FORMAT "\n", + get_timestamp_str (), + cache, + directory, + icon_name, + offset, + length); + fflush (log_file); +} + GtkIconCache * _gtk_icon_cache_new_for_path (const gchar *path) { @@ -138,6 +223,8 @@ _gtk_icon_cache_new_for_path (const gcha cache->map = map; cache->buffer = buffer; + log_cache_mapped (path, cache); + done: g_free (cache_filename); if (fd >= 0) @@ -346,6 +433,7 @@ _gtk_icon_cache_get_icon (GtkIconCache * GdkPixbuf *pixbuf; GdkPixdata pixdata; GError *error = NULL; + gsize pixdata_offset; offset = find_image_offset (cache, icon_name, directory); @@ -366,9 +454,10 @@ _gtk_icon_cache_get_icon (GtkIconCache * } length = GET_UINT32 (cache->buffer, pixel_data_offset + 4); + pixdata_offset = pixel_data_offset + 8; if (!gdk_pixdata_deserialize (&pixdata, length, - cache->buffer + pixel_data_offset + 8, + cache->buffer + pixdata_offset, &error)) { GTK_NOTE (ICONTHEME, @@ -394,6 +483,8 @@ _gtk_icon_cache_get_icon (GtkIconCache * _gtk_icon_cache_ref (cache); + log_get_icon (cache, directory, icon_name, pixdata_offset, length); + return pixbuf; }