documentation fixes
[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 <literal>gc-friendly</literal>.
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 <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>.
83  *
84  * If @string is equal to <code>"help"</code>, all the available keys in @keys
85  * are printed out to standard error.
86  *
87  * Returns: the combined set of bit flags.
88  */
89 guint
90 g_parse_debug_string  (const gchar     *string,
91                        const GDebugKey *keys,
92                        guint            nkeys)
93 {
94   guint i;
95   guint result = 0;
96
97   if (string == NULL)
98     return 0;
99
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.
103    *
104    * this means, more or less, that this code may not call anything
105    * inside GLib.
106    */
107
108   if (!strcasecmp (string, "help"))
109     {
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");
115     }
116   else
117     {
118       const gchar *p = string;
119       const gchar *q;
120       gboolean invert = FALSE;
121
122       while (*p)
123        {
124          q = strpbrk (p, ":;, \t");
125          if (!q)
126            q = p + strlen (p);
127
128          if (debug_key_matches ("all", p, q - p))
129            {
130              invert = TRUE;
131            }
132          else
133            {
134              for (i = 0; i < nkeys; i++)
135                if (debug_key_matches (keys[i].key, p, q - p))
136                  result |= keys[i].value;
137            }
138
139          p = q;
140          if (*p)
141            p++;
142        }
143
144       if (invert)
145         {
146           guint all_flags = 0;
147
148           for (i = 0; i < nkeys; i++)
149             all_flags |= keys[i].value;
150
151           result = all_flags & (~result);
152         }
153     }
154
155   return result;
156 }
157
158 static guint
159 g_parse_debug_envvar (const gchar     *envvar,
160                       const GDebugKey *keys,
161                       gint             n_keys)
162 {
163   const gchar *value;
164
165 #ifdef OS_WIN32
166   /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
167   gchar buffer[80];
168
169   if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
170     value = buffer;
171   else
172     return 0;
173 #else
174   value = getenv (envvar);
175 #endif
176
177   return g_parse_debug_string (value, keys, n_keys);
178 }
179
180 static void
181 g_messages_prefixed_init (void)
182 {
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 }
190   };
191
192   g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys));
193 }
194
195 static void
196 g_debug_init (void)
197 {
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 }
202   };
203   GLogLevelFlags flags;
204
205   flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
206
207   g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
208
209   g_mem_gc_friendly = flags & 1;
210 }
211
212 static void
213 glib_init (void)
214 {
215   g_messages_prefixed_init ();
216   g_debug_init ();
217 }
218
219 #if defined (G_OS_WIN32)
220
221 HMODULE glib_dll;
222
223 BOOL WINAPI
224 DllMain (HINSTANCE hinstDLL,
225          DWORD     fdwReason,
226          LPVOID    lpvReserved)
227 {
228   switch (fdwReason)
229     {
230     case DLL_PROCESS_ATTACH:
231       glib_dll = hinstDLL;
232       g_clock_win32_init ();
233       g_thread_win32_init ();
234       glib_init ();
235       break;
236
237     case DLL_THREAD_DETACH:
238       g_thread_win32_thread_detach ();
239       break;
240
241     default:
242       /* do nothing */
243       ;
244     }
245
246   return TRUE;
247 }
248
249 #elif defined (__GNUC__)
250
251 __attribute__ ((constructor)) static void
252 glib_init_ctor (void)
253 {
254   glib_init ();
255 }
256
257 #else
258 # error Your platform/compiler is missing constructor support
259 #endif