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"
28 #include "gmem.h" /* for g_mem_gc_friendly */
38 * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
39 * includes the key <literal>gc-friendly</literal>.
41 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
42 gboolean g_mem_gc_friendly = TRUE;
44 gboolean g_mem_gc_friendly = FALSE;
46 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
47 G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
48 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
51 debug_key_matches (const gchar *key,
55 /* may not call GLib functions: see note in g_parse_debug_string() */
56 for (; length; length--, key++, token++)
58 char k = (*key == '_') ? '-' : tolower (*key );
59 char t = (*token == '_') ? '-' : tolower (*token);
69 * g_parse_debug_string:
70 * @string: (allow-none): a list of debug options separated by colons, spaces, or
72 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
73 * strings with bit flags.
74 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
76 * Parses a string containing debugging options
77 * into a %guint containing bit flags. This is used
78 * within GDK and GTK+ to parse the debug options passed on the
79 * command line or through environment variables.
81 * If @string is equal to <code>"all"</code>, all flags are set. Any flags
82 * specified along with <code>"all"</code> in @string are inverted; thus,
83 * <code>"all,foo,bar"</code> or <code>"foo,bar,all"</code> sets all flags
84 * except those corresponding to <code>"foo"</code> and <code>"bar"</code>.
86 * If @string is equal to <code>"help"</code>, all the available keys in @keys
87 * are printed out to standard error.
89 * Returns: the combined set of bit flags.
92 g_parse_debug_string (const gchar *string,
93 const GDebugKey *keys,
102 /* this function is used during the initialisation of gmessages, gmem
103 * and gslice, so it may not do anything that causes memory to be
104 * allocated or risks messages being emitted.
106 * this means, more or less, that this code may not call anything
110 if (!strcasecmp (string, "help"))
112 /* using stdio directly for the reason stated above */
113 fprintf (stderr, "Supported debug values:");
114 for (i = 0; i < nkeys; i++)
115 fprintf (stderr, " %s", keys[i].key);
116 fprintf (stderr, " all help\n");
120 const gchar *p = string;
122 gboolean invert = FALSE;
126 q = strpbrk (p, ":;, \t");
130 if (debug_key_matches ("all", p, q - p))
136 for (i = 0; i < nkeys; i++)
137 if (debug_key_matches (keys[i].key, p, q - p))
138 result |= keys[i].value;
150 for (i = 0; i < nkeys; i++)
151 all_flags |= keys[i].value;
153 result = all_flags & (~result);
161 g_parse_debug_envvar (const gchar *envvar,
162 const GDebugKey *keys,
169 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
172 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
177 value = getenv (envvar);
181 return default_value;
183 return g_parse_debug_string (value, keys, n_keys);
187 g_messages_prefixed_init (void)
189 const GDebugKey keys[] = {
190 { "error", G_LOG_LEVEL_ERROR },
191 { "critical", G_LOG_LEVEL_CRITICAL },
192 { "warning", G_LOG_LEVEL_WARNING },
193 { "message", G_LOG_LEVEL_MESSAGE },
194 { "info", G_LOG_LEVEL_INFO },
195 { "debug", G_LOG_LEVEL_DEBUG }
198 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
204 const GDebugKey keys[] = {
205 { "gc-friendly", 1 },
206 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
207 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
209 GLogLevelFlags flags;
211 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
213 g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
215 g_mem_gc_friendly = flags & 1;
221 g_messages_prefixed_init ();
225 #if defined (G_OS_WIN32)
227 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
234 DllMain (HINSTANCE hinstDLL,
240 case DLL_PROCESS_ATTACH:
242 g_clock_win32_init ();
244 g_thread_win32_init ();
249 case DLL_THREAD_DETACH:
251 g_thread_win32_thread_detach ();
263 #elif defined (G_HAS_CONSTRUCTORS)
265 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
266 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
268 G_DEFINE_CONSTRUCTOR(glib_init_ctor)
271 glib_init_ctor (void)
277 # error Your platform/compiler is missing constructor support