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