nautilus-3.6.3/libnautilus-private/nautilus-debug.c

No issues found

  1 /*
  2  * nautilus-debug: debug loggers for nautilus
  3  *
  4  * Copyright (C) 2007 Collabora Ltd.
  5  * Copyright (C) 2007 Nokia Corporation
  6  * Copyright (C) 2010 Red Hat, Inc.
  7  *
  8  * This library is free software; you can redistribute it and/or
  9  * modify it under the terms of the GNU Lesser General Public
 10  * License as published by the Free Software Foundation; either
 11  * version 2.1 of the License, or (at your option) any later version.
 12  *
 13  * This library is distributed in the hope that it will be useful,
 14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16  * Lesser General Public License for more details.
 17  *
 18  * You should have received a copy of the GNU Lesser General Public
 19  * License along with this library; if not, write to the Free Software
 20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 21  *
 22  * Based on Empathy's empathy-debug.
 23  */
 24 
 25 #include "config.h"
 26 
 27 #include <stdarg.h>
 28 #include <glib.h>
 29 
 30 #include "nautilus-debug.h"
 31 
 32 #include "nautilus-file.h"
 33 
 34 #ifdef ENABLE_DEBUG
 35 
 36 static DebugFlags flags = 0;
 37 static gboolean initialized = FALSE;
 38 
 39 static GDebugKey keys[] = {
 40   { "Application", NAUTILUS_DEBUG_APPLICATION },
 41   { "Bookmarks", NAUTILUS_DEBUG_BOOKMARKS },
 42   { "DBus", NAUTILUS_DEBUG_DBUS },
 43   { "DirectoryView", NAUTILUS_DEBUG_DIRECTORY_VIEW },
 44   { "File", NAUTILUS_DEBUG_FILE },
 45   { "CanvasContainer", NAUTILUS_DEBUG_CANVAS_CONTAINER },
 46   { "IconView", NAUTILUS_DEBUG_CANVAS_VIEW },
 47   { "ListView", NAUTILUS_DEBUG_LIST_VIEW },
 48   { "Mime", NAUTILUS_DEBUG_MIME },
 49   { "Places", NAUTILUS_DEBUG_PLACES },
 50   { "Previewer", NAUTILUS_DEBUG_PREVIEWER },
 51   { "Search", NAUTILUS_DEBUG_SEARCH },
 52   { "SearchHit", NAUTILUS_DEBUG_SEARCH_HIT },
 53   { "Smclient", NAUTILUS_DEBUG_SMCLIENT },
 54   { "Window", NAUTILUS_DEBUG_WINDOW },
 55   { "Undo", NAUTILUS_DEBUG_UNDO },
 56   { 0, }
 57 };
 58 
 59 static void
 60 nautilus_debug_set_flags_from_env ()
 61 {
 62   guint nkeys;
 63   const gchar *flags_string;
 64 
 65   for (nkeys = 0; keys[nkeys].value; nkeys++);
 66 
 67   flags_string = g_getenv ("NAUTILUS_DEBUG");
 68 
 69   if (flags_string)
 70     nautilus_debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
 71 
 72   initialized = TRUE;
 73 }
 74 
 75 void
 76 nautilus_debug_set_flags (DebugFlags new_flags)
 77 {
 78   flags |= new_flags;
 79   initialized = TRUE;
 80 }
 81 
 82 gboolean
 83 nautilus_debug_flag_is_set (DebugFlags flag)
 84 {
 85   return flag & flags;
 86 }
 87 
 88 void
 89 nautilus_debug (DebugFlags flag,
 90                 const gchar *format,
 91                 ...)
 92 {
 93   va_list args;
 94   va_start (args, format);
 95   nautilus_debug_valist (flag, format, args);
 96   va_end (args);
 97 }
 98 
 99 void
100 nautilus_debug_valist (DebugFlags flag,
101                        const gchar *format,
102                        va_list args)
103 {
104   if (G_UNLIKELY(!initialized))
105     nautilus_debug_set_flags_from_env ();
106 
107   if (flag & flags)
108     g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
109 }
110 
111 static void
112 nautilus_debug_files_valist (DebugFlags flag,
113                              GList *files,
114                              const gchar *format,
115                              va_list args)
116 {
117   NautilusFile *file;
118   GList *l;
119   gchar *uri, *msg;
120 
121   if (G_UNLIKELY (!initialized))
122     nautilus_debug_set_flags_from_env ();
123 
124   if (!(flag & flags))
125     return;
126 
127   msg = g_strdup_vprintf (format, args);
128 
129   g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s:", msg);
130 
131   for (l = files; l != NULL; l = l->next)
132     {
133       file = l->data;
134       uri = nautilus_file_get_uri (file);
135 
136       if (nautilus_file_is_gone (file)) {
137         gchar *new_uri;
138 
139         /* Hack: this will create an invalid URI, but it's for
140          * display purposes only.
141          */
142         new_uri = g_strconcat (uri ? uri : "", " (gone)", NULL);
143         g_free (uri);
144         uri = new_uri;
145       }
146 
147       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "   %s", uri);
148       g_free (uri);
149     }
150 
151   g_free (msg);
152 }
153 
154 void
155 nautilus_debug_files (DebugFlags flag,
156                       GList *files,
157                       const gchar *format,
158                       ...)
159 {
160   va_list args;
161 
162   va_start (args, format);
163   nautilus_debug_files_valist (flag, files, format, args);
164   va_end (args);
165 }
166 
167 #endif /* ENABLE_DEBUG */