1 /*
2  * xapp-debug: debug loggers for xapp
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  * Copied from nemo
23  */
24 
25 #include <stdarg.h>
26 
27 #include <glib.h>
28 
29 #include "xapp-debug.h"
30 
31 static DebugFlags flags = 0;
32 static gboolean initialized = FALSE;
33 
34 static GDebugKey keys[] = {
35   { "GtkWindow", XAPP_DEBUG_WINDOW },
36   { "Favorites", XAPP_DEBUG_FAVORITES },
37   { "FavoriteVfs", XAPP_DEBUG_FAVORITE_VFS },
38   { "StatusIcon", XAPP_DEBUG_STATUS_ICON },
39   { "SnWatcher", XAPP_DEBUG_SN_WATCHER },
40   { "GtkModule", XAPP_DEBUG_MODULE},
41   { 0, }
42 };
43 
44 static void
xapp_debug_set_flags_from_env(void)45 xapp_debug_set_flags_from_env (void)
46 {
47   guint nkeys;
48   const gchar *flags_string;
49 
50   for (nkeys = 0; keys[nkeys].value; nkeys++);
51 
52   flags_string = g_getenv ("XAPP_DEBUG");
53 
54   if (flags_string)
55     xapp_debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys));
56 
57   initialized = TRUE;
58 }
59 
60 void
xapp_debug_set_flags(DebugFlags new_flags)61 xapp_debug_set_flags (DebugFlags new_flags)
62 {
63   flags |= new_flags;
64   initialized = TRUE;
65 }
66 
67 gboolean
xapp_debug_flag_is_set(DebugFlags flag)68 xapp_debug_flag_is_set (DebugFlags flag)
69 {
70   return flag & flags;
71 }
72 
73 void
xapp_debug(DebugFlags flag,const gchar * format,...)74 xapp_debug (DebugFlags flag,
75                 const gchar *format,
76                 ...)
77 {
78   va_list args;
79   va_start (args, format);
80   xapp_debug_valist (flag, format, args);
81   va_end (args);
82 }
83 
84 void
xapp_debug_valist(DebugFlags flag,const gchar * format,va_list args)85 xapp_debug_valist (DebugFlags flag,
86                        const gchar *format,
87                        va_list args)
88 {
89   if (G_UNLIKELY(!initialized))
90     xapp_debug_set_flags_from_env ();
91 
92   if (flag & flags)
93     g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
94 }
95 
96