nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GLIB - Library of useful routines for C programming |
2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
||
3 | * |
||
4 | * This library is free software; you can redistribute it and/or |
||
5 | * modify it under the terms of the GNU Lesser General Public |
||
6 | * License as published by the Free Software Foundation; either |
||
7 | * version 2 of the License, or (at your option) any later version. |
||
8 | * |
||
9 | * This library is distributed in the hope that it will be useful, |
||
10 | * but 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 | |||
18 | /* |
||
19 | * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
||
20 | * file for a list of people on the GLib Team. See the ChangeLog |
||
21 | * files for a list of changes. These files are distributed with |
||
22 | * GLib at ftp://ftp.gtk.org/pub/gtk/. |
||
23 | */ |
||
24 | |||
25 | /* |
||
26 | * MT safe |
||
27 | */ |
||
28 | |||
29 | /** |
||
30 | * SECTION:warnings |
||
31 | * @Title: Message Output and Debugging Functions |
||
32 | * @Short_description: functions to output messages and help debug applications |
||
33 | * |
||
34 | * These functions provide support for outputting messages. |
||
35 | * |
||
36 | * The g_return family of macros (g_return_if_fail(), |
||
37 | * g_return_val_if_fail(), g_return_if_reached(), |
||
38 | * g_return_val_if_reached()) should only be used for programming |
||
39 | * errors, a typical use case is checking for invalid parameters at |
||
40 | * the beginning of a public function. They should not be used if |
||
41 | * you just mean "if (error) return", they should only be used if |
||
42 | * you mean "if (bug in program) return". The program behavior is |
||
43 | * generally considered undefined after one of these checks fails. |
||
44 | * They are not intended for normal control flow, only to give a |
||
45 | * perhaps-helpful warning before giving up. |
||
46 | */ |
||
47 | |||
48 | #include "config.h" |
||
49 | |||
50 | #include <stdlib.h> |
||
51 | #include <stdarg.h> |
||
52 | #include <stdio.h> |
||
53 | #include <string.h> |
||
54 | #include <signal.h> |
||
55 | #include <locale.h> |
||
56 | #include <errno.h> |
||
57 | |||
58 | #include "glib-init.h" |
||
59 | #include "gbacktrace.h" |
||
60 | #include "gcharset.h" |
||
61 | #include "gconvert.h" |
||
62 | #include "genviron.h" |
||
63 | #include "gmem.h" |
||
64 | #include "gprintfint.h" |
||
65 | #include "gtestutils.h" |
||
66 | #include "gthread.h" |
||
67 | #include "gstrfuncs.h" |
||
68 | #include "gstring.h" |
||
69 | #include "gpattern.h" |
||
70 | |||
71 | #ifdef G_OS_UNIX |
||
72 | #include <unistd.h> |
||
73 | #endif |
||
74 | |||
75 | #ifdef G_OS_WIN32 |
||
76 | #include <process.h> /* For getpid() */ |
||
77 | #include <io.h> |
||
78 | # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */ |
||
79 | # include <windows.h> |
||
80 | #endif |
||
81 | |||
82 | |||
83 | /** |
||
84 | * SECTION:messages |
||
85 | * @title: Message Logging |
||
86 | * @short_description: versatile support for logging messages |
||
87 | * with different levels of importance |
||
88 | * |
||
89 | * These functions provide support for logging error messages |
||
90 | * or messages used for debugging. |
||
91 | * |
||
92 | * There are several built-in levels of messages, defined in |
||
93 | * #GLogLevelFlags. These can be extended with user-defined levels. |
||
94 | */ |
||
95 | |||
96 | /** |
||
97 | * G_LOG_DOMAIN: |
||
98 | * |
||
99 | * Defines the log domain. |
||
100 | * |
||
101 | * For applications, this is typically left as the default %NULL |
||
102 | * (or "") domain. Libraries should define this so that any messages |
||
103 | * which they log can be differentiated from messages from other |
||
104 | * libraries and application code. But be careful not to define |
||
105 | * it in any public header files. |
||
106 | * |
||
107 | * For example, GTK+ uses this in its Makefile.am: |
||
108 | * |[ |
||
109 | * AM_CPPFLAGS = -DG_LOG_DOMAIN=\"Gtk\" |
||
110 | * ]| |
||
111 | */ |
||
112 | |||
113 | /** |
||
114 | * G_LOG_FATAL_MASK: |
||
115 | * |
||
116 | * GLib log levels that are considered fatal by default. |
||
117 | */ |
||
118 | |||
119 | /** |
||
120 | * GLogFunc: |
||
121 | * @log_domain: the log domain of the message |
||
122 | * @log_level: the log level of the message (including the |
||
123 | * fatal and recursion flags) |
||
124 | * @message: the message to process |
||
125 | * @user_data: user data, set in g_log_set_handler() |
||
126 | * |
||
127 | * Specifies the prototype of log handler functions. |
||
128 | * |
||
129 | * The default log handler, g_log_default_handler(), automatically appends a |
||
130 | * new-line character to @message when printing it. It is advised that any |
||
131 | * custom log handler functions behave similarly, so that logging calls in user |
||
132 | * code do not need modifying to add a new-line character to the message if the |
||
133 | * log handler is changed. |
||
134 | */ |
||
135 | |||
136 | /** |
||
137 | * GLogLevelFlags: |
||
138 | * @G_LOG_FLAG_RECURSION: internal flag |
||
139 | * @G_LOG_FLAG_FATAL: internal flag |
||
140 | * @G_LOG_LEVEL_ERROR: log level for errors, see g_error(). |
||
141 | * This level is also used for messages produced by g_assert(). |
||
142 | * @G_LOG_LEVEL_CRITICAL: log level for critical warning messages, see |
||
143 | * g_critical(). |
||
144 | * This level is also used for messages produced by g_return_if_fail() |
||
145 | * and g_return_val_if_fail(). |
||
146 | * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning() |
||
147 | * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message() |
||
148 | * @G_LOG_LEVEL_INFO: log level for informational messages, see g_info() |
||
149 | * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug() |
||
150 | * @G_LOG_LEVEL_MASK: a mask including all log levels |
||
151 | * |
||
152 | * Flags specifying the level of log messages. |
||
153 | * |
||
154 | * It is possible to change how GLib treats messages of the various |
||
155 | * levels using g_log_set_handler() and g_log_set_fatal_mask(). |
||
156 | */ |
||
157 | |||
158 | /** |
||
159 | * G_LOG_LEVEL_USER_SHIFT: |
||
160 | * |
||
161 | * Log levels below 1<<G_LOG_LEVEL_USER_SHIFT are used by GLib. |
||
162 | * Higher bits can be used for user-defined log levels. |
||
163 | */ |
||
164 | |||
165 | /** |
||
166 | * g_message: |
||
167 | * @...: format string, followed by parameters to insert |
||
168 | * into the format string (as with printf()) |
||
169 | * |
||
170 | * A convenience function/macro to log a normal message. |
||
171 | * |
||
172 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
173 | * character will automatically be appended to @..., and need not be entered |
||
174 | * manually. |
||
175 | */ |
||
176 | |||
177 | /** |
||
178 | * g_warning: |
||
179 | * @...: format string, followed by parameters to insert |
||
180 | * into the format string (as with printf()) |
||
181 | * |
||
182 | * A convenience function/macro to log a warning message. |
||
183 | * |
||
184 | * This is not intended for end user error reporting. Use of #GError is |
||
185 | * preferred for that instead, as it allows calling functions to perform actions |
||
186 | * conditional on the type of error. |
||
187 | * |
||
188 | * You can make warnings fatal at runtime by setting the `G_DEBUG` |
||
189 | * environment variable (see |
||
190 | * [Running GLib Applications](glib-running.html)). |
||
191 | * |
||
192 | * If g_log_default_handler() is used as the log handler function, |
||
193 | * a newline character will automatically be appended to @..., and |
||
194 | * need not be entered manually. |
||
195 | */ |
||
196 | |||
197 | /** |
||
198 | * g_critical: |
||
199 | * @...: format string, followed by parameters to insert |
||
200 | * into the format string (as with printf()) |
||
201 | * |
||
202 | * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL). |
||
203 | * It's more or less application-defined what constitutes |
||
204 | * a critical vs. a regular warning. You could call |
||
205 | * g_log_set_always_fatal() to make critical warnings exit |
||
206 | * the program, then use g_critical() for fatal errors, for |
||
207 | * example. |
||
208 | * |
||
209 | * You can also make critical warnings fatal at runtime by |
||
210 | * setting the `G_DEBUG` environment variable (see |
||
211 | * [Running GLib Applications](glib-running.html)). |
||
212 | * |
||
213 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
214 | * character will automatically be appended to @..., and need not be entered |
||
215 | * manually. |
||
216 | */ |
||
217 | |||
218 | /** |
||
219 | * g_error: |
||
220 | * @...: format string, followed by parameters to insert |
||
221 | * into the format string (as with printf()) |
||
222 | * |
||
223 | * A convenience function/macro to log an error message. |
||
224 | * |
||
225 | * This is not intended for end user error reporting. Use of #GError is |
||
226 | * preferred for that instead, as it allows calling functions to perform actions |
||
227 | * conditional on the type of error. |
||
228 | * |
||
229 | * Error messages are always fatal, resulting in a call to |
||
230 | * abort() to terminate the application. This function will |
||
231 | * result in a core dump; don't use it for errors you expect. |
||
232 | * Using this function indicates a bug in your program, i.e. |
||
233 | * an assertion failure. |
||
234 | * |
||
235 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
236 | * character will automatically be appended to @..., and need not be entered |
||
237 | * manually. |
||
238 | * |
||
239 | */ |
||
240 | |||
241 | /** |
||
242 | * g_info: |
||
243 | * @...: format string, followed by parameters to insert |
||
244 | * into the format string (as with printf()) |
||
245 | * |
||
246 | * A convenience function/macro to log an informational message. Seldom used. |
||
247 | * |
||
248 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
249 | * character will automatically be appended to @..., and need not be entered |
||
250 | * manually. |
||
251 | * |
||
252 | * Such messages are suppressed by the g_log_default_handler() unless |
||
253 | * the G_MESSAGES_DEBUG environment variable is set appropriately. |
||
254 | * |
||
255 | * Since: 2.40 |
||
256 | */ |
||
257 | |||
258 | /** |
||
259 | * g_debug: |
||
260 | * @...: format string, followed by parameters to insert |
||
261 | * into the format string (as with printf()) |
||
262 | * |
||
263 | * A convenience function/macro to log a debug message. |
||
264 | * |
||
265 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
266 | * character will automatically be appended to @..., and need not be entered |
||
267 | * manually. |
||
268 | * |
||
269 | * Such messages are suppressed by the g_log_default_handler() unless |
||
270 | * the G_MESSAGES_DEBUG environment variable is set appropriately. |
||
271 | * |
||
272 | * Since: 2.6 |
||
273 | */ |
||
274 | |||
275 | /* --- structures --- */ |
||
276 | typedef struct _GLogDomain GLogDomain; |
||
277 | typedef struct _GLogHandler GLogHandler; |
||
278 | struct _GLogDomain |
||
279 | { |
||
280 | gchar *log_domain; |
||
281 | GLogLevelFlags fatal_mask; |
||
282 | GLogHandler *handlers; |
||
283 | GLogDomain *next; |
||
284 | }; |
||
285 | struct _GLogHandler |
||
286 | { |
||
287 | guint id; |
||
288 | GLogLevelFlags log_level; |
||
289 | GLogFunc log_func; |
||
290 | gpointer data; |
||
291 | GDestroyNotify destroy; |
||
292 | GLogHandler *next; |
||
293 | }; |
||
294 | |||
295 | |||
296 | /* --- variables --- */ |
||
297 | static GMutex g_messages_lock; |
||
298 | static GLogDomain *g_log_domains = NULL; |
||
299 | static GPrintFunc glib_print_func = NULL; |
||
300 | static GPrintFunc glib_printerr_func = NULL; |
||
301 | static GPrivate g_log_depth; |
||
302 | static GLogFunc default_log_func = g_log_default_handler; |
||
303 | static gpointer default_log_data = NULL; |
||
304 | static GTestLogFatalFunc fatal_log_func = NULL; |
||
305 | static gpointer fatal_log_data; |
||
306 | |||
307 | /* --- functions --- */ |
||
308 | |||
309 | static void _g_log_abort (gboolean breakpoint); |
||
310 | |||
311 | static void |
||
312 | _g_log_abort (gboolean breakpoint) |
||
313 | { |
||
314 | if (g_test_subprocess ()) |
||
315 | { |
||
316 | /* If this is a test case subprocess then it probably caused |
||
317 | * this error message on purpose, so just exit() rather than |
||
318 | * abort()ing, to avoid triggering any system crash-reporting |
||
319 | * daemon. |
||
320 | */ |
||
321 | _exit (1); |
||
322 | } |
||
323 | |||
324 | if (breakpoint) |
||
325 | G_BREAKPOINT (); |
||
326 | else |
||
327 | abort (); |
||
328 | } |
||
329 | |||
330 | #ifdef G_OS_WIN32 |
||
331 | # include <windows.h> |
||
332 | static gboolean win32_keep_fatal_message = FALSE; |
||
333 | |||
334 | /* This default message will usually be overwritten. */ |
||
335 | /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never |
||
336 | * called with huge strings, is it? |
||
337 | */ |
||
338 | static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting."; |
||
339 | static gchar *fatal_msg_ptr = fatal_msg_buf; |
||
340 | |||
341 | #undef write |
||
342 | static inline int |
||
343 | dowrite (int fd, |
||
344 | const void *buf, |
||
345 | unsigned int len) |
||
346 | { |
||
347 | if (win32_keep_fatal_message) |
||
348 | { |
||
349 | memcpy (fatal_msg_ptr, buf, len); |
||
350 | fatal_msg_ptr += len; |
||
351 | *fatal_msg_ptr = 0; |
||
352 | return len; |
||
353 | } |
||
354 | |||
355 | write (fd, buf, len); |
||
356 | |||
357 | return len; |
||
358 | } |
||
359 | #define write(fd, buf, len) dowrite(fd, buf, len) |
||
360 | |||
361 | #endif |
||
362 | |||
363 | static void |
||
364 | write_string (FILE *stream, |
||
365 | const gchar *string) |
||
366 | { |
||
367 | fputs (string, stream); |
||
368 | } |
||
369 | |||
370 | static GLogDomain* |
||
371 | g_log_find_domain_L (const gchar *log_domain) |
||
372 | { |
||
373 | GLogDomain *domain; |
||
374 | |||
375 | domain = g_log_domains; |
||
376 | while (domain) |
||
377 | { |
||
378 | if (strcmp (domain->log_domain, log_domain) == 0) |
||
379 | return domain; |
||
380 | domain = domain->next; |
||
381 | } |
||
382 | return NULL; |
||
383 | } |
||
384 | |||
385 | static GLogDomain* |
||
386 | g_log_domain_new_L (const gchar *log_domain) |
||
387 | { |
||
388 | GLogDomain *domain; |
||
389 | |||
390 | domain = g_new (GLogDomain, 1); |
||
391 | domain->log_domain = g_strdup (log_domain); |
||
392 | domain->fatal_mask = G_LOG_FATAL_MASK; |
||
393 | domain->handlers = NULL; |
||
394 | |||
395 | domain->next = g_log_domains; |
||
396 | g_log_domains = domain; |
||
397 | |||
398 | return domain; |
||
399 | } |
||
400 | |||
401 | static void |
||
402 | g_log_domain_check_free_L (GLogDomain *domain) |
||
403 | { |
||
404 | if (domain->fatal_mask == G_LOG_FATAL_MASK && |
||
405 | domain->handlers == NULL) |
||
406 | { |
||
407 | GLogDomain *last, *work; |
||
408 | |||
409 | last = NULL; |
||
410 | |||
411 | work = g_log_domains; |
||
412 | while (work) |
||
413 | { |
||
414 | if (work == domain) |
||
415 | { |
||
416 | if (last) |
||
417 | last->next = domain->next; |
||
418 | else |
||
419 | g_log_domains = domain->next; |
||
420 | g_free (domain->log_domain); |
||
421 | g_free (domain); |
||
422 | break; |
||
423 | } |
||
424 | last = work; |
||
425 | work = last->next; |
||
426 | } |
||
427 | } |
||
428 | } |
||
429 | |||
430 | static GLogFunc |
||
431 | g_log_domain_get_handler_L (GLogDomain *domain, |
||
432 | GLogLevelFlags log_level, |
||
433 | gpointer *data) |
||
434 | { |
||
435 | if (domain && log_level) |
||
436 | { |
||
437 | GLogHandler *handler; |
||
438 | |||
439 | handler = domain->handlers; |
||
440 | while (handler) |
||
441 | { |
||
442 | if ((handler->log_level & log_level) == log_level) |
||
443 | { |
||
444 | *data = handler->data; |
||
445 | return handler->log_func; |
||
446 | } |
||
447 | handler = handler->next; |
||
448 | } |
||
449 | } |
||
450 | |||
451 | *data = default_log_data; |
||
452 | return default_log_func; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * g_log_set_always_fatal: |
||
457 | * @fatal_mask: the mask containing bits set for each level |
||
458 | * of error which is to be fatal |
||
459 | * |
||
460 | * Sets the message levels which are always fatal, in any log domain. |
||
461 | * When a message with any of these levels is logged the program terminates. |
||
462 | * You can only set the levels defined by GLib to be fatal. |
||
463 | * %G_LOG_LEVEL_ERROR is always fatal. |
||
464 | * |
||
465 | * You can also make some message levels fatal at runtime by setting |
||
466 | * the `G_DEBUG` environment variable (see |
||
467 | * [Running GLib Applications](glib-running.html)). |
||
468 | * |
||
469 | * Returns: the old fatal mask |
||
470 | */ |
||
471 | GLogLevelFlags |
||
472 | g_log_set_always_fatal (GLogLevelFlags fatal_mask) |
||
473 | { |
||
474 | GLogLevelFlags old_mask; |
||
475 | |||
476 | /* restrict the global mask to levels that are known to glib |
||
477 | * since this setting applies to all domains |
||
478 | */ |
||
479 | fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1; |
||
480 | /* force errors to be fatal */ |
||
481 | fatal_mask |= G_LOG_LEVEL_ERROR; |
||
482 | /* remove bogus flag */ |
||
483 | fatal_mask &= ~G_LOG_FLAG_FATAL; |
||
484 | |||
485 | g_mutex_lock (&g_messages_lock); |
||
486 | old_mask = g_log_always_fatal; |
||
487 | g_log_always_fatal = fatal_mask; |
||
488 | g_mutex_unlock (&g_messages_lock); |
||
489 | |||
490 | return old_mask; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * g_log_set_fatal_mask: |
||
495 | * @log_domain: the log domain |
||
496 | * @fatal_mask: the new fatal mask |
||
497 | * |
||
498 | * Sets the log levels which are fatal in the given domain. |
||
499 | * %G_LOG_LEVEL_ERROR is always fatal. |
||
500 | * |
||
501 | * Returns: the old fatal mask for the log domain |
||
502 | */ |
||
503 | GLogLevelFlags |
||
504 | g_log_set_fatal_mask (const gchar *log_domain, |
||
505 | GLogLevelFlags fatal_mask) |
||
506 | { |
||
507 | GLogLevelFlags old_flags; |
||
508 | GLogDomain *domain; |
||
509 | |||
510 | if (!log_domain) |
||
511 | log_domain = ""; |
||
512 | |||
513 | /* force errors to be fatal */ |
||
514 | fatal_mask |= G_LOG_LEVEL_ERROR; |
||
515 | /* remove bogus flag */ |
||
516 | fatal_mask &= ~G_LOG_FLAG_FATAL; |
||
517 | |||
518 | g_mutex_lock (&g_messages_lock); |
||
519 | |||
520 | domain = g_log_find_domain_L (log_domain); |
||
521 | if (!domain) |
||
522 | domain = g_log_domain_new_L (log_domain); |
||
523 | old_flags = domain->fatal_mask; |
||
524 | |||
525 | domain->fatal_mask = fatal_mask; |
||
526 | g_log_domain_check_free_L (domain); |
||
527 | |||
528 | g_mutex_unlock (&g_messages_lock); |
||
529 | |||
530 | return old_flags; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * g_log_set_handler: |
||
535 | * @log_domain: (allow-none): the log domain, or %NULL for the default "" |
||
536 | * application domain |
||
537 | * @log_levels: the log levels to apply the log handler for. |
||
538 | * To handle fatal and recursive messages as well, combine |
||
539 | * the log levels with the #G_LOG_FLAG_FATAL and |
||
540 | * #G_LOG_FLAG_RECURSION bit flags. |
||
541 | * @log_func: the log handler function |
||
542 | * @user_data: data passed to the log handler |
||
543 | * |
||
544 | * Sets the log handler for a domain and a set of log levels. |
||
545 | * To handle fatal and recursive messages the @log_levels parameter |
||
546 | * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION |
||
547 | * bit flags. |
||
548 | * |
||
549 | * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if |
||
550 | * you want to set a handler for this log level you must combine it with |
||
551 | * #G_LOG_FLAG_FATAL. |
||
552 | * |
||
553 | * Here is an example for adding a log handler for all warning messages |
||
554 | * in the default domain: |
||
555 | * |[<!-- language="C" --> |
||
556 | * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL |
||
557 | * | G_LOG_FLAG_RECURSION, my_log_handler, NULL); |
||
558 | * ]| |
||
559 | * |
||
560 | * This example adds a log handler for all critical messages from GTK+: |
||
561 | * |[<!-- language="C" --> |
||
562 | * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL |
||
563 | * | G_LOG_FLAG_RECURSION, my_log_handler, NULL); |
||
564 | * ]| |
||
565 | * |
||
566 | * This example adds a log handler for all messages from GLib: |
||
567 | * |[<!-- language="C" --> |
||
568 | * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
||
569 | * | G_LOG_FLAG_RECURSION, my_log_handler, NULL); |
||
570 | * ]| |
||
571 | * |
||
572 | * Returns: the id of the new handler |
||
573 | */ |
||
574 | guint |
||
575 | g_log_set_handler (const gchar *log_domain, |
||
576 | GLogLevelFlags log_levels, |
||
577 | GLogFunc log_func, |
||
578 | gpointer user_data) |
||
579 | { |
||
580 | return g_log_set_handler_full (log_domain, log_levels, log_func, user_data, NULL); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * g_log_set_handler_full: (rename-to g_log_set_handler) |
||
585 | * @log_domain: (allow-none): the log domain, or %NULL for the default "" |
||
586 | * application domain |
||
587 | * @log_levels: the log levels to apply the log handler for. |
||
588 | * To handle fatal and recursive messages as well, combine |
||
589 | * the log levels with the #G_LOG_FLAG_FATAL and |
||
590 | * #G_LOG_FLAG_RECURSION bit flags. |
||
591 | * @log_func: the log handler function |
||
592 | * @user_data: data passed to the log handler |
||
593 | * @destroy: destroy notify for @user_data, or %NULL |
||
594 | * |
||
595 | * Like g_log_sets_handler(), but takes a destroy notify for the @user_data. |
||
596 | * |
||
597 | * Returns: the id of the new handler |
||
598 | * |
||
599 | * Since: 2.46 |
||
600 | */ |
||
601 | guint |
||
602 | g_log_set_handler_full (const gchar *log_domain, |
||
603 | GLogLevelFlags log_levels, |
||
604 | GLogFunc log_func, |
||
605 | gpointer user_data, |
||
606 | GDestroyNotify destroy) |
||
607 | { |
||
608 | static guint handler_id = 0; |
||
609 | GLogDomain *domain; |
||
610 | GLogHandler *handler; |
||
611 | |||
612 | g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0); |
||
613 | g_return_val_if_fail (log_func != NULL, 0); |
||
614 | |||
615 | if (!log_domain) |
||
616 | log_domain = ""; |
||
617 | |||
618 | handler = g_new (GLogHandler, 1); |
||
619 | |||
620 | g_mutex_lock (&g_messages_lock); |
||
621 | |||
622 | domain = g_log_find_domain_L (log_domain); |
||
623 | if (!domain) |
||
624 | domain = g_log_domain_new_L (log_domain); |
||
625 | |||
626 | handler->id = ++handler_id; |
||
627 | handler->log_level = log_levels; |
||
628 | handler->log_func = log_func; |
||
629 | handler->data = user_data; |
||
630 | handler->destroy = destroy; |
||
631 | handler->next = domain->handlers; |
||
632 | domain->handlers = handler; |
||
633 | |||
634 | g_mutex_unlock (&g_messages_lock); |
||
635 | |||
636 | return handler_id; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * g_log_set_default_handler: |
||
641 | * @log_func: the log handler function |
||
642 | * @user_data: data passed to the log handler |
||
643 | * |
||
644 | * Installs a default log handler which is used if no |
||
645 | * log handler has been set for the particular log domain |
||
646 | * and log level combination. By default, GLib uses |
||
647 | * g_log_default_handler() as default log handler. |
||
648 | * |
||
649 | * Returns: the previous default log handler |
||
650 | * |
||
651 | * Since: 2.6 |
||
652 | */ |
||
653 | GLogFunc |
||
654 | g_log_set_default_handler (GLogFunc log_func, |
||
655 | gpointer user_data) |
||
656 | { |
||
657 | GLogFunc old_log_func; |
||
658 | |||
659 | g_mutex_lock (&g_messages_lock); |
||
660 | old_log_func = default_log_func; |
||
661 | default_log_func = log_func; |
||
662 | default_log_data = user_data; |
||
663 | g_mutex_unlock (&g_messages_lock); |
||
664 | |||
665 | return old_log_func; |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * g_test_log_set_fatal_handler: |
||
670 | * @log_func: the log handler function. |
||
671 | * @user_data: data passed to the log handler. |
||
672 | * |
||
673 | * Installs a non-error fatal log handler which can be |
||
674 | * used to decide whether log messages which are counted |
||
675 | * as fatal abort the program. |
||
676 | * |
||
677 | * The use case here is that you are running a test case |
||
678 | * that depends on particular libraries or circumstances |
||
679 | * and cannot prevent certain known critical or warning |
||
680 | * messages. So you install a handler that compares the |
||
681 | * domain and message to precisely not abort in such a case. |
||
682 | * |
||
683 | * Note that the handler is reset at the beginning of |
||
684 | * any test case, so you have to set it inside each test |
||
685 | * function which needs the special behavior. |
||
686 | * |
||
687 | * This handler has no effect on g_error messages. |
||
688 | * |
||
689 | * Since: 2.22 |
||
690 | **/ |
||
691 | void |
||
692 | g_test_log_set_fatal_handler (GTestLogFatalFunc log_func, |
||
693 | gpointer user_data) |
||
694 | { |
||
695 | g_mutex_lock (&g_messages_lock); |
||
696 | fatal_log_func = log_func; |
||
697 | fatal_log_data = user_data; |
||
698 | g_mutex_unlock (&g_messages_lock); |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * g_log_remove_handler: |
||
703 | * @log_domain: the log domain |
||
704 | * @handler_id: the id of the handler, which was returned |
||
705 | * in g_log_set_handler() |
||
706 | * |
||
707 | * Removes the log handler. |
||
708 | */ |
||
709 | void |
||
710 | g_log_remove_handler (const gchar *log_domain, |
||
711 | guint handler_id) |
||
712 | { |
||
713 | GLogDomain *domain; |
||
714 | |||
715 | g_return_if_fail (handler_id > 0); |
||
716 | |||
717 | if (!log_domain) |
||
718 | log_domain = ""; |
||
719 | |||
720 | g_mutex_lock (&g_messages_lock); |
||
721 | domain = g_log_find_domain_L (log_domain); |
||
722 | if (domain) |
||
723 | { |
||
724 | GLogHandler *work, *last; |
||
725 | |||
726 | last = NULL; |
||
727 | work = domain->handlers; |
||
728 | while (work) |
||
729 | { |
||
730 | if (work->id == handler_id) |
||
731 | { |
||
732 | if (last) |
||
733 | last->next = work->next; |
||
734 | else |
||
735 | domain->handlers = work->next; |
||
736 | g_log_domain_check_free_L (domain); |
||
737 | g_mutex_unlock (&g_messages_lock); |
||
738 | if (work->destroy) |
||
739 | work->destroy (work->data); |
||
740 | g_free (work); |
||
741 | return; |
||
742 | } |
||
743 | last = work; |
||
744 | work = last->next; |
||
745 | } |
||
746 | } |
||
747 | g_mutex_unlock (&g_messages_lock); |
||
748 | g_warning ("%s: could not find handler with id '%d' for domain \"%s\"", |
||
749 | G_STRLOC, handler_id, log_domain); |
||
750 | } |
||
751 | |||
752 | #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \ |
||
753 | (wc == 0x7f) || \ |
||
754 | (wc >= 0x80 && wc < 0xa0))) |
||
755 | |||
756 | static gchar* |
||
757 | strdup_convert (const gchar *string, |
||
758 | const gchar *charset) |
||
759 | { |
||
760 | if (!g_utf8_validate (string, -1, NULL)) |
||
761 | { |
||
762 | GString *gstring = g_string_new ("[Invalid UTF-8] "); |
||
763 | guchar *p; |
||
764 | |||
765 | for (p = (guchar *)string; *p; p++) |
||
766 | { |
||
767 | if (CHAR_IS_SAFE(*p) && |
||
768 | !(*p == '\r' && *(p + 1) != '\n') && |
||
769 | *p < 0x80) |
||
770 | g_string_append_c (gstring, *p); |
||
771 | else |
||
772 | g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p); |
||
773 | } |
||
774 | |||
775 | return g_string_free (gstring, FALSE); |
||
776 | } |
||
777 | else |
||
778 | { |
||
779 | GError *err = NULL; |
||
780 | |||
781 | gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err); |
||
782 | if (result) |
||
783 | return result; |
||
784 | else |
||
785 | { |
||
786 | /* Not thread-safe, but doesn't matter if we print the warning twice |
||
787 | */ |
||
788 | static gboolean warned = FALSE; |
||
789 | if (!warned) |
||
790 | { |
||
791 | warned = TRUE; |
||
792 | _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message); |
||
793 | } |
||
794 | g_error_free (err); |
||
795 | |||
796 | return g_strdup (string); |
||
797 | } |
||
798 | } |
||
799 | } |
||
800 | |||
801 | /* For a radix of 8 we need at most 3 output bytes for 1 input |
||
802 | * byte. Additionally we might need up to 2 output bytes for the |
||
803 | * readix prefix and 1 byte for the trailing NULL. |
||
804 | */ |
||
805 | #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3) |
||
806 | |||
807 | static void |
||
808 | format_unsigned (gchar *buf, |
||
809 | gulong num, |
||
810 | guint radix) |
||
811 | { |
||
812 | gulong tmp; |
||
813 | gchar c; |
||
814 | gint i, n; |
||
815 | |||
816 | /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */ |
||
817 | |||
818 | if (radix != 8 && radix != 10 && radix != 16) |
||
819 | { |
||
820 | *buf = '\000'; |
||
821 | return; |
||
822 | } |
||
823 | |||
824 | if (!num) |
||
825 | { |
||
826 | *buf++ = '0'; |
||
827 | *buf = '\000'; |
||
828 | return; |
||
829 | } |
||
830 | |||
831 | if (radix == 16) |
||
832 | { |
||
833 | *buf++ = '0'; |
||
834 | *buf++ = 'x'; |
||
835 | } |
||
836 | else if (radix == 8) |
||
837 | { |
||
838 | *buf++ = '0'; |
||
839 | } |
||
840 | |||
841 | n = 0; |
||
842 | tmp = num; |
||
843 | while (tmp) |
||
844 | { |
||
845 | tmp /= radix; |
||
846 | n++; |
||
847 | } |
||
848 | |||
849 | i = n; |
||
850 | |||
851 | /* Again we can't use g_assert; actually this check should _never_ fail. */ |
||
852 | if (n > FORMAT_UNSIGNED_BUFSIZE - 3) |
||
853 | { |
||
854 | *buf = '\000'; |
||
855 | return; |
||
856 | } |
||
857 | |||
858 | while (num) |
||
859 | { |
||
860 | i--; |
||
861 | c = (num % radix); |
||
862 | if (c < 10) |
||
863 | buf[i] = c + '0'; |
||
864 | else |
||
865 | buf[i] = c + 'a' - 10; |
||
866 | num /= radix; |
||
867 | } |
||
868 | |||
869 | buf[n] = '\000'; |
||
870 | } |
||
871 | |||
872 | /* string size big enough to hold level prefix */ |
||
873 | #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32) |
||
874 | |||
875 | #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING) |
||
876 | |||
877 | /* these are emitted by the default log handler */ |
||
878 | #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE) |
||
879 | /* these are filtered by G_MESSAGES_DEBUG by the default log handler */ |
||
880 | #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG) |
||
881 | |||
882 | static FILE * |
||
883 | mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE], |
||
884 | GLogLevelFlags log_level) |
||
885 | { |
||
886 | gboolean to_stdout = TRUE; |
||
887 | |||
888 | /* we may not call _any_ GLib functions here */ |
||
889 | |||
890 | switch (log_level & G_LOG_LEVEL_MASK) |
||
891 | { |
||
892 | case G_LOG_LEVEL_ERROR: |
||
893 | strcpy (level_prefix, "ERROR"); |
||
894 | to_stdout = FALSE; |
||
895 | break; |
||
896 | case G_LOG_LEVEL_CRITICAL: |
||
897 | strcpy (level_prefix, "CRITICAL"); |
||
898 | to_stdout = FALSE; |
||
899 | break; |
||
900 | case G_LOG_LEVEL_WARNING: |
||
901 | strcpy (level_prefix, "WARNING"); |
||
902 | to_stdout = FALSE; |
||
903 | break; |
||
904 | case G_LOG_LEVEL_MESSAGE: |
||
905 | strcpy (level_prefix, "Message"); |
||
906 | to_stdout = FALSE; |
||
907 | break; |
||
908 | case G_LOG_LEVEL_INFO: |
||
909 | strcpy (level_prefix, "INFO"); |
||
910 | break; |
||
911 | case G_LOG_LEVEL_DEBUG: |
||
912 | strcpy (level_prefix, "DEBUG"); |
||
913 | break; |
||
914 | default: |
||
915 | if (log_level) |
||
916 | { |
||
917 | strcpy (level_prefix, "LOG-"); |
||
918 | format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16); |
||
919 | } |
||
920 | else |
||
921 | strcpy (level_prefix, "LOG"); |
||
922 | break; |
||
923 | } |
||
924 | if (log_level & G_LOG_FLAG_RECURSION) |
||
925 | strcat (level_prefix, " (recursed)"); |
||
926 | if (log_level & ALERT_LEVELS) |
||
927 | strcat (level_prefix, " **"); |
||
928 | |||
929 | #ifdef G_OS_WIN32 |
||
930 | if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ()) |
||
931 | win32_keep_fatal_message = TRUE; |
||
932 | #endif |
||
933 | return to_stdout ? stdout : stderr; |
||
934 | } |
||
935 | |||
936 | typedef struct { |
||
937 | gchar *log_domain; |
||
938 | GLogLevelFlags log_level; |
||
939 | gchar *pattern; |
||
940 | } GTestExpectedMessage; |
||
941 | |||
942 | static GSList *expected_messages = NULL; |
||
943 | |||
944 | /** |
||
945 | * g_logv: |
||
946 | * @log_domain: (nullable): the log domain, or %NULL for the default "" |
||
947 | * application domain |
||
948 | * @log_level: the log level |
||
949 | * @format: the message format. See the printf() documentation |
||
950 | * @args: the parameters to insert into the format string |
||
951 | * |
||
952 | * Logs an error or debugging message. |
||
953 | * |
||
954 | * If the log level has been set as fatal, the abort() |
||
955 | * function is called to terminate the program. |
||
956 | * |
||
957 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
958 | * character will automatically be appended to @..., and need not be entered |
||
959 | * manually. |
||
960 | */ |
||
961 | void |
||
962 | g_logv (const gchar *log_domain, |
||
963 | GLogLevelFlags log_level, |
||
964 | const gchar *format, |
||
965 | va_list args) |
||
966 | { |
||
967 | gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0; |
||
968 | gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0; |
||
969 | gchar buffer[1025], *msg, *msg_alloc = NULL; |
||
970 | gint i; |
||
971 | |||
972 | log_level &= G_LOG_LEVEL_MASK; |
||
973 | if (!log_level) |
||
974 | return; |
||
975 | |||
976 | if (log_level & G_LOG_FLAG_RECURSION) |
||
977 | { |
||
978 | /* we use a stack buffer of fixed size, since we're likely |
||
979 | * in an out-of-memory situation |
||
980 | */ |
||
981 | gsize size G_GNUC_UNUSED; |
||
982 | |||
983 | size = _g_vsnprintf (buffer, 1024, format, args); |
||
984 | msg = buffer; |
||
985 | } |
||
986 | else |
||
987 | msg = msg_alloc = g_strdup_vprintf (format, args); |
||
988 | |||
989 | if (expected_messages) |
||
990 | { |
||
991 | GTestExpectedMessage *expected = expected_messages->data; |
||
992 | |||
993 | if (g_strcmp0 (expected->log_domain, log_domain) == 0 && |
||
994 | ((log_level & expected->log_level) == expected->log_level) && |
||
995 | g_pattern_match_simple (expected->pattern, msg)) |
||
996 | { |
||
997 | expected_messages = g_slist_delete_link (expected_messages, |
||
998 | expected_messages); |
||
999 | g_free (expected->log_domain); |
||
1000 | g_free (expected->pattern); |
||
1001 | g_free (expected); |
||
1002 | g_free (msg_alloc); |
||
1003 | return; |
||
1004 | } |
||
1005 | else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG) |
||
1006 | { |
||
1007 | gchar level_prefix[STRING_BUFFER_SIZE]; |
||
1008 | gchar *expected_message; |
||
1009 | |||
1010 | mklevel_prefix (level_prefix, expected->log_level); |
||
1011 | expected_message = g_strdup_printf ("Did not see expected message %s-%s: %s", |
||
1012 | expected->log_domain ? expected->log_domain : "**", |
||
1013 | level_prefix, expected->pattern); |
||
1014 | g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, expected_message, NULL); |
||
1015 | g_free (expected_message); |
||
1016 | |||
1017 | log_level |= G_LOG_FLAG_FATAL; |
||
1018 | } |
||
1019 | } |
||
1020 | |||
1021 | for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i)) |
||
1022 | { |
||
1023 | GLogLevelFlags test_level; |
||
1024 | |||
1025 | test_level = 1 << i; |
||
1026 | if (log_level & test_level) |
||
1027 | { |
||
1028 | GLogDomain *domain; |
||
1029 | GLogFunc log_func; |
||
1030 | GLogLevelFlags domain_fatal_mask; |
||
1031 | gpointer data = NULL; |
||
1032 | gboolean masquerade_fatal = FALSE; |
||
1033 | guint depth; |
||
1034 | |||
1035 | if (was_fatal) |
||
1036 | test_level |= G_LOG_FLAG_FATAL; |
||
1037 | if (was_recursion) |
||
1038 | test_level |= G_LOG_FLAG_RECURSION; |
||
1039 | |||
1040 | /* check recursion and lookup handler */ |
||
1041 | g_mutex_lock (&g_messages_lock); |
||
1042 | depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth)); |
||
1043 | domain = g_log_find_domain_L (log_domain ? log_domain : ""); |
||
1044 | if (depth) |
||
1045 | test_level |= G_LOG_FLAG_RECURSION; |
||
1046 | depth++; |
||
1047 | domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK; |
||
1048 | if ((domain_fatal_mask | g_log_always_fatal) & test_level) |
||
1049 | test_level |= G_LOG_FLAG_FATAL; |
||
1050 | if (test_level & G_LOG_FLAG_RECURSION) |
||
1051 | log_func = _g_log_fallback_handler; |
||
1052 | else |
||
1053 | log_func = g_log_domain_get_handler_L (domain, test_level, &data); |
||
1054 | domain = NULL; |
||
1055 | g_mutex_unlock (&g_messages_lock); |
||
1056 | |||
1057 | g_private_set (&g_log_depth, GUINT_TO_POINTER (depth)); |
||
1058 | |||
1059 | log_func (log_domain, test_level, msg, data); |
||
1060 | |||
1061 | if ((test_level & G_LOG_FLAG_FATAL) |
||
1062 | && !(test_level & G_LOG_LEVEL_ERROR)) |
||
1063 | { |
||
1064 | masquerade_fatal = fatal_log_func |
||
1065 | && !fatal_log_func (log_domain, test_level, msg, fatal_log_data); |
||
1066 | } |
||
1067 | |||
1068 | if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal) |
||
1069 | { |
||
1070 | #ifdef G_OS_WIN32 |
||
1071 | if (win32_keep_fatal_message) |
||
1072 | { |
||
1073 | gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL); |
||
1074 | |||
1075 | MessageBox (NULL, locale_msg, NULL, |
||
1076 | MB_ICONERROR|MB_SETFOREGROUND); |
||
1077 | } |
||
1078 | _g_log_abort (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION)); |
||
1079 | #else |
||
1080 | _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION)); |
||
1081 | #endif /* !G_OS_WIN32 */ |
||
1082 | } |
||
1083 | |||
1084 | depth--; |
||
1085 | g_private_set (&g_log_depth, GUINT_TO_POINTER (depth)); |
||
1086 | } |
||
1087 | } |
||
1088 | |||
1089 | g_free (msg_alloc); |
||
1090 | } |
||
1091 | |||
1092 | /** |
||
1093 | * g_log: |
||
1094 | * @log_domain: (nullable): the log domain, usually #G_LOG_DOMAIN, or %NULL |
||
1095 | * for the default |
||
1096 | * @log_level: the log level, either from #GLogLevelFlags |
||
1097 | * or a user-defined level |
||
1098 | * @format: the message format. See the printf() documentation |
||
1099 | * @...: the parameters to insert into the format string |
||
1100 | * |
||
1101 | * Logs an error or debugging message. |
||
1102 | * |
||
1103 | * If the log level has been set as fatal, the abort() |
||
1104 | * function is called to terminate the program. |
||
1105 | * |
||
1106 | * If g_log_default_handler() is used as the log handler function, a new-line |
||
1107 | * character will automatically be appended to @..., and need not be entered |
||
1108 | * manually. |
||
1109 | */ |
||
1110 | void |
||
1111 | g_log (const gchar *log_domain, |
||
1112 | GLogLevelFlags log_level, |
||
1113 | const gchar *format, |
||
1114 | ...) |
||
1115 | { |
||
1116 | va_list args; |
||
1117 | |||
1118 | va_start (args, format); |
||
1119 | g_logv (log_domain, log_level, format, args); |
||
1120 | va_end (args); |
||
1121 | } |
||
1122 | |||
1123 | /** |
||
1124 | * g_return_if_fail_warning: (skip) |
||
1125 | * @log_domain: (nullable): |
||
1126 | * @pretty_function: |
||
1127 | * @expression: (nullable): |
||
1128 | */ |
||
1129 | void |
||
1130 | g_return_if_fail_warning (const char *log_domain, |
||
1131 | const char *pretty_function, |
||
1132 | const char *expression) |
||
1133 | { |
||
1134 | g_log (log_domain, |
||
1135 | G_LOG_LEVEL_CRITICAL, |
||
1136 | "%s: assertion '%s' failed", |
||
1137 | pretty_function, |
||
1138 | expression); |
||
1139 | } |
||
1140 | |||
1141 | /** |
||
1142 | * g_warn_message: (skip) |
||
1143 | * @domain: (nullable): |
||
1144 | * @file: |
||
1145 | * @line: |
||
1146 | * @func: |
||
1147 | * @warnexpr: (nullable): |
||
1148 | */ |
||
1149 | void |
||
1150 | g_warn_message (const char *domain, |
||
1151 | const char *file, |
||
1152 | int line, |
||
1153 | const char *func, |
||
1154 | const char *warnexpr) |
||
1155 | { |
||
1156 | char *s, lstr[32]; |
||
1157 | g_snprintf (lstr, 32, "%d", line); |
||
1158 | if (warnexpr) |
||
1159 | s = g_strconcat ("(", file, ":", lstr, "):", |
||
1160 | func, func[0] ? ":" : "", |
||
1161 | " runtime check failed: (", warnexpr, ")", NULL); |
||
1162 | else |
||
1163 | s = g_strconcat ("(", file, ":", lstr, "):", |
||
1164 | func, func[0] ? ":" : "", |
||
1165 | " ", "code should not be reached", NULL); |
||
1166 | g_log (domain, G_LOG_LEVEL_WARNING, "%s", s); |
||
1167 | g_free (s); |
||
1168 | } |
||
1169 | |||
1170 | void |
||
1171 | g_assert_warning (const char *log_domain, |
||
1172 | const char *file, |
||
1173 | const int line, |
||
1174 | const char *pretty_function, |
||
1175 | const char *expression) |
||
1176 | { |
||
1177 | if (expression) |
||
1178 | g_log (log_domain, |
||
1179 | G_LOG_LEVEL_ERROR, |
||
1180 | "file %s: line %d (%s): assertion failed: (%s)", |
||
1181 | file, |
||
1182 | line, |
||
1183 | pretty_function, |
||
1184 | expression); |
||
1185 | else |
||
1186 | g_log (log_domain, |
||
1187 | G_LOG_LEVEL_ERROR, |
||
1188 | "file %s: line %d (%s): should not be reached", |
||
1189 | file, |
||
1190 | line, |
||
1191 | pretty_function); |
||
1192 | _g_log_abort (FALSE); |
||
1193 | abort (); |
||
1194 | } |
||
1195 | |||
1196 | /** |
||
1197 | * g_test_expect_message: |
||
1198 | * @log_domain: (allow-none): the log domain of the message |
||
1199 | * @log_level: the log level of the message |
||
1200 | * @pattern: a glob-style [pattern][glib-Glob-style-pattern-matching] |
||
1201 | * |
||
1202 | * Indicates that a message with the given @log_domain and @log_level, |
||
1203 | * with text matching @pattern, is expected to be logged. When this |
||
1204 | * message is logged, it will not be printed, and the test case will |
||
1205 | * not abort. |
||
1206 | * |
||
1207 | * Use g_test_assert_expected_messages() to assert that all |
||
1208 | * previously-expected messages have been seen and suppressed. |
||
1209 | * |
||
1210 | * You can call this multiple times in a row, if multiple messages are |
||
1211 | * expected as a result of a single call. (The messages must appear in |
||
1212 | * the same order as the calls to g_test_expect_message().) |
||
1213 | * |
||
1214 | * For example: |
||
1215 | * |
||
1216 | * |[<!-- language="C" --> |
||
1217 | * // g_main_context_push_thread_default() should fail if the |
||
1218 | * // context is already owned by another thread. |
||
1219 | * g_test_expect_message (G_LOG_DOMAIN, |
||
1220 | * G_LOG_LEVEL_CRITICAL, |
||
1221 | * "assertion*acquired_context*failed"); |
||
1222 | * g_main_context_push_thread_default (bad_context); |
||
1223 | * g_test_assert_expected_messages (); |
||
1224 | * ]| |
||
1225 | * |
||
1226 | * Note that you cannot use this to test g_error() messages, since |
||
1227 | * g_error() intentionally never returns even if the program doesn't |
||
1228 | * abort; use g_test_trap_subprocess() in this case. |
||
1229 | * |
||
1230 | * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly |
||
1231 | * expected via g_test_expect_message() then they will be ignored. |
||
1232 | * |
||
1233 | * Since: 2.34 |
||
1234 | */ |
||
1235 | void |
||
1236 | g_test_expect_message (const gchar *log_domain, |
||
1237 | GLogLevelFlags log_level, |
||
1238 | const gchar *pattern) |
||
1239 | { |
||
1240 | GTestExpectedMessage *expected; |
||
1241 | |||
1242 | g_return_if_fail (log_level != 0); |
||
1243 | g_return_if_fail (pattern != NULL); |
||
1244 | g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR); |
||
1245 | |||
1246 | expected = g_new (GTestExpectedMessage, 1); |
||
1247 | expected->log_domain = g_strdup (log_domain); |
||
1248 | expected->log_level = log_level; |
||
1249 | expected->pattern = g_strdup (pattern); |
||
1250 | |||
1251 | expected_messages = g_slist_append (expected_messages, expected); |
||
1252 | } |
||
1253 | |||
1254 | void |
||
1255 | g_test_assert_expected_messages_internal (const char *domain, |
||
1256 | const char *file, |
||
1257 | int line, |
||
1258 | const char *func) |
||
1259 | { |
||
1260 | if (expected_messages) |
||
1261 | { |
||
1262 | GTestExpectedMessage *expected; |
||
1263 | gchar level_prefix[STRING_BUFFER_SIZE]; |
||
1264 | gchar *message; |
||
1265 | |||
1266 | expected = expected_messages->data; |
||
1267 | |||
1268 | mklevel_prefix (level_prefix, expected->log_level); |
||
1269 | message = g_strdup_printf ("Did not see expected message %s-%s: %s", |
||
1270 | expected->log_domain ? expected->log_domain : "**", |
||
1271 | level_prefix, expected->pattern); |
||
1272 | g_assertion_message (G_LOG_DOMAIN, file, line, func, message); |
||
1273 | g_free (message); |
||
1274 | } |
||
1275 | } |
||
1276 | |||
1277 | /** |
||
1278 | * g_test_assert_expected_messages: |
||
1279 | * |
||
1280 | * Asserts that all messages previously indicated via |
||
1281 | * g_test_expect_message() have been seen and suppressed. |
||
1282 | * |
||
1283 | * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly |
||
1284 | * expected via g_test_expect_message() then they will be ignored. |
||
1285 | * |
||
1286 | * Since: 2.34 |
||
1287 | */ |
||
1288 | |||
1289 | void |
||
1290 | _g_log_fallback_handler (const gchar *log_domain, |
||
1291 | GLogLevelFlags log_level, |
||
1292 | const gchar *message, |
||
1293 | gpointer unused_data) |
||
1294 | { |
||
1295 | gchar level_prefix[STRING_BUFFER_SIZE]; |
||
1296 | #ifndef G_OS_WIN32 |
||
1297 | gchar pid_string[FORMAT_UNSIGNED_BUFSIZE]; |
||
1298 | #endif |
||
1299 | FILE *stream; |
||
1300 | |||
1301 | /* we cannot call _any_ GLib functions in this fallback handler, |
||
1302 | * which is why we skip UTF-8 conversion, etc. |
||
1303 | * since we either recursed or ran out of memory, we're in a pretty |
||
1304 | * pathologic situation anyways, what we can do is giving the |
||
1305 | * the process ID unconditionally however. |
||
1306 | */ |
||
1307 | |||
1308 | stream = mklevel_prefix (level_prefix, log_level); |
||
1309 | if (!message) |
||
1310 | message = "(NULL) message"; |
||
1311 | |||
1312 | #ifndef G_OS_WIN32 |
||
1313 | format_unsigned (pid_string, getpid (), 10); |
||
1314 | #endif |
||
1315 | |||
1316 | if (log_domain) |
||
1317 | write_string (stream, "\n"); |
||
1318 | else |
||
1319 | write_string (stream, "\n** "); |
||
1320 | |||
1321 | #ifndef G_OS_WIN32 |
||
1322 | write_string (stream, "(process:"); |
||
1323 | write_string (stream, pid_string); |
||
1324 | write_string (stream, "): "); |
||
1325 | #endif |
||
1326 | |||
1327 | if (log_domain) |
||
1328 | { |
||
1329 | write_string (stream, log_domain); |
||
1330 | write_string (stream, "-"); |
||
1331 | } |
||
1332 | write_string (stream, level_prefix); |
||
1333 | write_string (stream, ": "); |
||
1334 | write_string (stream, message); |
||
1335 | } |
||
1336 | |||
1337 | static void |
||
1338 | escape_string (GString *string) |
||
1339 | { |
||
1340 | const char *p = string->str; |
||
1341 | gunichar wc; |
||
1342 | |||
1343 | while (p < string->str + string->len) |
||
1344 | { |
||
1345 | gboolean safe; |
||
1346 | |||
1347 | wc = g_utf8_get_char_validated (p, -1); |
||
1348 | if (wc == (gunichar)-1 || wc == (gunichar)-2) |
||
1349 | { |
||
1350 | gchar *tmp; |
||
1351 | guint pos; |
||
1352 | |||
1353 | pos = p - string->str; |
||
1354 | |||
1355 | /* Emit invalid UTF-8 as hex escapes |
||
1356 | */ |
||
1357 | tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p); |
||
1358 | g_string_erase (string, pos, 1); |
||
1359 | g_string_insert (string, pos, tmp); |
||
1360 | |||
1361 | p = string->str + (pos + 4); /* Skip over escape sequence */ |
||
1362 | |||
1363 | g_free (tmp); |
||
1364 | continue; |
||
1365 | } |
||
1366 | if (wc == '\r') |
||
1367 | { |
||
1368 | safe = *(p + 1) == '\n'; |
||
1369 | } |
||
1370 | else |
||
1371 | { |
||
1372 | safe = CHAR_IS_SAFE (wc); |
||
1373 | } |
||
1374 | |||
1375 | if (!safe) |
||
1376 | { |
||
1377 | gchar *tmp; |
||
1378 | guint pos; |
||
1379 | |||
1380 | pos = p - string->str; |
||
1381 | |||
1382 | /* Largest char we escape is 0x0a, so we don't have to worry |
||
1383 | * about 8-digit \Uxxxxyyyy |
||
1384 | */ |
||
1385 | tmp = g_strdup_printf ("\\u%04x", wc); |
||
1386 | g_string_erase (string, pos, g_utf8_next_char (p) - p); |
||
1387 | g_string_insert (string, pos, tmp); |
||
1388 | g_free (tmp); |
||
1389 | |||
1390 | p = string->str + (pos + 6); /* Skip over escape sequence */ |
||
1391 | } |
||
1392 | else |
||
1393 | p = g_utf8_next_char (p); |
||
1394 | } |
||
1395 | } |
||
1396 | |||
1397 | /** |
||
1398 | * g_log_default_handler: |
||
1399 | * @log_domain: (nullable): the log domain of the message, or %NULL for the |
||
1400 | * default "" application domain |
||
1401 | * @log_level: the level of the message |
||
1402 | * @message: (nullable): the message |
||
1403 | * @unused_data: (nullable): data passed from g_log() which is unused |
||
1404 | * |
||
1405 | * The default log handler set up by GLib; g_log_set_default_handler() |
||
1406 | * allows to install an alternate default log handler. |
||
1407 | * This is used if no log handler has been set for the particular log |
||
1408 | * domain and log level combination. It outputs the message to stderr |
||
1409 | * or stdout and if the log level is fatal it calls abort(). It automatically |
||
1410 | * prints a new-line character after the message, so one does not need to be |
||
1411 | * manually included in @message. |
||
1412 | * |
||
1413 | * The behavior of this log handler can be influenced by a number of |
||
1414 | * environment variables: |
||
1415 | * |
||
1416 | * - `G_MESSAGES_PREFIXED`: A :-separated list of log levels for which |
||
1417 | * messages should be prefixed by the program name and PID of the |
||
1418 | * aplication. |
||
1419 | * |
||
1420 | * - `G_MESSAGES_DEBUG`: A space-separated list of log domains for |
||
1421 | * which debug and informational messages are printed. By default |
||
1422 | * these messages are not printed. |
||
1423 | * |
||
1424 | * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL, |
||
1425 | * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for |
||
1426 | * the rest. |
||
1427 | */ |
||
1428 | void |
||
1429 | g_log_default_handler (const gchar *log_domain, |
||
1430 | GLogLevelFlags log_level, |
||
1431 | const gchar *message, |
||
1432 | gpointer unused_data) |
||
1433 | { |
||
1434 | gchar level_prefix[STRING_BUFFER_SIZE], *string; |
||
1435 | GString *gstring; |
||
1436 | FILE *stream; |
||
1437 | const gchar *domains; |
||
1438 | |||
1439 | if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT)) |
||
1440 | goto emit; |
||
1441 | |||
1442 | domains = g_getenv ("G_MESSAGES_DEBUG"); |
||
1443 | if (((log_level & INFO_LEVELS) == 0) || |
||
1444 | domains == NULL || |
||
1445 | (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain)))) |
||
1446 | return; |
||
1447 | |||
1448 | emit: |
||
1449 | /* we can be called externally with recursion for whatever reason */ |
||
1450 | if (log_level & G_LOG_FLAG_RECURSION) |
||
1451 | { |
||
1452 | _g_log_fallback_handler (log_domain, log_level, message, unused_data); |
||
1453 | return; |
||
1454 | } |
||
1455 | |||
1456 | stream = mklevel_prefix (level_prefix, log_level); |
||
1457 | |||
1458 | gstring = g_string_new (NULL); |
||
1459 | if (log_level & ALERT_LEVELS) |
||
1460 | g_string_append (gstring, "\n"); |
||
1461 | if (!log_domain) |
||
1462 | g_string_append (gstring, "** "); |
||
1463 | |||
1464 | if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK)) |
||
1465 | { |
||
1466 | const gchar *prg_name = g_get_prgname (); |
||
1467 | |||
1468 | if (!prg_name) |
||
1469 | g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ()); |
||
1470 | else |
||
1471 | g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ()); |
||
1472 | } |
||
1473 | |||
1474 | if (log_domain) |
||
1475 | { |
||
1476 | g_string_append (gstring, log_domain); |
||
1477 | g_string_append_c (gstring, '-'); |
||
1478 | } |
||
1479 | g_string_append (gstring, level_prefix); |
||
1480 | |||
1481 | g_string_append (gstring, ": "); |
||
1482 | if (!message) |
||
1483 | g_string_append (gstring, "(NULL) message"); |
||
1484 | else |
||
1485 | { |
||
1486 | GString *msg; |
||
1487 | const gchar *charset; |
||
1488 | |||
1489 | msg = g_string_new (message); |
||
1490 | escape_string (msg); |
||
1491 | |||
1492 | if (g_get_charset (&charset)) |
||
1493 | g_string_append (gstring, msg->str); /* charset is UTF-8 already */ |
||
1494 | else |
||
1495 | { |
||
1496 | string = strdup_convert (msg->str, charset); |
||
1497 | g_string_append (gstring, string); |
||
1498 | g_free (string); |
||
1499 | } |
||
1500 | |||
1501 | g_string_free (msg, TRUE); |
||
1502 | } |
||
1503 | g_string_append (gstring, "\n"); |
||
1504 | |||
1505 | string = g_string_free (gstring, FALSE); |
||
1506 | |||
1507 | write_string (stream, string); |
||
1508 | g_free (string); |
||
1509 | } |
||
1510 | |||
1511 | /** |
||
1512 | * g_set_print_handler: |
||
1513 | * @func: the new print handler |
||
1514 | * |
||
1515 | * Sets the print handler. |
||
1516 | * |
||
1517 | * Any messages passed to g_print() will be output via |
||
1518 | * the new handler. The default handler simply outputs |
||
1519 | * the message to stdout. By providing your own handler |
||
1520 | * you can redirect the output, to a GTK+ widget or a |
||
1521 | * log file for example. |
||
1522 | * |
||
1523 | * Returns: the old print handler |
||
1524 | */ |
||
1525 | GPrintFunc |
||
1526 | g_set_print_handler (GPrintFunc func) |
||
1527 | { |
||
1528 | GPrintFunc old_print_func; |
||
1529 | |||
1530 | g_mutex_lock (&g_messages_lock); |
||
1531 | old_print_func = glib_print_func; |
||
1532 | glib_print_func = func; |
||
1533 | g_mutex_unlock (&g_messages_lock); |
||
1534 | |||
1535 | return old_print_func; |
||
1536 | } |
||
1537 | |||
1538 | /** |
||
1539 | * g_print: |
||
1540 | * @format: the message format. See the printf() documentation |
||
1541 | * @...: the parameters to insert into the format string |
||
1542 | * |
||
1543 | * Outputs a formatted message via the print handler. |
||
1544 | * The default print handler simply outputs the message to stdout, without |
||
1545 | * appending a trailing new-line character. Typically, @format should end with |
||
1546 | * its own new-line character. |
||
1547 | * |
||
1548 | * g_print() should not be used from within libraries for debugging |
||
1549 | * messages, since it may be redirected by applications to special |
||
1550 | * purpose message windows or even files. Instead, libraries should |
||
1551 | * use g_log(), or the convenience functions g_message(), g_warning() |
||
1552 | * and g_error(). |
||
1553 | */ |
||
1554 | void |
||
1555 | g_print (const gchar *format, |
||
1556 | ...) |
||
1557 | { |
||
1558 | va_list args; |
||
1559 | gchar *string; |
||
1560 | GPrintFunc local_glib_print_func; |
||
1561 | |||
1562 | g_return_if_fail (format != NULL); |
||
1563 | |||
1564 | va_start (args, format); |
||
1565 | string = g_strdup_vprintf (format, args); |
||
1566 | va_end (args); |
||
1567 | |||
1568 | g_mutex_lock (&g_messages_lock); |
||
1569 | local_glib_print_func = glib_print_func; |
||
1570 | g_mutex_unlock (&g_messages_lock); |
||
1571 | |||
1572 | if (local_glib_print_func) |
||
1573 | local_glib_print_func (string); |
||
1574 | else |
||
1575 | { |
||
1576 | const gchar *charset; |
||
1577 | |||
1578 | if (g_get_charset (&charset)) |
||
1579 | fputs (string, stdout); /* charset is UTF-8 already */ |
||
1580 | else |
||
1581 | { |
||
1582 | gchar *lstring = strdup_convert (string, charset); |
||
1583 | |||
1584 | fputs (lstring, stdout); |
||
1585 | g_free (lstring); |
||
1586 | } |
||
1587 | fflush (stdout); |
||
1588 | } |
||
1589 | g_free (string); |
||
1590 | } |
||
1591 | |||
1592 | /** |
||
1593 | * g_set_printerr_handler: |
||
1594 | * @func: the new error message handler |
||
1595 | * |
||
1596 | * Sets the handler for printing error messages. |
||
1597 | * |
||
1598 | * Any messages passed to g_printerr() will be output via |
||
1599 | * the new handler. The default handler simply outputs the |
||
1600 | * message to stderr. By providing your own handler you can |
||
1601 | * redirect the output, to a GTK+ widget or a log file for |
||
1602 | * example. |
||
1603 | * |
||
1604 | * Returns: the old error message handler |
||
1605 | */ |
||
1606 | GPrintFunc |
||
1607 | g_set_printerr_handler (GPrintFunc func) |
||
1608 | { |
||
1609 | GPrintFunc old_printerr_func; |
||
1610 | |||
1611 | g_mutex_lock (&g_messages_lock); |
||
1612 | old_printerr_func = glib_printerr_func; |
||
1613 | glib_printerr_func = func; |
||
1614 | g_mutex_unlock (&g_messages_lock); |
||
1615 | |||
1616 | return old_printerr_func; |
||
1617 | } |
||
1618 | |||
1619 | /** |
||
1620 | * g_printerr: |
||
1621 | * @format: the message format. See the printf() documentation |
||
1622 | * @...: the parameters to insert into the format string |
||
1623 | * |
||
1624 | * Outputs a formatted message via the error message handler. |
||
1625 | * The default handler simply outputs the message to stderr, without appending |
||
1626 | * a trailing new-line character. Typically, @format should end with its own |
||
1627 | * new-line character. |
||
1628 | * |
||
1629 | * g_printerr() should not be used from within libraries. |
||
1630 | * Instead g_log() should be used, or the convenience functions |
||
1631 | * g_message(), g_warning() and g_error(). |
||
1632 | */ |
||
1633 | void |
||
1634 | g_printerr (const gchar *format, |
||
1635 | ...) |
||
1636 | { |
||
1637 | va_list args; |
||
1638 | gchar *string; |
||
1639 | GPrintFunc local_glib_printerr_func; |
||
1640 | |||
1641 | g_return_if_fail (format != NULL); |
||
1642 | |||
1643 | va_start (args, format); |
||
1644 | string = g_strdup_vprintf (format, args); |
||
1645 | va_end (args); |
||
1646 | |||
1647 | g_mutex_lock (&g_messages_lock); |
||
1648 | local_glib_printerr_func = glib_printerr_func; |
||
1649 | g_mutex_unlock (&g_messages_lock); |
||
1650 | |||
1651 | if (local_glib_printerr_func) |
||
1652 | local_glib_printerr_func (string); |
||
1653 | else |
||
1654 | { |
||
1655 | const gchar *charset; |
||
1656 | |||
1657 | if (g_get_charset (&charset)) |
||
1658 | fputs (string, stderr); /* charset is UTF-8 already */ |
||
1659 | else |
||
1660 | { |
||
1661 | gchar *lstring = strdup_convert (string, charset); |
||
1662 | |||
1663 | fputs (lstring, stderr); |
||
1664 | g_free (lstring); |
||
1665 | } |
||
1666 | fflush (stderr); |
||
1667 | } |
||
1668 | g_free (string); |
||
1669 | } |
||
1670 | |||
1671 | /** |
||
1672 | * g_printf_string_upper_bound: |
||
1673 | * @format: the format string. See the printf() documentation |
||
1674 | * @args: the parameters to be inserted into the format string |
||
1675 | * |
||
1676 | * Calculates the maximum space needed to store the output |
||
1677 | * of the sprintf() function. |
||
1678 | * |
||
1679 | * Returns: the maximum space needed to store the formatted string |
||
1680 | */ |
||
1681 | gsize |
||
1682 | g_printf_string_upper_bound (const gchar *format, |
||
1683 | va_list args) |
||
1684 | { |
||
1685 | gchar c; |
||
1686 | return _g_vsnprintf (&c, 1, format, args) + 1; |
||
1687 | } |