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, see <http://www.gnu.org/licenses/>.
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/.
31 * @Title: Message Output and Debugging Functions
32 * @Short_description: functions to output messages and help debug applications
34 * These functions provide support for outputting messages.
36 * The <function>g_return</function> family of macros (g_return_if_fail(),
37 * g_return_val_if_fail(), g_return_if_reached(), g_return_val_if_reached())
38 * should only be used for programming errors, a typical use case is
39 * checking for invalid parameters at the beginning of a public function.
40 * They should not be used if you just mean "if (error) return", they
41 * should only be used if you mean "if (bug in program) return".
42 * The program behavior is generally considered undefined after one
43 * of these checks fails. They are not intended for normal control
44 * flow, only to give a perhaps-helpful warning before giving up.
57 #include "glib-init.h"
58 #include "gbacktrace.h"
63 #include "gprintfint.h"
64 #include "gtestutils.h"
66 #include "gstrfuncs.h"
75 #include <process.h> /* For getpid() */
77 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
84 * @title: Message Logging
85 * @short_description: versatile support for logging messages
86 * with different levels of importance
88 * These functions provide support for logging error messages
89 * or messages used for debugging.
91 * There are several built-in levels of messages, defined in
92 * #GLogLevelFlags. These can be extended with user-defined levels.
98 * Defines the log domain.
100 * For applications, this is typically left as the default %NULL
101 * (or "") domain. Libraries should define this so that any messages
102 * which they log can be differentiated from messages from other
103 * libraries and application code. But be careful not to define
104 * it in any public header files.
106 * For example, GTK+ uses this in its Makefile.am:
108 * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
115 * GLib log levels that are considered fatal by default.
120 * @log_domain: the log domain of the message
121 * @log_level: the log level of the message (including the
122 * fatal and recursion flags)
123 * @message: the message to process
124 * @user_data: user data, set in g_log_set_handler()
126 * Specifies the prototype of log handler functions.
128 * The default log handler, g_log_default_handler(), automatically appends a
129 * new-line character to @message when printing it. It is advised that any
130 * custom log handler functions behave similarly, so that logging calls in user
131 * code do not need modifying to add a new-line character to the message if the
132 * log handler is changed.
137 * @G_LOG_FLAG_RECURSION: internal flag
138 * @G_LOG_FLAG_FATAL: internal flag
139 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
140 * This level is also used for messages produced by g_assert().
141 * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
142 * This level is also used for messages produced by g_return_if_fail()
143 * and g_return_val_if_fail().
144 * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
145 * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
146 * @G_LOG_LEVEL_INFO: log level for informational messages, see g_info()
147 * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
148 * @G_LOG_LEVEL_MASK: a mask including all log levels
150 * Flags specifying the level of log messages.
152 * It is possible to change how GLib treats messages of the various
153 * levels using g_log_set_handler() and g_log_set_fatal_mask().
158 * @...: format string, followed by parameters to insert
159 * into the format string (as with printf())
161 * A convenience function/macro to log a normal message.
163 * If g_log_default_handler() is used as the log handler function, a new-line
164 * character will automatically be appended to @..., and need not be entered
170 * @...: format string, followed by parameters to insert
171 * into the format string (as with printf())
173 * A convenience function/macro to log a warning message.
175 * You can make warnings fatal at runtime by setting the
176 * <envar>G_DEBUG</envar> environment variable (see
177 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
179 * If g_log_default_handler() is used as the log handler function, a new-line
180 * character will automatically be appended to @..., and need not be entered
186 * @...: format string, followed by parameters to insert
187 * into the format string (as with printf())
189 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
190 * It's more or less application-defined what constitutes
191 * a critical vs. a regular warning. You could call
192 * g_log_set_always_fatal() to make critical warnings exit
193 * the program, then use g_critical() for fatal errors, for
196 * You can also make critical warnings fatal at runtime by
197 * setting the <envar>G_DEBUG</envar> environment variable (see
198 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
200 * If g_log_default_handler() is used as the log handler function, a new-line
201 * character will automatically be appended to @..., and need not be entered
207 * @...: format string, followed by parameters to insert
208 * into the format string (as with printf())
210 * A convenience function/macro to log an error message.
212 * Error messages are always fatal, resulting in a call to
213 * abort() to terminate the application. This function will
214 * result in a core dump; don't use it for errors you expect.
215 * Using this function indicates a bug in your program, i.e.
216 * an assertion failure.
218 * If g_log_default_handler() is used as the log handler function, a new-line
219 * character will automatically be appended to @..., and need not be entered
226 * @...: format string, followed by parameters to insert
227 * into the format string (as with printf())
229 * A convenience function/macro to log an informational message. Seldom used.
231 * If g_log_default_handler() is used as the log handler function, a new-line
232 * character will automatically be appended to @..., and need not be entered
235 * Such messages are suppressed by the g_log_default_handler() unless
236 * the G_MESSAGES_DEBUG environment variable is set appropriately.
243 * @...: format string, followed by parameters to insert
244 * into the format string (as with printf())
246 * A convenience function/macro to log a debug message.
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
252 * Such messages are suppressed by the g_log_default_handler() unless
253 * the G_MESSAGES_DEBUG environment variable is set appropriately.
258 /* --- structures --- */
259 typedef struct _GLogDomain GLogDomain;
260 typedef struct _GLogHandler GLogHandler;
264 GLogLevelFlags fatal_mask;
265 GLogHandler *handlers;
271 GLogLevelFlags log_level;
278 /* --- variables --- */
279 static GMutex g_messages_lock;
280 static GLogDomain *g_log_domains = NULL;
281 static GPrintFunc glib_print_func = NULL;
282 static GPrintFunc glib_printerr_func = NULL;
283 static GPrivate g_log_depth;
284 static GLogFunc default_log_func = g_log_default_handler;
285 static gpointer default_log_data = NULL;
286 static GTestLogFatalFunc fatal_log_func = NULL;
287 static gpointer fatal_log_data;
289 /* --- functions --- */
291 static void _g_log_abort (gboolean breakpoint);
294 _g_log_abort (gboolean breakpoint)
296 if (g_test_subprocess ())
298 /* If this is a test case subprocess then it probably caused
299 * this error message on purpose, so just exit() rather than
300 * abort()ing, to avoid triggering any system crash-reporting
313 # include <windows.h>
314 static gboolean win32_keep_fatal_message = FALSE;
316 /* This default message will usually be overwritten. */
317 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
318 * called with huge strings, is it?
320 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
321 static gchar *fatal_msg_ptr = fatal_msg_buf;
329 if (win32_keep_fatal_message)
331 memcpy (fatal_msg_ptr, buf, len);
332 fatal_msg_ptr += len;
337 write (fd, buf, len);
341 #define write(fd, buf, len) dowrite(fd, buf, len)
346 write_string (int fd,
351 res = write (fd, string, strlen (string));
352 while (G_UNLIKELY (res == -1 && errno == EINTR));
356 g_log_find_domain_L (const gchar *log_domain)
358 register GLogDomain *domain;
360 domain = g_log_domains;
363 if (strcmp (domain->log_domain, log_domain) == 0)
365 domain = domain->next;
371 g_log_domain_new_L (const gchar *log_domain)
373 register GLogDomain *domain;
375 domain = g_new (GLogDomain, 1);
376 domain->log_domain = g_strdup (log_domain);
377 domain->fatal_mask = G_LOG_FATAL_MASK;
378 domain->handlers = NULL;
380 domain->next = g_log_domains;
381 g_log_domains = domain;
387 g_log_domain_check_free_L (GLogDomain *domain)
389 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
390 domain->handlers == NULL)
392 register GLogDomain *last, *work;
396 work = g_log_domains;
402 last->next = domain->next;
404 g_log_domains = domain->next;
405 g_free (domain->log_domain);
416 g_log_domain_get_handler_L (GLogDomain *domain,
417 GLogLevelFlags log_level,
420 if (domain && log_level)
422 register GLogHandler *handler;
424 handler = domain->handlers;
427 if ((handler->log_level & log_level) == log_level)
429 *data = handler->data;
430 return handler->log_func;
432 handler = handler->next;
436 *data = default_log_data;
437 return default_log_func;
441 * g_log_set_always_fatal:
442 * @fatal_mask: the mask containing bits set for each level
443 * of error which is to be fatal
445 * Sets the message levels which are always fatal, in any log domain.
446 * When a message with any of these levels is logged the program terminates.
447 * You can only set the levels defined by GLib to be fatal.
448 * %G_LOG_LEVEL_ERROR is always fatal.
450 * You can also make some message levels fatal at runtime by setting
451 * the <envar>G_DEBUG</envar> environment variable (see
452 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
454 * Returns: the old fatal mask
457 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
459 GLogLevelFlags old_mask;
461 /* restrict the global mask to levels that are known to glib
462 * since this setting applies to all domains
464 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
465 /* force errors to be fatal */
466 fatal_mask |= G_LOG_LEVEL_ERROR;
467 /* remove bogus flag */
468 fatal_mask &= ~G_LOG_FLAG_FATAL;
470 g_mutex_lock (&g_messages_lock);
471 old_mask = g_log_always_fatal;
472 g_log_always_fatal = fatal_mask;
473 g_mutex_unlock (&g_messages_lock);
479 * g_log_set_fatal_mask:
480 * @log_domain: the log domain
481 * @fatal_mask: the new fatal mask
483 * Sets the log levels which are fatal in the given domain.
484 * %G_LOG_LEVEL_ERROR is always fatal.
486 * Returns: the old fatal mask for the log domain
489 g_log_set_fatal_mask (const gchar *log_domain,
490 GLogLevelFlags fatal_mask)
492 GLogLevelFlags old_flags;
493 register GLogDomain *domain;
498 /* force errors to be fatal */
499 fatal_mask |= G_LOG_LEVEL_ERROR;
500 /* remove bogus flag */
501 fatal_mask &= ~G_LOG_FLAG_FATAL;
503 g_mutex_lock (&g_messages_lock);
505 domain = g_log_find_domain_L (log_domain);
507 domain = g_log_domain_new_L (log_domain);
508 old_flags = domain->fatal_mask;
510 domain->fatal_mask = fatal_mask;
511 g_log_domain_check_free_L (domain);
513 g_mutex_unlock (&g_messages_lock);
520 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
522 * @log_levels: the log levels to apply the log handler for.
523 * To handle fatal and recursive messages as well, combine
524 * the log levels with the #G_LOG_FLAG_FATAL and
525 * #G_LOG_FLAG_RECURSION bit flags.
526 * @log_func: the log handler function
527 * @user_data: data passed to the log handler
529 * Sets the log handler for a domain and a set of log levels.
530 * To handle fatal and recursive messages the @log_levels parameter
531 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
534 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
535 * you want to set a handler for this log level you must combine it with
539 * <title>Adding a log handler for all warning messages in the default
540 * (application) domain</title>
542 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
543 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
548 * <title>Adding a log handler for all critical messages from GTK+</title>
550 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
551 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
556 * <title>Adding a log handler for <emphasis>all</emphasis> messages from
559 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
560 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
564 * Returns: the id of the new handler
567 g_log_set_handler (const gchar *log_domain,
568 GLogLevelFlags log_levels,
572 static guint handler_id = 0;
574 GLogHandler *handler;
576 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
577 g_return_val_if_fail (log_func != NULL, 0);
582 handler = g_new (GLogHandler, 1);
584 g_mutex_lock (&g_messages_lock);
586 domain = g_log_find_domain_L (log_domain);
588 domain = g_log_domain_new_L (log_domain);
590 handler->id = ++handler_id;
591 handler->log_level = log_levels;
592 handler->log_func = log_func;
593 handler->data = user_data;
594 handler->next = domain->handlers;
595 domain->handlers = handler;
597 g_mutex_unlock (&g_messages_lock);
603 * g_log_set_default_handler:
604 * @log_func: the log handler function
605 * @user_data: data passed to the log handler
607 * Installs a default log handler which is used if no
608 * log handler has been set for the particular log domain
609 * and log level combination. By default, GLib uses
610 * g_log_default_handler() as default log handler.
612 * Returns: the previous default log handler
617 g_log_set_default_handler (GLogFunc log_func,
620 GLogFunc old_log_func;
622 g_mutex_lock (&g_messages_lock);
623 old_log_func = default_log_func;
624 default_log_func = log_func;
625 default_log_data = user_data;
626 g_mutex_unlock (&g_messages_lock);
632 * g_test_log_set_fatal_handler:
633 * @log_func: the log handler function.
634 * @user_data: data passed to the log handler.
636 * Installs a non-error fatal log handler which can be
637 * used to decide whether log messages which are counted
638 * as fatal abort the program.
640 * The use case here is that you are running a test case
641 * that depends on particular libraries or circumstances
642 * and cannot prevent certain known critical or warning
643 * messages. So you install a handler that compares the
644 * domain and message to precisely not abort in such a case.
646 * Note that the handler is reset at the beginning of
647 * any test case, so you have to set it inside each test
648 * function which needs the special behavior.
650 * This handler has no effect on g_error messages.
655 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
658 g_mutex_lock (&g_messages_lock);
659 fatal_log_func = log_func;
660 fatal_log_data = user_data;
661 g_mutex_unlock (&g_messages_lock);
665 * g_log_remove_handler:
666 * @log_domain: the log domain
667 * @handler_id: the id of the handler, which was returned
668 * in g_log_set_handler()
670 * Removes the log handler.
673 g_log_remove_handler (const gchar *log_domain,
676 register GLogDomain *domain;
678 g_return_if_fail (handler_id > 0);
683 g_mutex_lock (&g_messages_lock);
684 domain = g_log_find_domain_L (log_domain);
687 GLogHandler *work, *last;
690 work = domain->handlers;
693 if (work->id == handler_id)
696 last->next = work->next;
698 domain->handlers = work->next;
699 g_log_domain_check_free_L (domain);
700 g_mutex_unlock (&g_messages_lock);
708 g_mutex_unlock (&g_messages_lock);
709 g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
710 G_STRLOC, handler_id, log_domain);
713 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
715 (wc >= 0x80 && wc < 0xa0)))
718 strdup_convert (const gchar *string,
719 const gchar *charset)
721 if (!g_utf8_validate (string, -1, NULL))
723 GString *gstring = g_string_new ("[Invalid UTF-8] ");
726 for (p = (guchar *)string; *p; p++)
728 if (CHAR_IS_SAFE(*p) &&
729 !(*p == '\r' && *(p + 1) != '\n') &&
731 g_string_append_c (gstring, *p);
733 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
736 return g_string_free (gstring, FALSE);
742 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
747 /* Not thread-safe, but doesn't matter if we print the warning twice
749 static gboolean warned = FALSE;
753 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
757 return g_strdup (string);
762 /* For a radix of 8 we need at most 3 output bytes for 1 input
763 * byte. Additionally we might need up to 2 output bytes for the
764 * readix prefix and 1 byte for the trailing NULL.
766 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
769 format_unsigned (gchar *buf,
777 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
779 if (radix != 8 && radix != 10 && radix != 16)
812 /* Again we can't use g_assert; actually this check should _never_ fail. */
813 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
826 buf[i] = c + 'a' - 10;
833 /* string size big enough to hold level prefix */
834 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
836 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
838 /* these are emitted by the default log handler */
839 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
840 /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
841 #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
844 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
845 GLogLevelFlags log_level)
847 gboolean to_stdout = TRUE;
849 /* we may not call _any_ GLib functions here */
851 switch (log_level & G_LOG_LEVEL_MASK)
853 case G_LOG_LEVEL_ERROR:
854 strcpy (level_prefix, "ERROR");
857 case G_LOG_LEVEL_CRITICAL:
858 strcpy (level_prefix, "CRITICAL");
861 case G_LOG_LEVEL_WARNING:
862 strcpy (level_prefix, "WARNING");
865 case G_LOG_LEVEL_MESSAGE:
866 strcpy (level_prefix, "Message");
869 case G_LOG_LEVEL_INFO:
870 strcpy (level_prefix, "INFO");
872 case G_LOG_LEVEL_DEBUG:
873 strcpy (level_prefix, "DEBUG");
878 strcpy (level_prefix, "LOG-");
879 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
882 strcpy (level_prefix, "LOG");
885 if (log_level & G_LOG_FLAG_RECURSION)
886 strcat (level_prefix, " (recursed)");
887 if (log_level & ALERT_LEVELS)
888 strcat (level_prefix, " **");
891 if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
892 win32_keep_fatal_message = TRUE;
894 return to_stdout ? 1 : 2;
899 GLogLevelFlags log_level;
901 } GTestExpectedMessage;
903 static GSList *expected_messages = NULL;
907 * @log_domain: the log domain
908 * @log_level: the log level
909 * @format: the message format. See the printf() documentation
910 * @args: the parameters to insert into the format string
912 * Logs an error or debugging message.
914 * If the log level has been set as fatal, the abort()
915 * function is called to terminate the program.
917 * If g_log_default_handler() is used as the log handler function, a new-line
918 * character will automatically be appended to @..., and need not be entered
922 g_logv (const gchar *log_domain,
923 GLogLevelFlags log_level,
927 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
928 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
929 gchar buffer[1025], *msg, *msg_alloc = NULL;
932 log_level &= G_LOG_LEVEL_MASK;
936 if (log_level & G_LOG_FLAG_RECURSION)
938 /* we use a stack buffer of fixed size, since we're likely
939 * in an out-of-memory situation
941 gsize size G_GNUC_UNUSED;
943 size = _g_vsnprintf (buffer, 1024, format, args);
947 msg = msg_alloc = g_strdup_vprintf (format, args);
949 if (expected_messages)
951 GTestExpectedMessage *expected = expected_messages->data;
953 if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
954 ((log_level & expected->log_level) == expected->log_level) &&
955 g_pattern_match_simple (expected->pattern, msg))
957 expected_messages = g_slist_delete_link (expected_messages,
959 g_free (expected->log_domain);
960 g_free (expected->pattern);
965 else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
967 gchar level_prefix[STRING_BUFFER_SIZE];
968 gchar *expected_message;
970 mklevel_prefix (level_prefix, expected->log_level);
971 expected_message = g_strdup_printf ("Did not see expected message %s: %s",
972 level_prefix, expected->pattern);
973 g_log_default_handler (log_domain, log_level, expected_message, NULL);
974 g_free (expected_message);
976 log_level |= G_LOG_FLAG_FATAL;
980 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
982 register GLogLevelFlags test_level;
985 if (log_level & test_level)
989 GLogLevelFlags domain_fatal_mask;
990 gpointer data = NULL;
991 gboolean masquerade_fatal = FALSE;
995 test_level |= G_LOG_FLAG_FATAL;
997 test_level |= G_LOG_FLAG_RECURSION;
999 /* check recursion and lookup handler */
1000 g_mutex_lock (&g_messages_lock);
1001 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
1002 domain = g_log_find_domain_L (log_domain ? log_domain : "");
1004 test_level |= G_LOG_FLAG_RECURSION;
1006 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
1007 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
1008 test_level |= G_LOG_FLAG_FATAL;
1009 if (test_level & G_LOG_FLAG_RECURSION)
1010 log_func = _g_log_fallback_handler;
1012 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
1014 g_mutex_unlock (&g_messages_lock);
1016 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
1018 log_func (log_domain, test_level, msg, data);
1020 if ((test_level & G_LOG_FLAG_FATAL)
1021 && !(test_level & G_LOG_LEVEL_ERROR))
1023 masquerade_fatal = fatal_log_func
1024 && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
1027 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
1030 if (win32_keep_fatal_message)
1032 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
1034 MessageBox (NULL, locale_msg, NULL,
1035 MB_ICONERROR|MB_SETFOREGROUND);
1037 _g_log_abort (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION));
1039 _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
1040 #endif /* !G_OS_WIN32 */
1044 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
1053 * @log_domain: the log domain, usually #G_LOG_DOMAIN
1054 * @log_level: the log level, either from #GLogLevelFlags
1055 * or a user-defined level
1056 * @format: the message format. See the printf() documentation
1057 * @...: the parameters to insert into the format string
1059 * Logs an error or debugging message.
1061 * If the log level has been set as fatal, the abort()
1062 * function is called to terminate the program.
1064 * If g_log_default_handler() is used as the log handler function, a new-line
1065 * character will automatically be appended to @..., and need not be entered
1069 g_log (const gchar *log_domain,
1070 GLogLevelFlags log_level,
1071 const gchar *format,
1076 va_start (args, format);
1077 g_logv (log_domain, log_level, format, args);
1082 g_return_if_fail_warning (const char *log_domain,
1083 const char *pretty_function,
1084 const char *expression)
1087 G_LOG_LEVEL_CRITICAL,
1088 "%s: assertion '%s' failed",
1094 g_warn_message (const char *domain,
1098 const char *warnexpr)
1101 g_snprintf (lstr, 32, "%d", line);
1103 s = g_strconcat ("(", file, ":", lstr, "):",
1104 func, func[0] ? ":" : "",
1105 " runtime check failed: (", warnexpr, ")", NULL);
1107 s = g_strconcat ("(", file, ":", lstr, "):",
1108 func, func[0] ? ":" : "",
1109 " ", "code should not be reached", NULL);
1110 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
1115 g_assert_warning (const char *log_domain,
1118 const char *pretty_function,
1119 const char *expression)
1124 ? "file %s: line %d (%s): assertion failed: (%s)"
1125 : "file %s: line %d (%s): should not be reached",
1130 _g_log_abort (FALSE);
1135 * g_test_expect_message:
1136 * @log_domain: (allow-none): the log domain of the message
1137 * @log_level: the log level of the message
1138 * @pattern: a glob-style
1139 * <link linkend="glib-Glob-style-pattern-matching">pattern</link>
1141 * Indicates that a message with the given @log_domain and @log_level,
1142 * with text matching @pattern, is expected to be logged. When this
1143 * message is logged, it will not be printed, and the test case will
1146 * Use g_test_assert_expected_messages() to assert that all
1147 * previously-expected messages have been seen and suppressed.
1149 * You can call this multiple times in a row, if multiple messages are
1150 * expected as a result of a single call. (The messages must appear in
1151 * the same order as the calls to g_test_expect_message().)
1156 * /* g_main_context_push_thread_default() should fail if the
1157 * * context is already owned by another thread.
1159 * g_test_expect_message (G_LOG_DOMAIN,
1160 * G_LOG_LEVEL_CRITICAL,
1161 * "assertion*acquired_context*failed");
1162 * g_main_context_push_thread_default (bad_context);
1163 * g_test_assert_expected_messages ();
1166 * Note that you cannot use this to test g_error() messages, since
1167 * g_error() intentionally never returns even if the program doesn't
1168 * abort; use g_test_trap_subprocess() in this case.
1170 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1171 * expected via g_test_expect_message() then they will be ignored.
1176 g_test_expect_message (const gchar *log_domain,
1177 GLogLevelFlags log_level,
1178 const gchar *pattern)
1180 GTestExpectedMessage *expected;
1182 g_return_if_fail (log_level != 0);
1183 g_return_if_fail (pattern != NULL);
1184 g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR);
1186 expected = g_new (GTestExpectedMessage, 1);
1187 expected->log_domain = g_strdup (log_domain);
1188 expected->log_level = log_level;
1189 expected->pattern = g_strdup (pattern);
1191 expected_messages = g_slist_append (expected_messages, expected);
1195 g_test_assert_expected_messages_internal (const char *domain,
1200 if (expected_messages)
1202 GTestExpectedMessage *expected;
1203 gchar level_prefix[STRING_BUFFER_SIZE];
1206 expected = expected_messages->data;
1208 mklevel_prefix (level_prefix, expected->log_level);
1209 message = g_strdup_printf ("Did not see expected message %s: %s",
1210 level_prefix, expected->pattern);
1211 g_assertion_message (domain, file, line, func, message);
1217 * g_test_assert_expected_messages:
1219 * Asserts that all messages previously indicated via
1220 * g_test_expect_message() have been seen and suppressed.
1222 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1223 * expected via g_test_expect_message() then they will be ignored.
1229 _g_log_fallback_handler (const gchar *log_domain,
1230 GLogLevelFlags log_level,
1231 const gchar *message,
1232 gpointer unused_data)
1234 gchar level_prefix[STRING_BUFFER_SIZE];
1236 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1240 /* we cannot call _any_ GLib functions in this fallback handler,
1241 * which is why we skip UTF-8 conversion, etc.
1242 * since we either recursed or ran out of memory, we're in a pretty
1243 * pathologic situation anyways, what we can do is giving the
1244 * the process ID unconditionally however.
1247 fd = mklevel_prefix (level_prefix, log_level);
1249 message = "(NULL) message";
1252 format_unsigned (pid_string, getpid (), 10);
1256 write_string (fd, "\n");
1258 write_string (fd, "\n** ");
1261 write_string (fd, "(process:");
1262 write_string (fd, pid_string);
1263 write_string (fd, "): ");
1268 write_string (fd, log_domain);
1269 write_string (fd, "-");
1271 write_string (fd, level_prefix);
1272 write_string (fd, ": ");
1273 write_string (fd, message);
1277 escape_string (GString *string)
1279 const char *p = string->str;
1282 while (p < string->str + string->len)
1286 wc = g_utf8_get_char_validated (p, -1);
1287 if (wc == (gunichar)-1 || wc == (gunichar)-2)
1292 pos = p - string->str;
1294 /* Emit invalid UTF-8 as hex escapes
1296 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
1297 g_string_erase (string, pos, 1);
1298 g_string_insert (string, pos, tmp);
1300 p = string->str + (pos + 4); /* Skip over escape sequence */
1307 safe = *(p + 1) == '\n';
1311 safe = CHAR_IS_SAFE (wc);
1319 pos = p - string->str;
1321 /* Largest char we escape is 0x0a, so we don't have to worry
1322 * about 8-digit \Uxxxxyyyy
1324 tmp = g_strdup_printf ("\\u%04x", wc);
1325 g_string_erase (string, pos, g_utf8_next_char (p) - p);
1326 g_string_insert (string, pos, tmp);
1329 p = string->str + (pos + 6); /* Skip over escape sequence */
1332 p = g_utf8_next_char (p);
1337 * g_log_default_handler:
1338 * @log_domain: the log domain of the message
1339 * @log_level: the level of the message
1340 * @message: the message
1341 * @unused_data: data passed from g_log() which is unused
1343 * The default log handler set up by GLib; g_log_set_default_handler()
1344 * allows to install an alternate default log handler.
1345 * This is used if no log handler has been set for the particular log
1346 * domain and log level combination. It outputs the message to stderr
1347 * or stdout and if the log level is fatal it calls abort(). It automatically
1348 * prints a new-line character after the message, so one does not need to be
1349 * manually included in @message.
1351 * The behavior of this log handler can be influenced by a number of
1352 * environment variables:
1355 * <term><envar>G_MESSAGES_PREFIXED</envar></term>
1357 * A :-separated list of log levels for which messages should
1358 * be prefixed by the program name and PID of the aplication.
1362 * <term><envar>G_MESSAGES_DEBUG</envar></term>
1364 * A space-separated list of log domains for which debug and
1365 * informational messages are printed. By default these
1366 * messages are not printed.
1371 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1372 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1376 g_log_default_handler (const gchar *log_domain,
1377 GLogLevelFlags log_level,
1378 const gchar *message,
1379 gpointer unused_data)
1381 gchar level_prefix[STRING_BUFFER_SIZE], *string;
1384 const gchar *domains;
1386 if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
1389 domains = g_getenv ("G_MESSAGES_DEBUG");
1390 if (((log_level & INFO_LEVELS) == 0) ||
1392 (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
1396 /* we can be called externally with recursion for whatever reason */
1397 if (log_level & G_LOG_FLAG_RECURSION)
1399 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1403 fd = mklevel_prefix (level_prefix, log_level);
1405 gstring = g_string_new (NULL);
1406 if (log_level & ALERT_LEVELS)
1407 g_string_append (gstring, "\n");
1409 g_string_append (gstring, "** ");
1411 if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
1413 const gchar *prg_name = g_get_prgname ();
1416 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1418 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1423 g_string_append (gstring, log_domain);
1424 g_string_append_c (gstring, '-');
1426 g_string_append (gstring, level_prefix);
1428 g_string_append (gstring, ": ");
1430 g_string_append (gstring, "(NULL) message");
1434 const gchar *charset;
1436 msg = g_string_new (message);
1437 escape_string (msg);
1439 if (g_get_charset (&charset))
1440 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1443 string = strdup_convert (msg->str, charset);
1444 g_string_append (gstring, string);
1448 g_string_free (msg, TRUE);
1450 g_string_append (gstring, "\n");
1452 string = g_string_free (gstring, FALSE);
1454 write_string (fd, string);
1459 * g_set_print_handler:
1460 * @func: the new print handler
1462 * Sets the print handler.
1464 * Any messages passed to g_print() will be output via
1465 * the new handler. The default handler simply outputs
1466 * the message to stdout. By providing your own handler
1467 * you can redirect the output, to a GTK+ widget or a
1468 * log file for example.
1470 * Returns: the old print handler
1473 g_set_print_handler (GPrintFunc func)
1475 GPrintFunc old_print_func;
1477 g_mutex_lock (&g_messages_lock);
1478 old_print_func = glib_print_func;
1479 glib_print_func = func;
1480 g_mutex_unlock (&g_messages_lock);
1482 return old_print_func;
1487 * @format: the message format. See the printf() documentation
1488 * @...: the parameters to insert into the format string
1490 * Outputs a formatted message via the print handler.
1491 * The default print handler simply outputs the message to stdout, without
1492 * appending a trailing new-line character. Typically, @format should end with
1493 * its own new-line character.
1495 * g_print() should not be used from within libraries for debugging
1496 * messages, since it may be redirected by applications to special
1497 * purpose message windows or even files. Instead, libraries should
1498 * use g_log(), or the convenience functions g_message(), g_warning()
1502 g_print (const gchar *format,
1507 GPrintFunc local_glib_print_func;
1509 g_return_if_fail (format != NULL);
1511 va_start (args, format);
1512 string = g_strdup_vprintf (format, args);
1515 g_mutex_lock (&g_messages_lock);
1516 local_glib_print_func = glib_print_func;
1517 g_mutex_unlock (&g_messages_lock);
1519 if (local_glib_print_func)
1520 local_glib_print_func (string);
1523 const gchar *charset;
1525 if (g_get_charset (&charset))
1526 fputs (string, stdout); /* charset is UTF-8 already */
1529 gchar *lstring = strdup_convert (string, charset);
1531 fputs (lstring, stdout);
1540 * g_set_printerr_handler:
1541 * @func: the new error message handler
1543 * Sets the handler for printing error messages.
1545 * Any messages passed to g_printerr() will be output via
1546 * the new handler. The default handler simply outputs the
1547 * message to stderr. By providing your own handler you can
1548 * redirect the output, to a GTK+ widget or a log file for
1551 * Returns: the old error message handler
1554 g_set_printerr_handler (GPrintFunc func)
1556 GPrintFunc old_printerr_func;
1558 g_mutex_lock (&g_messages_lock);
1559 old_printerr_func = glib_printerr_func;
1560 glib_printerr_func = func;
1561 g_mutex_unlock (&g_messages_lock);
1563 return old_printerr_func;
1568 * @format: the message format. See the printf() documentation
1569 * @...: the parameters to insert into the format string
1571 * Outputs a formatted message via the error message handler.
1572 * The default handler simply outputs the message to stderr, without appending
1573 * a trailing new-line character. Typically, @format should end with its own
1574 * new-line character.
1576 * g_printerr() should not be used from within libraries.
1577 * Instead g_log() should be used, or the convenience functions
1578 * g_message(), g_warning() and g_error().
1581 g_printerr (const gchar *format,
1586 GPrintFunc local_glib_printerr_func;
1588 g_return_if_fail (format != NULL);
1590 va_start (args, format);
1591 string = g_strdup_vprintf (format, args);
1594 g_mutex_lock (&g_messages_lock);
1595 local_glib_printerr_func = glib_printerr_func;
1596 g_mutex_unlock (&g_messages_lock);
1598 if (local_glib_printerr_func)
1599 local_glib_printerr_func (string);
1602 const gchar *charset;
1604 if (g_get_charset (&charset))
1605 fputs (string, stderr); /* charset is UTF-8 already */
1608 gchar *lstring = strdup_convert (string, charset);
1610 fputs (lstring, stderr);
1619 * g_printf_string_upper_bound:
1620 * @format: the format string. See the printf() documentation
1621 * @args: the parameters to be inserted into the format string
1623 * Calculates the maximum space needed to store the output
1624 * of the sprintf() function.
1626 * Returns: the maximum space needed to store the formatted string
1629 g_printf_string_upper_bound (const gchar *format,
1633 return _g_vsnprintf (&c, 1, format, args) + 1;