Deprecate g_thread_init()
[platform/upstream/glib.git] / glib / glib-init.c
1 /*
2  * Copyright © 2011 Canonical Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "glib-init.h"
25
26 #include "gutils.h"     /* for GDebugKey */
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
32
33 /**
34  * g_mem_gc_friendly:
35  *
36  * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
37  * includes the key <link linkend="G_DEBUG">gc-friendly</link>.
38  */
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;
47
48 static gboolean
49 debug_key_matches (const gchar *key,
50                    const gchar *token,
51                    guint        length)
52 {
53   /* may not call GLib functions: see note in g_parse_debug_string() */
54   for (; length; length--, key++, token++)
55     {
56       char k = (*key   == '_') ? '-' : tolower (*key  );
57       char t = (*token == '_') ? '-' : tolower (*token);
58
59       if (k != t)
60         return FALSE;
61     }
62
63   return *key == '\0';
64 }
65
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.
73  *
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.
78  *
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.
82  *
83  * Returns: the combined set of bit flags.
84  */
85 guint
86 g_parse_debug_string  (const gchar     *string,
87                        const GDebugKey *keys,
88                        guint            nkeys)
89 {
90   guint i;
91   guint result = 0;
92
93   if (string == NULL)
94     return 0;
95
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.
99    *
100    * this means, more or less, that this code may not call anything
101    * inside GLib.
102    */
103
104   if (!strcasecmp (string, "all"))
105     {
106       for (i = 0; i < nkeys; i++)
107        result |= keys[i].value;
108     }
109   else if (!strcasecmp (string, "help"))
110     {
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");
116     }
117   else
118     {
119       const gchar *p = string;
120       const gchar *q;
121
122       while (*p)
123        {
124          q = strpbrk (p, ":;, \t");
125          if (!q)
126            q = p + strlen(p);
127
128          for (i = 0; i < nkeys; i++)
129            if (debug_key_matches (keys[i].key, p, q - p))
130              result |= keys[i].value;
131
132          p = q;
133          if (*p)
134            p++;
135        }
136     }
137
138   return result;
139 }
140
141 static guint
142 g_parse_debug_envvar (const gchar     *envvar,
143                       const GDebugKey *keys,
144                       gint             n_keys)
145 {
146   const gchar *value;
147
148 #ifdef OS_WIN32
149   /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
150   gchar buffer[80];
151
152   if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
153     value = buffer;
154   else
155     return 0;
156 #else
157   value = getenv (envvar);
158 #endif
159
160   return g_parse_debug_string (value, keys, n_keys);
161 }
162
163 static void
164 g_messages_prefixed_init (void)
165 {
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 }
173   };
174
175   g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys));
176 }
177
178 static void
179 g_debug_init (void)
180 {
181   const GDebugKey keys[] = {
182     {"fatal-warnings",  G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
183     {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
184   };
185   GLogLevelFlags flags;
186
187   flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
188
189   g_log_always_fatal |= flags;
190 }
191
192 static void
193 g_mem_init (void)
194 {
195   const GDebugKey keys[] = {
196     { "gc-friendly", 1 },
197   };
198
199   if (g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys)))
200     g_mem_gc_friendly = TRUE;
201 }
202
203 static void
204 glib_init (void)
205 {
206   g_messages_prefixed_init ();
207   g_debug_init ();
208   g_mem_init ();
209 }
210
211 #if defined (G_OS_WIN32)
212
213 HMODULE glib_dll;
214
215 BOOL WINAPI
216 DllMain (HINSTANCE hinstDLL,
217          DWORD     fdwReason,
218          LPVOID    lpvReserved)
219 {
220   switch (fdwReason)
221     {
222     case DLL_PROCESS_ATTACH:
223       glib_dll = hinstDLL;
224       g_thread_win32_init ();
225       glib_init ();
226       break;
227
228     default:
229       /* do nothing */
230       ;
231     }
232
233   return TRUE;
234 }
235
236 #elif defined (__GNUC__)
237
238 __attribute__ ((constructor)) static void
239 glib_init_ctor (void)
240 {
241   glib_init ();
242 }
243
244 #else
245 # error Your platform/compiler is missing constructor support
246 #endif