glib-init: restore default for G_MESSAGES_PREFIXED
[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 #include "gconstructor.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <ctype.h>
33
34 /**
35  * g_mem_gc_friendly:
36  *
37  * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
38  * includes the key <literal>gc-friendly</literal>.
39  */
40 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
41 gboolean g_mem_gc_friendly = TRUE;
42 #else
43 gboolean g_mem_gc_friendly = FALSE;
44 #endif
45 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
46                                   G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
47 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
48
49 static gboolean
50 debug_key_matches (const gchar *key,
51                    const gchar *token,
52                    guint        length)
53 {
54   /* may not call GLib functions: see note in g_parse_debug_string() */
55   for (; length; length--, key++, token++)
56     {
57       char k = (*key   == '_') ? '-' : tolower (*key  );
58       char t = (*token == '_') ? '-' : tolower (*token);
59
60       if (k != t)
61         return FALSE;
62     }
63
64   return *key == '\0';
65 }
66
67 /**
68  * g_parse_debug_string:
69  * @string: (allow-none): a list of debug options separated by colons, spaces, or
70  * commas, or %NULL.
71  * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
72  *     strings with bit flags.
73  * @nkeys: the number of #GDebugKey<!-- -->s in the array.
74  *
75  * Parses a string containing debugging options
76  * into a %guint containing bit flags. This is used
77  * within GDK and GTK+ to parse the debug options passed on the
78  * command line or through environment variables.
79  *
80  * If @string is equal to <code>"all"</code>, all flags are set. Any flags
81  * specified along with <code>"all"</code> in @string are inverted; thus,
82  * <code>"all,foo,bar"</code> or <code>"foo,bar,all"</code> sets all flags
83  * except those corresponding to <code>"foo"</code> and <code>"bar"</code>.
84  *
85  * If @string is equal to <code>"help"</code>, all the available keys in @keys
86  * are printed out to standard error.
87  *
88  * Returns: the combined set of bit flags.
89  */
90 guint
91 g_parse_debug_string  (const gchar     *string,
92                        const GDebugKey *keys,
93                        guint            nkeys)
94 {
95   guint i;
96   guint result = 0;
97
98   if (string == NULL)
99     return 0;
100
101   /* this function is used during the initialisation of gmessages, gmem
102    * and gslice, so it may not do anything that causes memory to be
103    * allocated or risks messages being emitted.
104    *
105    * this means, more or less, that this code may not call anything
106    * inside GLib.
107    */
108
109   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, " all help\n");
116     }
117   else
118     {
119       const gchar *p = string;
120       const gchar *q;
121       gboolean invert = FALSE;
122
123       while (*p)
124        {
125          q = strpbrk (p, ":;, \t");
126          if (!q)
127            q = p + strlen (p);
128
129          if (debug_key_matches ("all", p, q - p))
130            {
131              invert = TRUE;
132            }
133          else
134            {
135              for (i = 0; i < nkeys; i++)
136                if (debug_key_matches (keys[i].key, p, q - p))
137                  result |= keys[i].value;
138            }
139
140          p = q;
141          if (*p)
142            p++;
143        }
144
145       if (invert)
146         {
147           guint all_flags = 0;
148
149           for (i = 0; i < nkeys; i++)
150             all_flags |= keys[i].value;
151
152           result = all_flags & (~result);
153         }
154     }
155
156   return result;
157 }
158
159 static guint
160 g_parse_debug_envvar (const gchar     *envvar,
161                       const GDebugKey *keys,
162                       gint             n_keys,
163                       guint            default_value)
164 {
165   const gchar *value;
166
167 #ifdef OS_WIN32
168   /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
169   gchar buffer[100];
170
171   if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
172     value = buffer;
173   else
174     return 0;
175 #else
176   value = getenv (envvar);
177 #endif
178
179   if (value == NULL)
180     return default_value;
181
182   return g_parse_debug_string (value, keys, n_keys);
183 }
184
185 static void
186 g_messages_prefixed_init (void)
187 {
188   const GDebugKey keys[] = {
189     { "error", G_LOG_LEVEL_ERROR },
190     { "critical", G_LOG_LEVEL_CRITICAL },
191     { "warning", G_LOG_LEVEL_WARNING },
192     { "message", G_LOG_LEVEL_MESSAGE },
193     { "info", G_LOG_LEVEL_INFO },
194     { "debug", G_LOG_LEVEL_DEBUG }
195   };
196
197   g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
198 }
199
200 static void
201 g_debug_init (void)
202 {
203   const GDebugKey keys[] = {
204     { "gc-friendly", 1 },
205     {"fatal-warnings",  G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
206     {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
207   };
208   GLogLevelFlags flags;
209
210   flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
211
212   g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
213
214   g_mem_gc_friendly = flags & 1;
215 }
216
217 static void
218 glib_init (void)
219 {
220   g_messages_prefixed_init ();
221   g_debug_init ();
222 }
223
224 #if defined (G_OS_WIN32)
225
226 HMODULE glib_dll;
227
228 BOOL WINAPI
229 DllMain (HINSTANCE hinstDLL,
230          DWORD     fdwReason,
231          LPVOID    lpvReserved)
232 {
233   switch (fdwReason)
234     {
235     case DLL_PROCESS_ATTACH:
236       glib_dll = hinstDLL;
237       g_clock_win32_init ();
238       g_thread_win32_init ();
239       glib_init ();
240       break;
241
242     case DLL_THREAD_DETACH:
243       g_thread_win32_thread_detach ();
244       break;
245
246     default:
247       /* do nothing */
248       ;
249     }
250
251   return TRUE;
252 }
253
254 #elif defined (G_HAS_CONSTRUCTORS)
255
256 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
257 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
258 #endif
259 G_DEFINE_CONSTRUCTOR(glib_init_ctor)
260
261 static void
262 glib_init_ctor (void)
263 {
264   glib_init ();
265 }
266
267 #else
268 # error Your platform/compiler is missing constructor support
269 #endif