2 * Copyright © 2011 Canonical Limited
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * 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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "glib-init.h"
26 #include "gutils.h" /* for GDebugKey */
36 * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
37 * includes the key <link linkend="G_DEBUG">gc-friendly</link>.
39 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
40 gboolean g_mem_gc_friendly = TRUE;
42 gboolean g_mem_gc_friendly = FALSE;
44 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
45 G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
46 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
49 debug_key_matches (const gchar *key,
53 /* may not call GLib functions: see note in g_parse_debug_string() */
54 for (; length; length--, key++, token++)
56 char k = (*key == '_') ? '-' : tolower (*key );
57 char t = (*token == '_') ? '-' : tolower (*token);
67 * g_parse_debug_string:
68 * @string: (allow-none): a list of debug options separated by colons, spaces, or
70 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
71 * strings with bit flags.
72 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
74 * Parses a string containing debugging options
75 * into a %guint containing bit flags. This is used
76 * within GDK and GTK+ to parse the debug options passed on the
77 * command line or through environment variables.
79 * If @string is equal to <code>"all"</code>, all flags are set. Any flags
80 * specified along with <code>"all"</code> in @string are inverted; thus,
81 * <code>"all,foo,bar"</code> or <code>"foo,bar,all"</code> sets all flags
82 * except those corresponding to <code>"foo"</code> and <code>"bar"</code>.
84 * If @string is equal to <code>"help"</code>, all the available keys in @keys
85 * are printed out to standard error.
87 * Returns: the combined set of bit flags.
90 g_parse_debug_string (const gchar *string,
91 const GDebugKey *keys,
100 /* this function is used during the initialisation of gmessages, gmem
101 * and gslice, so it may not do anything that causes memory to be
102 * allocated or risks messages being emitted.
104 * this means, more or less, that this code may not call anything
108 if (!strcasecmp (string, "help"))
110 /* using stdio directly for the reason stated above */
111 fprintf (stderr, "Supported debug values:");
112 for (i = 0; i < nkeys; i++)
113 fprintf (stderr, " %s", keys[i].key);
114 fprintf (stderr, " all help\n");
118 const gchar *p = string;
120 gboolean invert = FALSE;
124 q = strpbrk (p, ":;, \t");
128 if (debug_key_matches ("all", p, q - p))
134 for (i = 0; i < nkeys; i++)
135 if (debug_key_matches (keys[i].key, p, q - p))
136 result |= keys[i].value;
148 for (i = 0; i < nkeys; i++)
149 all_flags |= keys[i].value;
151 result = all_flags & (~result);
159 g_parse_debug_envvar (const gchar *envvar,
160 const GDebugKey *keys,
166 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
169 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
174 value = getenv (envvar);
177 return g_parse_debug_string (value, keys, n_keys);
181 g_messages_prefixed_init (void)
183 const GDebugKey keys[] = {
184 { "error", G_LOG_LEVEL_ERROR },
185 { "critical", G_LOG_LEVEL_CRITICAL },
186 { "warning", G_LOG_LEVEL_WARNING },
187 { "message", G_LOG_LEVEL_MESSAGE },
188 { "info", G_LOG_LEVEL_INFO },
189 { "debug", G_LOG_LEVEL_DEBUG }
192 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys));
198 const GDebugKey keys[] = {
199 { "gc-friendly", 1 },
200 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
201 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
203 GLogLevelFlags flags;
205 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
207 g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
209 g_mem_gc_friendly = flags & 1;
215 g_messages_prefixed_init ();
219 #if defined (G_OS_WIN32)
224 DllMain (HINSTANCE hinstDLL,
230 case DLL_PROCESS_ATTACH:
232 g_thread_win32_init ();
236 case DLL_THREAD_DETACH:
237 g_thread_win32_thread_detach ();
248 #elif defined (__GNUC__)
250 __attribute__ ((constructor)) static void
251 glib_init_ctor (void)
257 # error Your platform/compiler is missing constructor support