Update German translation
[glib.git] / glib / glib-init.c
blob30a9654059da343c52ca005fe5b15805ecd0a38f
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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #include "glib-init.h"
24 #include "glib-private.h"
25 #include "gtypes.h"
26 #include "gutils.h" /* for GDebugKey */
27 #include "gconstructor.h"
28 #include "gmem.h" /* for g_mem_gc_friendly */
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
35 /* This seems as good a place as any to make static assertions about platform
36 * assumptions we make throughout GLib. */
38 /* We do not support 36-bit bytes or other historical curiosities. */
39 G_STATIC_ASSERT (CHAR_BIT == 8);
41 /* We assume that data pointers are the same size as function pointers... */
42 G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
43 G_STATIC_ASSERT (_g_alignof (gpointer) == _g_alignof (GFunc));
44 /* ... and that all function pointers are the same size. */
45 G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
46 G_STATIC_ASSERT (_g_alignof (GFunc) == _g_alignof (GCompareDataFunc));
48 /* We assume that "small" enums (those where all values fit in INT32_MIN
49 * to INT32_MAX) are exactly int-sized. In particular, we assume that if
50 * an enum has no members that exceed the range of char/short, the
51 * compiler will make it int-sized anyway, so adding a member later that
52 * *does* exceed the range of char/short is not an ABI break. */
53 typedef enum {
54 TEST_CHAR_0 = 0
55 } TestChar;
56 typedef enum {
57 TEST_SHORT_0 = 0,
58 TEST_SHORT_256 = 256
59 } TestShort;
60 typedef enum {
61 TEST_INT32_MIN = G_MININT32,
62 TEST_INT32_MAX = G_MAXINT32
63 } TestInt;
64 G_STATIC_ASSERT (sizeof (TestChar) == sizeof (int));
65 G_STATIC_ASSERT (sizeof (TestShort) == sizeof (int));
66 G_STATIC_ASSERT (sizeof (TestInt) == sizeof (int));
67 G_STATIC_ASSERT (_g_alignof (TestChar) == _g_alignof (int));
68 G_STATIC_ASSERT (_g_alignof (TestShort) == _g_alignof (int));
69 G_STATIC_ASSERT (_g_alignof (TestInt) == _g_alignof (int));
71 /**
72 * g_mem_gc_friendly:
74 * This variable is %TRUE if the `G_DEBUG` environment variable
75 * includes the key `gc-friendly`.
77 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
78 gboolean g_mem_gc_friendly = TRUE;
79 #else
80 gboolean g_mem_gc_friendly = FALSE;
81 #endif
82 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
83 G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
84 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
86 static gboolean
87 debug_key_matches (const gchar *key,
88 const gchar *token,
89 guint length)
91 /* may not call GLib functions: see note in g_parse_debug_string() */
92 for (; length; length--, key++, token++)
94 char k = (*key == '_') ? '-' : tolower (*key );
95 char t = (*token == '_') ? '-' : tolower (*token);
97 if (k != t)
98 return FALSE;
101 return *key == '\0';
104 /* The GVariant documentation indirectly says that int is at least 32 bits
105 * (by saying that b, y, n, q, i, u, h are promoted to int). On any
106 * reasonable platform, int is in fact *exactly* 32 bits long, because
107 * otherwise, {signed char, short, int} wouldn't be sufficient to provide
108 * {int8_t, int16_t, int32_t}. */
109 G_STATIC_ASSERT (sizeof (int) == sizeof (gint32));
112 * g_parse_debug_string:
113 * @string: (nullable): a list of debug options separated by colons, spaces, or
114 * commas, or %NULL.
115 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
116 * strings with bit flags.
117 * @nkeys: the number of #GDebugKeys in the array.
119 * Parses a string containing debugging options
120 * into a %guint containing bit flags. This is used
121 * within GDK and GTK+ to parse the debug options passed on the
122 * command line or through environment variables.
124 * If @string is equal to "all", all flags are set. Any flags
125 * specified along with "all" in @string are inverted; thus,
126 * "all,foo,bar" or "foo,bar,all" sets all flags except those
127 * corresponding to "foo" and "bar".
129 * If @string is equal to "help", all the available keys in @keys
130 * are printed out to standard error.
132 * Returns: the combined set of bit flags.
134 guint
135 g_parse_debug_string (const gchar *string,
136 const GDebugKey *keys,
137 guint nkeys)
139 guint i;
140 guint result = 0;
142 if (string == NULL)
143 return 0;
145 /* this function is used during the initialisation of gmessages, gmem
146 * and gslice, so it may not do anything that causes memory to be
147 * allocated or risks messages being emitted.
149 * this means, more or less, that this code may not call anything
150 * inside GLib.
153 if (!strcasecmp (string, "help"))
155 /* using stdio directly for the reason stated above */
156 fprintf (stderr, "Supported debug values:");
157 for (i = 0; i < nkeys; i++)
158 fprintf (stderr, " %s", keys[i].key);
159 fprintf (stderr, " all help\n");
161 else
163 const gchar *p = string;
164 const gchar *q;
165 gboolean invert = FALSE;
167 while (*p)
169 q = strpbrk (p, ":;, \t");
170 if (!q)
171 q = p + strlen (p);
173 if (debug_key_matches ("all", p, q - p))
175 invert = TRUE;
177 else
179 for (i = 0; i < nkeys; i++)
180 if (debug_key_matches (keys[i].key, p, q - p))
181 result |= keys[i].value;
184 p = q;
185 if (*p)
186 p++;
189 if (invert)
191 guint all_flags = 0;
193 for (i = 0; i < nkeys; i++)
194 all_flags |= keys[i].value;
196 result = all_flags & (~result);
200 return result;
203 static guint
204 g_parse_debug_envvar (const gchar *envvar,
205 const GDebugKey *keys,
206 gint n_keys,
207 guint default_value)
209 const gchar *value;
211 #ifdef OS_WIN32
212 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
213 gchar buffer[100];
215 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
216 value = buffer;
217 else
218 return 0;
219 #else
220 value = getenv (envvar);
221 #endif
223 if (value == NULL)
224 return default_value;
226 return g_parse_debug_string (value, keys, n_keys);
229 static void
230 g_messages_prefixed_init (void)
232 const GDebugKey keys[] = {
233 { "error", G_LOG_LEVEL_ERROR },
234 { "critical", G_LOG_LEVEL_CRITICAL },
235 { "warning", G_LOG_LEVEL_WARNING },
236 { "message", G_LOG_LEVEL_MESSAGE },
237 { "info", G_LOG_LEVEL_INFO },
238 { "debug", G_LOG_LEVEL_DEBUG }
241 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
244 static void
245 g_debug_init (void)
247 const GDebugKey keys[] = {
248 { "gc-friendly", 1 },
249 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
250 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
252 GLogLevelFlags flags;
254 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
256 g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
258 g_mem_gc_friendly = flags & 1;
261 void
262 glib_init (void)
264 static gboolean glib_inited;
266 if (glib_inited)
267 return;
269 glib_inited = TRUE;
271 g_messages_prefixed_init ();
272 g_debug_init ();
273 g_quark_init ();
276 #if defined (G_OS_WIN32)
278 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
279 DWORD fdwReason,
280 LPVOID lpvReserved);
282 HMODULE glib_dll;
284 BOOL WINAPI
285 DllMain (HINSTANCE hinstDLL,
286 DWORD fdwReason,
287 LPVOID lpvReserved)
289 switch (fdwReason)
291 case DLL_PROCESS_ATTACH:
292 glib_dll = hinstDLL;
293 g_clock_win32_init ();
294 #ifdef THREADS_WIN32
295 g_thread_win32_init ();
296 #endif
297 glib_init ();
298 break;
300 case DLL_THREAD_DETACH:
301 #ifdef THREADS_WIN32
302 g_thread_win32_thread_detach ();
303 #endif
304 break;
306 case DLL_PROCESS_DETACH:
307 #ifdef THREADS_WIN32
308 if (lpvReserved == NULL)
309 g_thread_win32_process_detach ();
310 #endif
311 break;
313 default:
314 /* do nothing */
318 return TRUE;
321 #elif defined (G_HAS_CONSTRUCTORS)
323 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
324 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
325 #endif
326 G_DEFINE_CONSTRUCTOR(glib_init_ctor)
328 static void
329 glib_init_ctor (void)
331 glib_init ();
334 #else
335 # error Your platform/compiler is missing constructor support
336 #endif