1 /*
2  * Copyright (C) 2010-2011 Nick Schermer <nick@xfce.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26 
27 #include <glib.h>
28 #include <common/panel-debug.h>
29 #include <common/panel-private.h>
30 
31 
32 
33 static PanelDebugFlag panel_debug_flags = 0;
34 
35 
36 
37 /* additional debug levels */
38 static const GDebugKey panel_debug_keys[] =
39 {
40   /* external plugin proxy modes */
41   { "gdb", PANEL_DEBUG_GDB },
42   { "valgrind", PANEL_DEBUG_VALGRIND },
43 
44   /* domains for debug messages in the code */
45   { "application", PANEL_DEBUG_APPLICATION },
46   { "applicationsmenu", PANEL_DEBUG_APPLICATIONSMENU },
47   { "base-window", PANEL_DEBUG_BASE_WINDOW },
48   { "display-layout", PANEL_DEBUG_DISPLAY_LAYOUT },
49   { "external", PANEL_DEBUG_EXTERNAL },
50   { "main", PANEL_DEBUG_MAIN },
51   { "module-factory", PANEL_DEBUG_MODULE_FACTORY },
52   { "module", PANEL_DEBUG_MODULE },
53   { "positioning", PANEL_DEBUG_POSITIONING },
54   { "struts", PANEL_DEBUG_STRUTS },
55   { "systray", PANEL_DEBUG_SYSTRAY },
56   { "tasklist", PANEL_DEBUG_TASKLIST },
57   { "pager", PANEL_DEBUG_PAGER }
58 };
59 
60 
61 
62 static PanelDebugFlag
panel_debug_init(void)63 panel_debug_init (void)
64 {
65   static volatile gsize  inited__volatile = 0;
66   const gchar           *value;
67 
68   if (g_once_init_enter (&inited__volatile))
69     {
70       value = g_getenv ("PANEL_DEBUG");
71       if (value != NULL && *value != '\0')
72         {
73           panel_debug_flags = g_parse_debug_string (value, panel_debug_keys,
74                                                     G_N_ELEMENTS (panel_debug_keys));
75 
76           /* always enable (unfiltered) debugging messages */
77           PANEL_SET_FLAG (panel_debug_flags, PANEL_DEBUG_YES);
78 
79           /* unset gdb and valgrind in 'all' mode */
80           if (g_ascii_strcasecmp (value, "all") == 0)
81             PANEL_UNSET_FLAG (panel_debug_flags, PANEL_DEBUG_GDB | PANEL_DEBUG_VALGRIND);
82         }
83 
84       g_once_init_leave (&inited__volatile, 1);
85     }
86 
87   return panel_debug_flags;
88 }
89 
90 
91 
92 static void
panel_debug_print(PanelDebugFlag domain,const gchar * message,va_list args)93 panel_debug_print (PanelDebugFlag  domain,
94                    const gchar    *message,
95                    va_list         args)
96 {
97   gchar       *string;
98   const gchar *domain_name = NULL;
99   guint        i;
100 
101   /* lookup domain name */
102   for (i = 0; i < G_N_ELEMENTS (panel_debug_keys); i++)
103     {
104       if (panel_debug_keys[i].value == domain)
105         {
106           domain_name = panel_debug_keys[i].key;
107           break;
108         }
109     }
110 
111   panel_assert (domain_name != NULL);
112 
113   string = g_strdup_vprintf (message, args);
114   g_printerr (PACKAGE_NAME "(%s): %s\n", domain_name, string);
115   g_free (string);
116 }
117 
118 
119 
120 gboolean
panel_debug_has_domain(PanelDebugFlag domain)121 panel_debug_has_domain (PanelDebugFlag domain)
122 {
123   return PANEL_HAS_FLAG (panel_debug_flags, domain);
124 }
125 
126 
127 
128 void
panel_debug(PanelDebugFlag domain,const gchar * message,...)129 panel_debug (PanelDebugFlag  domain,
130              const gchar    *message,
131              ...)
132 {
133   va_list args;
134 
135   panel_return_if_fail (domain > 0);
136   panel_return_if_fail (message != NULL);
137 
138   /* leave when debug is disabled */
139   if (panel_debug_init () == 0)
140     return;
141 
142   va_start (args, message);
143   panel_debug_print (domain, message, args);
144   va_end (args);
145 }
146 
147 
148 
149 void
panel_debug_filtered(PanelDebugFlag domain,const gchar * message,...)150 panel_debug_filtered (PanelDebugFlag  domain,
151                       const gchar    *message,
152                       ...)
153 {
154   va_list args;
155 
156   panel_return_if_fail (domain > 0);
157   panel_return_if_fail (message != NULL);
158 
159   /* leave when the filter does not match */
160   if (!PANEL_HAS_FLAG (panel_debug_init (), domain))
161     return;
162 
163   va_start (args, message);
164   panel_debug_print (domain, message, args);
165   va_end (args);
166 }
167