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 */
27 #include "gconstructor.h"
37 * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
38 * includes the key <literal>gc-friendly</literal>.
40 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
41 gboolean g_mem_gc_friendly = TRUE;
43 gboolean g_mem_gc_friendly = FALSE;
45 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
46 G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
47 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
50 debug_key_matches (const gchar *key,
54 /* may not call GLib functions: see note in g_parse_debug_string() */
55 for (; length; length--, key++, token++)
57 char k = (*key == '_') ? '-' : tolower (*key );
58 char t = (*token == '_') ? '-' : tolower (*token);
68 * g_parse_debug_string:
69 * @string: (allow-none): a list of debug options separated by colons, spaces, or
71 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
72 * strings with bit flags.
73 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
75 * Parses a string containing debugging options
76 * into a %guint containing bit flags. This is used
77 * within GDK and GTK+ to parse the debug options passed on the
78 * command line or through environment variables.
80 * If @string is equal to <code>"all"</code>, all flags are set. Any flags
81 * specified along with <code>"all"</code> in @string are inverted; thus,
82 * <code>"all,foo,bar"</code> or <code>"foo,bar,all"</code> sets all flags
83 * except those corresponding to <code>"foo"</code> and <code>"bar"</code>.
85 * If @string is equal to <code>"help"</code>, all the available keys in @keys
86 * are printed out to standard error.
88 * Returns: the combined set of bit flags.
91 g_parse_debug_string (const gchar *string,
92 const GDebugKey *keys,
101 /* this function is used during the initialisation of gmessages, gmem
102 * and gslice, so it may not do anything that causes memory to be
103 * allocated or risks messages being emitted.
105 * this means, more or less, that this code may not call anything
109 if (!strcasecmp (string, "help"))
111 /* using stdio directly for the reason stated above */
112 fprintf (stderr, "Supported debug values:");
113 for (i = 0; i < nkeys; i++)
114 fprintf (stderr, " %s", keys[i].key);
115 fprintf (stderr, " all help\n");
119 const gchar *p = string;
121 gboolean invert = FALSE;
125 q = strpbrk (p, ":;, \t");
129 if (debug_key_matches ("all", p, q - p))
135 for (i = 0; i < nkeys; i++)
136 if (debug_key_matches (keys[i].key, p, q - p))
137 result |= keys[i].value;
149 for (i = 0; i < nkeys; i++)
150 all_flags |= keys[i].value;
152 result = all_flags & (~result);
160 g_parse_debug_envvar (const gchar *envvar,
161 const GDebugKey *keys,
168 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
171 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
176 value = getenv (envvar);
180 return default_value;
182 return g_parse_debug_string (value, keys, n_keys);
186 g_messages_prefixed_init (void)
188 const GDebugKey keys[] = {
189 { "error", G_LOG_LEVEL_ERROR },
190 { "critical", G_LOG_LEVEL_CRITICAL },
191 { "warning", G_LOG_LEVEL_WARNING },
192 { "message", G_LOG_LEVEL_MESSAGE },
193 { "info", G_LOG_LEVEL_INFO },
194 { "debug", G_LOG_LEVEL_DEBUG }
197 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
203 const GDebugKey keys[] = {
204 { "gc-friendly", 1 },
205 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
206 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
208 GLogLevelFlags flags;
210 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
212 g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
214 g_mem_gc_friendly = flags & 1;
220 g_messages_prefixed_init ();
224 #if defined (G_OS_WIN32)
226 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
233 DllMain (HINSTANCE hinstDLL,
239 case DLL_PROCESS_ATTACH:
241 g_clock_win32_init ();
243 g_thread_win32_init ();
248 case DLL_THREAD_DETACH:
250 g_thread_win32_thread_detach ();
262 #elif defined (G_HAS_CONSTRUCTORS)
264 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
265 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
267 G_DEFINE_CONSTRUCTOR(glib_init_ctor)
270 glib_init_ctor (void)
276 # error Your platform/compiler is missing constructor support