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 "all", all flags are set. If @string
80 * is equal to "help", all the available keys in @keys are printed
81 * out to standard error.
83 * Returns: the combined set of bit flags.
86 g_parse_debug_string (const gchar *string,
87 const GDebugKey *keys,
96 /* this function is used during the initialisation of gmessages, gmem
97 * and gslice, so it may not do anything that causes memory to be
98 * allocated or risks messages being emitted.
100 * this means, more or less, that this code may not call anything
104 if (!strcasecmp (string, "all"))
106 for (i = 0; i < nkeys; i++)
107 result |= keys[i].value;
109 else 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, "\n");
119 const gchar *p = string;
124 q = strpbrk (p, ":;, \t");
128 for (i = 0; i < nkeys; i++)
129 if (debug_key_matches (keys[i].key, p, q - p))
130 result |= keys[i].value;
142 g_parse_debug_envvar (const gchar *envvar,
143 const GDebugKey *keys,
149 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
152 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
157 value = getenv (envvar);
160 return g_parse_debug_string (value, keys, n_keys);
164 g_messages_prefixed_init (void)
166 const GDebugKey keys[] = {
167 { "error", G_LOG_LEVEL_ERROR },
168 { "critical", G_LOG_LEVEL_CRITICAL },
169 { "warning", G_LOG_LEVEL_WARNING },
170 { "message", G_LOG_LEVEL_MESSAGE },
171 { "info", G_LOG_LEVEL_INFO },
172 { "debug", G_LOG_LEVEL_DEBUG }
175 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys));
181 const GDebugKey keys[] = {
182 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
183 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
185 GLogLevelFlags flags;
187 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
189 g_log_always_fatal |= flags;
195 const GDebugKey keys[] = {
196 { "gc-friendly", 1 },
199 if (g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys)))
200 g_mem_gc_friendly = TRUE;
206 g_messages_prefixed_init ();
211 #if defined (G_OS_WIN32)
216 DllMain (HINSTANCE hinstDLL,
222 case DLL_PROCESS_ATTACH:
224 g_thread_win32_init ();
236 #elif defined (__GNUC__)
238 __attribute__ ((constructor)) static void
239 glib_init_ctor (void)
245 # error Your platform/compiler is missing constructor support