1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 * @Title: Message Output and Debugging Functions
34 * @Short_description: functions to output messages and help debug applications
36 * These functions provide support for outputting messages.
38 * The <function>g_return</function> family of macros (g_return_if_fail(),
39 * g_return_val_if_fail(), g_return_if_reached(), g_return_val_if_reached())
40 * should only be used for programming errors, a typical use case is
41 * checking for invalid parameters at the beginning of a public function.
42 * They should not be used if you just mean "if (error) return", they
43 * should only be used if you mean "if (bug in program) return".
44 * The program behavior is generally considered undefined after one
45 * of these checks fails. They are not intended for normal control
46 * flow, only to give a perhaps-helpful warning before giving up.
59 #include "glib-init.h"
60 #include "gbacktrace.h"
65 #include "gprintfint.h"
66 #include "gtestutils.h"
68 #include "gstrfuncs.h"
77 #include <process.h> /* For getpid() */
79 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
86 * @title: Message Logging
87 * @short_description: versatile support for logging messages
88 * with different levels of importance
90 * These functions provide support for logging error messages
91 * or messages used for debugging.
93 * There are several built-in levels of messages, defined in
94 * #GLogLevelFlags. These can be extended with user-defined levels.
100 * Defines the log domain.
102 * For applications, this is typically left as the default %NULL
103 * (or "") domain. Libraries should define this so that any messages
104 * which they log can be differentiated from messages from other
105 * libraries and application code. But be careful not to define
106 * it in any public header files.
108 * For example, GTK+ uses this in its Makefile.am:
110 * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
117 * GLib log levels that are considered fatal by default.
122 * @log_domain: the log domain of the message
123 * @log_level: the log level of the message (including the
124 * fatal and recursion flags)
125 * @message: the message to process
126 * @user_data: user data, set in g_log_set_handler()
128 * Specifies the prototype of log handler functions.
130 * The default log handler, g_log_default_handler(), automatically appends a
131 * new-line character to @message when printing it. It is advised that any
132 * custom log handler functions behave similarly, so that logging calls in user
133 * code do not need modifying to add a new-line character to the message if the
134 * log handler is changed.
139 * @G_LOG_FLAG_RECURSION: internal flag
140 * @G_LOG_FLAG_FATAL: internal flag
141 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
142 * This level is also used for messages produced by g_assert().
143 * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see 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
152 * Flags specifying the level of log messages.
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().
160 * @...: format string, followed by parameters to insert
161 * into the format string (as with printf())
163 * A convenience function/macro to log a normal message.
165 * If g_log_default_handler() is used as the log handler function, a new-line
166 * character will automatically be appended to @..., and need not be entered
172 * @...: format string, followed by parameters to insert
173 * into the format string (as with printf())
175 * A convenience function/macro to log a warning message.
177 * You can make warnings fatal at runtime by setting the
178 * <envar>G_DEBUG</envar> environment variable (see
179 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
181 * If g_log_default_handler() is used as the log handler function, a new-line
182 * character will automatically be appended to @..., and need not be entered
188 * @...: format string, followed by parameters to insert
189 * into the format string (as with printf())
191 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
192 * It's more or less application-defined what constitutes
193 * a critical vs. a regular warning. You could call
194 * g_log_set_always_fatal() to make critical warnings exit
195 * the program, then use g_critical() for fatal errors, for
198 * You can also make critical warnings fatal at runtime by
199 * setting the <envar>G_DEBUG</envar> environment variable (see
200 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
202 * If g_log_default_handler() is used as the log handler function, a new-line
203 * character will automatically be appended to @..., and need not be entered
209 * @...: format string, followed by parameters to insert
210 * into the format string (as with printf())
212 * A convenience function/macro to log an error message.
214 * Error messages are always fatal, resulting in a call to
215 * abort() to terminate the application. This function will
216 * result in a core dump; don't use it for errors you expect.
217 * Using this function indicates a bug in your program, i.e.
218 * an assertion failure.
220 * If g_log_default_handler() is used as the log handler function, a new-line
221 * character will automatically be appended to @..., and need not be entered
228 * @...: format string, followed by parameters to insert
229 * into the format string (as with printf())
231 * A convenience function/macro to log an informational message. Seldom used.
233 * If g_log_default_handler() is used as the log handler function, a new-line
234 * character will automatically be appended to @..., and need not be entered
237 * Such messages are suppressed by the g_log_default_handler() unless
238 * the G_MESSAGES_DEBUG environment variable is set appropriately.
245 * @...: format string, followed by parameters to insert
246 * into the format string (as with printf())
248 * A convenience function/macro to log a debug message.
250 * If g_log_default_handler() is used as the log handler function, a new-line
251 * character will automatically be appended to @..., and need not be entered
254 * Such messages are suppressed by the g_log_default_handler() unless
255 * the G_MESSAGES_DEBUG environment variable is set appropriately.
260 /* --- structures --- */
261 typedef struct _GLogDomain GLogDomain;
262 typedef struct _GLogHandler GLogHandler;
266 GLogLevelFlags fatal_mask;
267 GLogHandler *handlers;
273 GLogLevelFlags log_level;
280 /* --- variables --- */
281 static GMutex g_messages_lock;
282 static GLogDomain *g_log_domains = NULL;
283 static GPrintFunc glib_print_func = NULL;
284 static GPrintFunc glib_printerr_func = NULL;
285 static GPrivate g_log_depth;
286 static GLogFunc default_log_func = g_log_default_handler;
287 static gpointer default_log_data = NULL;
288 static GTestLogFatalFunc fatal_log_func = NULL;
289 static gpointer fatal_log_data;
291 /* --- functions --- */
293 static void _g_log_abort (gboolean breakpoint) G_GNUC_NORETURN;
296 _g_log_abort (gboolean breakpoint)
298 if (g_test_subprocess ())
300 /* If this is a test case subprocess then it probably caused
301 * this error message on purpose, so just exit() rather than
302 * abort()ing, to avoid triggering any system crash-reporting
315 # include <windows.h>
316 static gboolean win32_keep_fatal_message = FALSE;
318 /* This default message will usually be overwritten. */
319 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
320 * called with huge strings, is it?
322 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
323 static gchar *fatal_msg_ptr = fatal_msg_buf;
331 if (win32_keep_fatal_message)
333 memcpy (fatal_msg_ptr, buf, len);
334 fatal_msg_ptr += len;
339 write (fd, buf, len);
343 #define write(fd, buf, len) dowrite(fd, buf, len)
348 write_string (int fd,
353 res = write (fd, string, strlen (string));
354 while (G_UNLIKELY (res == -1 && errno == EINTR));
358 g_log_find_domain_L (const gchar *log_domain)
360 register GLogDomain *domain;
362 domain = g_log_domains;
365 if (strcmp (domain->log_domain, log_domain) == 0)
367 domain = domain->next;
373 g_log_domain_new_L (const gchar *log_domain)
375 register GLogDomain *domain;
377 domain = g_new (GLogDomain, 1);
378 domain->log_domain = g_strdup (log_domain);
379 domain->fatal_mask = G_LOG_FATAL_MASK;
380 domain->handlers = NULL;
382 domain->next = g_log_domains;
383 g_log_domains = domain;
389 g_log_domain_check_free_L (GLogDomain *domain)
391 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
392 domain->handlers == NULL)
394 register GLogDomain *last, *work;
398 work = g_log_domains;
404 last->next = domain->next;
406 g_log_domains = domain->next;
407 g_free (domain->log_domain);
418 g_log_domain_get_handler_L (GLogDomain *domain,
419 GLogLevelFlags log_level,
422 if (domain && log_level)
424 register GLogHandler *handler;
426 handler = domain->handlers;
429 if ((handler->log_level & log_level) == log_level)
431 *data = handler->data;
432 return handler->log_func;
434 handler = handler->next;
438 *data = default_log_data;
439 return default_log_func;
443 * g_log_set_always_fatal:
444 * @fatal_mask: the mask containing bits set for each level
445 * of error which is to be fatal
447 * Sets the message levels which are always fatal, in any log domain.
448 * When a message with any of these levels is logged the program terminates.
449 * You can only set the levels defined by GLib to be fatal.
450 * %G_LOG_LEVEL_ERROR is always fatal.
452 * You can also make some message levels fatal at runtime by setting
453 * the <envar>G_DEBUG</envar> environment variable (see
454 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
456 * Returns: the old fatal mask
459 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
461 GLogLevelFlags old_mask;
463 /* restrict the global mask to levels that are known to glib
464 * since this setting applies to all domains
466 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
467 /* force errors to be fatal */
468 fatal_mask |= G_LOG_LEVEL_ERROR;
469 /* remove bogus flag */
470 fatal_mask &= ~G_LOG_FLAG_FATAL;
472 g_mutex_lock (&g_messages_lock);
473 old_mask = g_log_always_fatal;
474 g_log_always_fatal = fatal_mask;
475 g_mutex_unlock (&g_messages_lock);
481 * g_log_set_fatal_mask:
482 * @log_domain: the log domain
483 * @fatal_mask: the new fatal mask
485 * Sets the log levels which are fatal in the given domain.
486 * %G_LOG_LEVEL_ERROR is always fatal.
488 * Returns: the old fatal mask for the log domain
491 g_log_set_fatal_mask (const gchar *log_domain,
492 GLogLevelFlags fatal_mask)
494 GLogLevelFlags old_flags;
495 register GLogDomain *domain;
500 /* force errors to be fatal */
501 fatal_mask |= G_LOG_LEVEL_ERROR;
502 /* remove bogus flag */
503 fatal_mask &= ~G_LOG_FLAG_FATAL;
505 g_mutex_lock (&g_messages_lock);
507 domain = g_log_find_domain_L (log_domain);
509 domain = g_log_domain_new_L (log_domain);
510 old_flags = domain->fatal_mask;
512 domain->fatal_mask = fatal_mask;
513 g_log_domain_check_free_L (domain);
515 g_mutex_unlock (&g_messages_lock);
522 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
524 * @log_levels: the log levels to apply the log handler for.
525 * To handle fatal and recursive messages as well, combine
526 * the log levels with the #G_LOG_FLAG_FATAL and
527 * #G_LOG_FLAG_RECURSION bit flags.
528 * @log_func: the log handler function
529 * @user_data: data passed to the log handler
531 * Sets the log handler for a domain and a set of log levels.
532 * To handle fatal and recursive messages the @log_levels parameter
533 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
536 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
537 * you want to set a handler for this log level you must combine it with
541 * <title>Adding a log handler for all warning messages in the default
542 * (application) domain</title>
544 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
545 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
550 * <title>Adding a log handler for all critical messages from GTK+</title>
552 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
553 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
558 * <title>Adding a log handler for <emphasis>all</emphasis> messages from
561 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
562 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
566 * Returns: the id of the new handler
569 g_log_set_handler (const gchar *log_domain,
570 GLogLevelFlags log_levels,
574 static guint handler_id = 0;
576 GLogHandler *handler;
578 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
579 g_return_val_if_fail (log_func != NULL, 0);
584 handler = g_new (GLogHandler, 1);
586 g_mutex_lock (&g_messages_lock);
588 domain = g_log_find_domain_L (log_domain);
590 domain = g_log_domain_new_L (log_domain);
592 handler->id = ++handler_id;
593 handler->log_level = log_levels;
594 handler->log_func = log_func;
595 handler->data = user_data;
596 handler->next = domain->handlers;
597 domain->handlers = handler;
599 g_mutex_unlock (&g_messages_lock);
605 * g_log_set_default_handler:
606 * @log_func: the log handler function
607 * @user_data: data passed to the log handler
609 * Installs a default log handler which is used if no
610 * log handler has been set for the particular log domain
611 * and log level combination. By default, GLib uses
612 * g_log_default_handler() as default log handler.
614 * Returns: the previous default log handler
619 g_log_set_default_handler (GLogFunc log_func,
622 GLogFunc old_log_func;
624 g_mutex_lock (&g_messages_lock);
625 old_log_func = default_log_func;
626 default_log_func = log_func;
627 default_log_data = user_data;
628 g_mutex_unlock (&g_messages_lock);
634 * g_test_log_set_fatal_handler:
635 * @log_func: the log handler function.
636 * @user_data: data passed to the log handler.
638 * Installs a non-error fatal log handler which can be
639 * used to decide whether log messages which are counted
640 * as fatal abort the program.
642 * The use case here is that you are running a test case
643 * that depends on particular libraries or circumstances
644 * and cannot prevent certain known critical or warning
645 * messages. So you install a handler that compares the
646 * domain and message to precisely not abort in such a case.
648 * Note that the handler is reset at the beginning of
649 * any test case, so you have to set it inside each test
650 * function which needs the special behavior.
652 * This handler has no effect on g_error messages.
657 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
660 g_mutex_lock (&g_messages_lock);
661 fatal_log_func = log_func;
662 fatal_log_data = user_data;
663 g_mutex_unlock (&g_messages_lock);
667 * g_log_remove_handler:
668 * @log_domain: the log domain
669 * @handler_id: the id of the handler, which was returned
670 * in g_log_set_handler()
672 * Removes the log handler.
675 g_log_remove_handler (const gchar *log_domain,
678 register GLogDomain *domain;
680 g_return_if_fail (handler_id > 0);
685 g_mutex_lock (&g_messages_lock);
686 domain = g_log_find_domain_L (log_domain);
689 GLogHandler *work, *last;
692 work = domain->handlers;
695 if (work->id == handler_id)
698 last->next = work->next;
700 domain->handlers = work->next;
701 g_log_domain_check_free_L (domain);
702 g_mutex_unlock (&g_messages_lock);
710 g_mutex_unlock (&g_messages_lock);
711 g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
712 G_STRLOC, handler_id, log_domain);
715 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
717 (wc >= 0x80 && wc < 0xa0)))
720 strdup_convert (const gchar *string,
721 const gchar *charset)
723 if (!g_utf8_validate (string, -1, NULL))
725 GString *gstring = g_string_new ("[Invalid UTF-8] ");
728 for (p = (guchar *)string; *p; p++)
730 if (CHAR_IS_SAFE(*p) &&
731 !(*p == '\r' && *(p + 1) != '\n') &&
733 g_string_append_c (gstring, *p);
735 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
738 return g_string_free (gstring, FALSE);
744 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
749 /* Not thread-safe, but doesn't matter if we print the warning twice
751 static gboolean warned = FALSE;
755 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
759 return g_strdup (string);
764 /* For a radix of 8 we need at most 3 output bytes for 1 input
765 * byte. Additionally we might need up to 2 output bytes for the
766 * readix prefix and 1 byte for the trailing NULL.
768 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
771 format_unsigned (gchar *buf,
779 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
781 if (radix != 8 && radix != 10 && radix != 16)
814 /* Again we can't use g_assert; actually this check should _never_ fail. */
815 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
828 buf[i] = c + 'a' - 10;
835 /* string size big enough to hold level prefix */
836 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
838 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
840 /* these are emitted by the default log handler */
841 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
842 /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
843 #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
846 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
847 GLogLevelFlags log_level)
849 gboolean to_stdout = TRUE;
851 /* we may not call _any_ GLib functions here */
853 switch (log_level & G_LOG_LEVEL_MASK)
855 case G_LOG_LEVEL_ERROR:
856 strcpy (level_prefix, "ERROR");
859 case G_LOG_LEVEL_CRITICAL:
860 strcpy (level_prefix, "CRITICAL");
863 case G_LOG_LEVEL_WARNING:
864 strcpy (level_prefix, "WARNING");
867 case G_LOG_LEVEL_MESSAGE:
868 strcpy (level_prefix, "Message");
871 case G_LOG_LEVEL_INFO:
872 strcpy (level_prefix, "INFO");
874 case G_LOG_LEVEL_DEBUG:
875 strcpy (level_prefix, "DEBUG");
880 strcpy (level_prefix, "LOG-");
881 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
884 strcpy (level_prefix, "LOG");
887 if (log_level & G_LOG_FLAG_RECURSION)
888 strcat (level_prefix, " (recursed)");
889 if (log_level & ALERT_LEVELS)
890 strcat (level_prefix, " **");
893 if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
894 win32_keep_fatal_message = TRUE;
896 return to_stdout ? 1 : 2;
901 GLogLevelFlags log_level;
903 } GTestExpectedMessage;
905 static GSList *expected_messages = NULL;
909 * @log_domain: the log domain
910 * @log_level: the log level
911 * @format: the message format. See the printf() documentation
912 * @args: the parameters to insert into the format string
914 * Logs an error or debugging message.
916 * If the log level has been set as fatal, the abort()
917 * function is called to terminate the program.
919 * If g_log_default_handler() is used as the log handler function, a new-line
920 * character will automatically be appended to @..., and need not be entered
924 g_logv (const gchar *log_domain,
925 GLogLevelFlags log_level,
929 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
930 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
931 gchar buffer[1025], *msg, *msg_alloc = NULL;
934 log_level &= G_LOG_LEVEL_MASK;
938 if (log_level & G_LOG_FLAG_RECURSION)
940 /* we use a stack buffer of fixed size, since we're likely
941 * in an out-of-memory situation
943 gsize size G_GNUC_UNUSED;
945 size = _g_vsnprintf (buffer, 1024, format, args);
949 msg = msg_alloc = g_strdup_vprintf (format, args);
951 if (expected_messages)
953 GTestExpectedMessage *expected = expected_messages->data;
955 if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
956 ((log_level & expected->log_level) == expected->log_level) &&
957 g_pattern_match_simple (expected->pattern, msg))
959 expected_messages = g_slist_delete_link (expected_messages,
961 g_free (expected->log_domain);
962 g_free (expected->pattern);
967 else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
969 gchar level_prefix[STRING_BUFFER_SIZE];
970 gchar *expected_message;
972 mklevel_prefix (level_prefix, expected->log_level);
973 expected_message = g_strdup_printf ("Did not see expected message %s: %s",
974 level_prefix, expected->pattern);
975 g_log_default_handler (log_domain, log_level, expected_message, NULL);
976 g_free (expected_message);
978 log_level |= G_LOG_FLAG_FATAL;
982 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
984 register GLogLevelFlags test_level;
987 if (log_level & test_level)
991 GLogLevelFlags domain_fatal_mask;
992 gpointer data = NULL;
993 gboolean masquerade_fatal = FALSE;
997 test_level |= G_LOG_FLAG_FATAL;
999 test_level |= G_LOG_FLAG_RECURSION;
1001 /* check recursion and lookup handler */
1002 g_mutex_lock (&g_messages_lock);
1003 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
1004 domain = g_log_find_domain_L (log_domain ? log_domain : "");
1006 test_level |= G_LOG_FLAG_RECURSION;
1008 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
1009 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
1010 test_level |= G_LOG_FLAG_FATAL;
1011 if (test_level & G_LOG_FLAG_RECURSION)
1012 log_func = _g_log_fallback_handler;
1014 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
1016 g_mutex_unlock (&g_messages_lock);
1018 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
1020 log_func (log_domain, test_level, msg, data);
1022 if ((test_level & G_LOG_FLAG_FATAL)
1023 && !(test_level & G_LOG_LEVEL_ERROR))
1025 masquerade_fatal = fatal_log_func
1026 && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
1029 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
1032 if (win32_keep_fatal_message)
1034 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
1036 MessageBox (NULL, locale_msg, NULL,
1037 MB_ICONERROR|MB_SETFOREGROUND);
1039 _g_log_abort (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION));
1041 _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
1042 #endif /* !G_OS_WIN32 */
1046 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
1055 * @log_domain: the log domain, usually #G_LOG_DOMAIN
1056 * @log_level: the log level, either from #GLogLevelFlags
1057 * or a user-defined level
1058 * @format: the message format. See the printf() documentation
1059 * @...: the parameters to insert into the format string
1061 * Logs an error or debugging message.
1063 * If the log level has been set as fatal, the abort()
1064 * function is called to terminate the program.
1066 * If g_log_default_handler() is used as the log handler function, a new-line
1067 * character will automatically be appended to @..., and need not be entered
1071 g_log (const gchar *log_domain,
1072 GLogLevelFlags log_level,
1073 const gchar *format,
1078 va_start (args, format);
1079 g_logv (log_domain, log_level, format, args);
1084 g_return_if_fail_warning (const char *log_domain,
1085 const char *pretty_function,
1086 const char *expression)
1089 G_LOG_LEVEL_CRITICAL,
1090 "%s: assertion '%s' failed",
1096 g_warn_message (const char *domain,
1100 const char *warnexpr)
1103 g_snprintf (lstr, 32, "%d", line);
1105 s = g_strconcat ("(", file, ":", lstr, "):",
1106 func, func[0] ? ":" : "",
1107 " runtime check failed: (", warnexpr, ")", NULL);
1109 s = g_strconcat ("(", file, ":", lstr, "):",
1110 func, func[0] ? ":" : "",
1111 " ", "code should not be reached", NULL);
1112 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
1117 g_assert_warning (const char *log_domain,
1120 const char *pretty_function,
1121 const char *expression)
1126 ? "file %s: line %d (%s): assertion failed: (%s)"
1127 : "file %s: line %d (%s): should not be reached",
1132 _g_log_abort (FALSE);
1136 * g_test_expect_message:
1137 * @log_domain: (allow-none): the log domain of the message
1138 * @log_level: the log level of the message
1139 * @pattern: a glob-style
1140 * <link linkend="glib-Glob-style-pattern-matching">pattern</link>
1142 * Indicates that a message with the given @log_domain and @log_level,
1143 * with text matching @pattern, is expected to be logged. When this
1144 * message is logged, it will not be printed, and the test case will
1147 * Use g_test_assert_expected_messages() to assert that all
1148 * previously-expected messages have been seen and suppressed.
1150 * You can call this multiple times in a row, if multiple messages are
1151 * expected as a result of a single call. (The messages must appear in
1152 * the same order as the calls to g_test_expect_message().)
1157 * /* g_main_context_push_thread_default() should fail if the
1158 * * context is already owned by another thread.
1160 * g_test_expect_message (G_LOG_DOMAIN,
1161 * G_LOG_LEVEL_CRITICAL,
1162 * "assertion*acquired_context*failed");
1163 * g_main_context_push_thread_default (bad_context);
1164 * g_test_assert_expected_messages ();
1167 * Note that you cannot use this to test g_error() messages, since
1168 * g_error() intentionally never returns even if the program doesn't
1169 * abort; use g_test_trap_subprocess() in this case.
1171 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1172 * expected via g_test_expect_message() then they will be ignored.
1177 g_test_expect_message (const gchar *log_domain,
1178 GLogLevelFlags log_level,
1179 const gchar *pattern)
1181 GTestExpectedMessage *expected;
1183 g_return_if_fail (log_level != 0);
1184 g_return_if_fail (pattern != NULL);
1185 g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR);
1187 expected = g_new (GTestExpectedMessage, 1);
1188 expected->log_domain = g_strdup (log_domain);
1189 expected->log_level = log_level;
1190 expected->pattern = g_strdup (pattern);
1192 expected_messages = g_slist_append (expected_messages, expected);
1196 g_test_assert_expected_messages_internal (const char *domain,
1201 if (expected_messages)
1203 GTestExpectedMessage *expected;
1204 gchar level_prefix[STRING_BUFFER_SIZE];
1207 expected = expected_messages->data;
1209 mklevel_prefix (level_prefix, expected->log_level);
1210 message = g_strdup_printf ("Did not see expected message %s: %s",
1211 level_prefix, expected->pattern);
1212 g_assertion_message (domain, file, line, func, message);
1218 * g_test_assert_expected_messages:
1220 * Asserts that all messages previously indicated via
1221 * g_test_expect_message() have been seen and suppressed.
1223 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1224 * expected via g_test_expect_message() then they will be ignored.
1230 _g_log_fallback_handler (const gchar *log_domain,
1231 GLogLevelFlags log_level,
1232 const gchar *message,
1233 gpointer unused_data)
1235 gchar level_prefix[STRING_BUFFER_SIZE];
1237 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1241 /* we cannot call _any_ GLib functions in this fallback handler,
1242 * which is why we skip UTF-8 conversion, etc.
1243 * since we either recursed or ran out of memory, we're in a pretty
1244 * pathologic situation anyways, what we can do is giving the
1245 * the process ID unconditionally however.
1248 fd = mklevel_prefix (level_prefix, log_level);
1250 message = "(NULL) message";
1253 format_unsigned (pid_string, getpid (), 10);
1257 write_string (fd, "\n");
1259 write_string (fd, "\n** ");
1262 write_string (fd, "(process:");
1263 write_string (fd, pid_string);
1264 write_string (fd, "): ");
1269 write_string (fd, log_domain);
1270 write_string (fd, "-");
1272 write_string (fd, level_prefix);
1273 write_string (fd, ": ");
1274 write_string (fd, message);
1278 escape_string (GString *string)
1280 const char *p = string->str;
1283 while (p < string->str + string->len)
1287 wc = g_utf8_get_char_validated (p, -1);
1288 if (wc == (gunichar)-1 || wc == (gunichar)-2)
1293 pos = p - string->str;
1295 /* Emit invalid UTF-8 as hex escapes
1297 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
1298 g_string_erase (string, pos, 1);
1299 g_string_insert (string, pos, tmp);
1301 p = string->str + (pos + 4); /* Skip over escape sequence */
1308 safe = *(p + 1) == '\n';
1312 safe = CHAR_IS_SAFE (wc);
1320 pos = p - string->str;
1322 /* Largest char we escape is 0x0a, so we don't have to worry
1323 * about 8-digit \Uxxxxyyyy
1325 tmp = g_strdup_printf ("\\u%04x", wc);
1326 g_string_erase (string, pos, g_utf8_next_char (p) - p);
1327 g_string_insert (string, pos, tmp);
1330 p = string->str + (pos + 6); /* Skip over escape sequence */
1333 p = g_utf8_next_char (p);
1338 * g_log_default_handler:
1339 * @log_domain: the log domain of the message
1340 * @log_level: the level of the message
1341 * @message: the message
1342 * @unused_data: data passed from g_log() which is unused
1344 * The default log handler set up by GLib; g_log_set_default_handler()
1345 * allows to install an alternate default log handler.
1346 * This is used if no log handler has been set for the particular log
1347 * domain and log level combination. It outputs the message to stderr
1348 * or stdout and if the log level is fatal it calls abort(). It automatically
1349 * prints a new-line character after the message, so one does not need to be
1350 * manually included in @message.
1352 * The behavior of this log handler can be influenced by a number of
1353 * environment variables:
1356 * <term><envar>G_MESSAGES_PREFIXED</envar></term>
1358 * A :-separated list of log levels for which messages should
1359 * be prefixed by the program name and PID of the aplication.
1363 * <term><envar>G_MESSAGES_DEBUG</envar></term>
1365 * A space-separated list of log domains for which debug and
1366 * informational messages are printed. By default these
1367 * messages are not printed.
1372 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1373 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1377 g_log_default_handler (const gchar *log_domain,
1378 GLogLevelFlags log_level,
1379 const gchar *message,
1380 gpointer unused_data)
1382 gchar level_prefix[STRING_BUFFER_SIZE], *string;
1385 const gchar *domains;
1387 if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
1390 domains = g_getenv ("G_MESSAGES_DEBUG");
1391 if (((log_level & INFO_LEVELS) == 0) ||
1393 (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
1397 /* we can be called externally with recursion for whatever reason */
1398 if (log_level & G_LOG_FLAG_RECURSION)
1400 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1404 fd = mklevel_prefix (level_prefix, log_level);
1406 gstring = g_string_new (NULL);
1407 if (log_level & ALERT_LEVELS)
1408 g_string_append (gstring, "\n");
1410 g_string_append (gstring, "** ");
1412 if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
1414 const gchar *prg_name = g_get_prgname ();
1417 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1419 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1424 g_string_append (gstring, log_domain);
1425 g_string_append_c (gstring, '-');
1427 g_string_append (gstring, level_prefix);
1429 g_string_append (gstring, ": ");
1431 g_string_append (gstring, "(NULL) message");
1435 const gchar *charset;
1437 msg = g_string_new (message);
1438 escape_string (msg);
1440 if (g_get_charset (&charset))
1441 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1444 string = strdup_convert (msg->str, charset);
1445 g_string_append (gstring, string);
1449 g_string_free (msg, TRUE);
1451 g_string_append (gstring, "\n");
1453 string = g_string_free (gstring, FALSE);
1455 write_string (fd, string);
1460 * g_set_print_handler:
1461 * @func: the new print handler
1463 * Sets the print handler.
1465 * Any messages passed to g_print() will be output via
1466 * the new handler. The default handler simply outputs
1467 * the message to stdout. By providing your own handler
1468 * you can redirect the output, to a GTK+ widget or a
1469 * log file for example.
1471 * Returns: the old print handler
1474 g_set_print_handler (GPrintFunc func)
1476 GPrintFunc old_print_func;
1478 g_mutex_lock (&g_messages_lock);
1479 old_print_func = glib_print_func;
1480 glib_print_func = func;
1481 g_mutex_unlock (&g_messages_lock);
1483 return old_print_func;
1488 * @format: the message format. See the printf() documentation
1489 * @...: the parameters to insert into the format string
1491 * Outputs a formatted message via the print handler.
1492 * The default print handler simply outputs the message to stdout, without
1493 * appending a trailing new-line character. Typically, @format should end with
1494 * its own new-line character.
1496 * g_print() should not be used from within libraries for debugging
1497 * messages, since it may be redirected by applications to special
1498 * purpose message windows or even files. Instead, libraries should
1499 * use g_log(), or the convenience functions g_message(), g_warning()
1503 g_print (const gchar *format,
1508 GPrintFunc local_glib_print_func;
1510 g_return_if_fail (format != NULL);
1512 va_start (args, format);
1513 string = g_strdup_vprintf (format, args);
1516 g_mutex_lock (&g_messages_lock);
1517 local_glib_print_func = glib_print_func;
1518 g_mutex_unlock (&g_messages_lock);
1520 if (local_glib_print_func)
1521 local_glib_print_func (string);
1524 const gchar *charset;
1526 if (g_get_charset (&charset))
1527 fputs (string, stdout); /* charset is UTF-8 already */
1530 gchar *lstring = strdup_convert (string, charset);
1532 fputs (lstring, stdout);
1541 * g_set_printerr_handler:
1542 * @func: the new error message handler
1544 * Sets the handler for printing error messages.
1546 * Any messages passed to g_printerr() will be output via
1547 * the new handler. The default handler simply outputs the
1548 * message to stderr. By providing your own handler you can
1549 * redirect the output, to a GTK+ widget or a log file for
1552 * Returns: the old error message handler
1555 g_set_printerr_handler (GPrintFunc func)
1557 GPrintFunc old_printerr_func;
1559 g_mutex_lock (&g_messages_lock);
1560 old_printerr_func = glib_printerr_func;
1561 glib_printerr_func = func;
1562 g_mutex_unlock (&g_messages_lock);
1564 return old_printerr_func;
1569 * @format: the message format. See the printf() documentation
1570 * @...: the parameters to insert into the format string
1572 * Outputs a formatted message via the error message handler.
1573 * The default handler simply outputs the message to stderr, without appending
1574 * a trailing new-line character. Typically, @format should end with its own
1575 * new-line character.
1577 * g_printerr() should not be used from within libraries.
1578 * Instead g_log() should be used, or the convenience functions
1579 * g_message(), g_warning() and g_error().
1582 g_printerr (const gchar *format,
1587 GPrintFunc local_glib_printerr_func;
1589 g_return_if_fail (format != NULL);
1591 va_start (args, format);
1592 string = g_strdup_vprintf (format, args);
1595 g_mutex_lock (&g_messages_lock);
1596 local_glib_printerr_func = glib_printerr_func;
1597 g_mutex_unlock (&g_messages_lock);
1599 if (local_glib_printerr_func)
1600 local_glib_printerr_func (string);
1603 const gchar *charset;
1605 if (g_get_charset (&charset))
1606 fputs (string, stderr); /* charset is UTF-8 already */
1609 gchar *lstring = strdup_convert (string, charset);
1611 fputs (lstring, stderr);
1620 * g_printf_string_upper_bound:
1621 * @format: the format string. See the printf() documentation
1622 * @args: the parameters to be inserted into the format string
1624 * Calculates the maximum space needed to store the output
1625 * of the sprintf() function.
1627 * Returns: the maximum space needed to store the formatted string
1630 g_printf_string_upper_bound (const gchar *format,
1634 return _g_vsnprintf (&c, 1, format, args) + 1;