docs: Fix GNetworkAddress typo
[glib.git] / glib / glib-init.c
blob5442a97056e2e8cbb13623b31653d6082c300cc6
1 /*
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,
17 * USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "config.h"
24 #include "glib-init.h"
26 #include "gutils.h" /* for GDebugKey */
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
33 /**
34 * g_mem_gc_friendly:
36 * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
37 * includes the key <literal>gc-friendly</literal>.
39 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
40 gboolean g_mem_gc_friendly = TRUE;
41 #else
42 gboolean g_mem_gc_friendly = FALSE;
43 #endif
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;
48 static gboolean
49 debug_key_matches (const gchar *key,
50 const gchar *token,
51 guint length)
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);
59 if (k != t)
60 return FALSE;
63 return *key == '\0';
66 /**
67 * g_parse_debug_string:
68 * @string: (allow-none): a list of debug options separated by colons, spaces, or
69 * commas, or %NULL.
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.
89 guint
90 g_parse_debug_string (const gchar *string,
91 const GDebugKey *keys,
92 guint nkeys)
94 guint i;
95 guint result = 0;
97 if (string == NULL)
98 return 0;
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
105 * inside GLib.
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");
116 else
118 const gchar *p = string;
119 const gchar *q;
120 gboolean invert = FALSE;
122 while (*p)
124 q = strpbrk (p, ":;, \t");
125 if (!q)
126 q = p + strlen (p);
128 if (debug_key_matches ("all", p, q - p))
130 invert = TRUE;
132 else
134 for (i = 0; i < nkeys; i++)
135 if (debug_key_matches (keys[i].key, p, q - p))
136 result |= keys[i].value;
139 p = q;
140 if (*p)
141 p++;
144 if (invert)
146 guint all_flags = 0;
148 for (i = 0; i < nkeys; i++)
149 all_flags |= keys[i].value;
151 result = all_flags & (~result);
155 return result;
158 static guint
159 g_parse_debug_envvar (const gchar *envvar,
160 const GDebugKey *keys,
161 gint n_keys)
163 const gchar *value;
165 #ifdef OS_WIN32
166 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
167 gchar buffer[80];
169 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
170 value = buffer;
171 else
172 return 0;
173 #else
174 value = getenv (envvar);
175 #endif
177 return g_parse_debug_string (value, keys, n_keys);
180 static void
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));
195 static void
196 g_debug_init (void)
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;
212 static void
213 glib_init (void)
215 g_messages_prefixed_init ();
216 g_debug_init ();
219 #if defined (G_OS_WIN32)
221 HMODULE glib_dll;
223 BOOL WINAPI
224 DllMain (HINSTANCE hinstDLL,
225 DWORD fdwReason,
226 LPVOID lpvReserved)
228 switch (fdwReason)
230 case DLL_PROCESS_ATTACH:
231 glib_dll = hinstDLL;
232 g_clock_win32_init ();
233 g_thread_win32_init ();
234 glib_init ();
235 break;
237 case DLL_THREAD_DETACH:
238 g_thread_win32_thread_detach ();
239 break;
241 default:
242 /* do nothing */
246 return TRUE;
249 #elif defined (__GNUC__)
251 __attribute__ ((constructor)) static void
252 glib_init_ctor (void)
254 glib_init ();
257 #else
258 # error Your platform/compiler is missing constructor support
259 #endif