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.
62 #include "gmessages.h"
64 #include "glib-init.h"
65 #include "gbacktrace.h"
70 #include "gprintfint.h"
71 #include "gtestutils.h"
73 #include "gstrfuncs.h"
78 #include <process.h> /* For getpid() */
80 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
87 * @title: Message Logging
88 * @short_description: versatile support for logging messages
89 * with different levels of importance
91 * These functions provide support for logging error messages
92 * or messages used for debugging.
94 * There are several built-in levels of messages, defined in
95 * #GLogLevelFlags. These can be extended with user-defined levels.
101 * Defines the log domain.
103 * For applications, this is typically left as the default %NULL
104 * (or "") domain. Libraries should define this so that any messages
105 * which they log can be differentiated from messages from other
106 * libraries and application code. But be careful not to define
107 * it in any public header files.
109 * For example, GTK+ uses this in its Makefile.am:
111 * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
118 * GLib log levels that are considered fatal by default.
123 * @log_domain: the log domain of the message
124 * @log_level: the log level of the message (including the
125 * fatal and recursion flags)
126 * @message: the message to process
127 * @user_data: user data, set in g_log_set_handler()
129 * Specifies the prototype of log handler functions.
134 * @G_LOG_FLAG_RECURSION: internal flag
135 * @G_LOG_FLAG_FATAL: internal flag
136 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
137 * This level is also used for messages produced by g_assert().
138 * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
139 * This level is also used for messages produced by g_return_if_fail()
140 * and g_return_val_if_fail().
141 * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
142 * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
143 * @G_LOG_LEVEL_INFO: log level for informational messages
144 * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
145 * @G_LOG_LEVEL_MASK: a mask including all log levels
147 * Flags specifying the level of log messages.
149 * It is possible to change how GLib treats messages of the various
150 * levels using g_log_set_handler() and g_log_set_fatal_mask().
155 * @...: format string, followed by parameters to insert
156 * into the format string (as with printf())
158 * A convenience function/macro to log a normal message.
163 * @...: format string, followed by parameters to insert
164 * into the format string (as with printf())
166 * A convenience function/macro to log a warning message.
168 * You can make warnings fatal at runtime by setting the
169 * <envar>G_DEBUG</envar> environment variable (see
170 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
175 * @...: format string, followed by parameters to insert
176 * into the format string (as with printf())
178 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
179 * It's more or less application-defined what constitutes
180 * a critical vs. a regular warning. You could call
181 * g_log_set_always_fatal() to make critical warnings exit
182 * the program, then use g_critical() for fatal errors, for
185 * You can also make critical warnings fatal at runtime by
186 * setting the <envar>G_DEBUG</envar> environment variable (see
187 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
192 * @...: format string, followed by parameters to insert
193 * into the format string (as with printf())
195 * A convenience function/macro to log an error message.
197 * Error messages are always fatal, resulting in a call to
198 * abort() to terminate the application. This function will
199 * result in a core dump; don't use it for errors you expect.
200 * Using this function indicates a bug in your program, i.e.
201 * an assertion failure.
207 * @...: format string, followed by parameters to insert
208 * into the format string (as with printf())
210 * A convenience function/macro to log a debug message.
215 /* --- structures --- */
216 typedef struct _GLogDomain GLogDomain;
217 typedef struct _GLogHandler GLogHandler;
221 GLogLevelFlags fatal_mask;
222 GLogHandler *handlers;
228 GLogLevelFlags log_level;
235 /* --- variables --- */
236 static GMutex g_messages_lock;
237 static GLogDomain *g_log_domains = NULL;
238 static GPrintFunc glib_print_func = NULL;
239 static GPrintFunc glib_printerr_func = NULL;
240 static GPrivate g_log_depth;
241 static GLogFunc default_log_func = g_log_default_handler;
242 static gpointer default_log_data = NULL;
243 static GTestLogFatalFunc fatal_log_func = NULL;
244 static gpointer fatal_log_data;
246 /* --- functions --- */
248 # include <windows.h>
249 static gboolean win32_keep_fatal_message = FALSE;
251 /* This default message will usually be overwritten. */
252 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
253 * called with huge strings, is it?
255 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
256 static gchar *fatal_msg_ptr = fatal_msg_buf;
264 if (win32_keep_fatal_message)
266 memcpy (fatal_msg_ptr, buf, len);
267 fatal_msg_ptr += len;
272 write (fd, buf, len);
276 #define write(fd, buf, len) dowrite(fd, buf, len)
281 write_string (int fd,
284 write (fd, string, strlen (string));
288 g_log_find_domain_L (const gchar *log_domain)
290 register GLogDomain *domain;
292 domain = g_log_domains;
295 if (strcmp (domain->log_domain, log_domain) == 0)
297 domain = domain->next;
303 g_log_domain_new_L (const gchar *log_domain)
305 register GLogDomain *domain;
307 domain = g_new (GLogDomain, 1);
308 domain->log_domain = g_strdup (log_domain);
309 domain->fatal_mask = G_LOG_FATAL_MASK;
310 domain->handlers = NULL;
312 domain->next = g_log_domains;
313 g_log_domains = domain;
319 g_log_domain_check_free_L (GLogDomain *domain)
321 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
322 domain->handlers == NULL)
324 register GLogDomain *last, *work;
328 work = g_log_domains;
334 last->next = domain->next;
336 g_log_domains = domain->next;
337 g_free (domain->log_domain);
348 g_log_domain_get_handler_L (GLogDomain *domain,
349 GLogLevelFlags log_level,
352 if (domain && log_level)
354 register GLogHandler *handler;
356 handler = domain->handlers;
359 if ((handler->log_level & log_level) == log_level)
361 *data = handler->data;
362 return handler->log_func;
364 handler = handler->next;
368 *data = default_log_data;
369 return default_log_func;
373 * g_log_set_always_fatal:
374 * @fatal_mask: the mask containing bits set for each level
375 * of error which is to be fatal
377 * Sets the message levels which are always fatal, in any log domain.
378 * When a message with any of these levels is logged the program terminates.
379 * You can only set the levels defined by GLib to be fatal.
380 * %G_LOG_LEVEL_ERROR is always fatal.
382 * You can also make some message levels fatal at runtime by setting
383 * the <envar>G_DEBUG</envar> environment variable (see
384 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
386 * Returns: the old fatal mask
389 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
391 GLogLevelFlags old_mask;
393 /* restrict the global mask to levels that are known to glib
394 * since this setting applies to all domains
396 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
397 /* force errors to be fatal */
398 fatal_mask |= G_LOG_LEVEL_ERROR;
399 /* remove bogus flag */
400 fatal_mask &= ~G_LOG_FLAG_FATAL;
402 g_mutex_lock (&g_messages_lock);
403 old_mask = g_log_always_fatal;
404 g_log_always_fatal = fatal_mask;
405 g_mutex_unlock (&g_messages_lock);
411 * g_log_set_fatal_mask:
412 * @log_domain: the log domain
413 * @fatal_mask: the new fatal mask
415 * Sets the log levels which are fatal in the given domain.
416 * %G_LOG_LEVEL_ERROR is always fatal.
418 * Returns: the old fatal mask for the log domain
421 g_log_set_fatal_mask (const gchar *log_domain,
422 GLogLevelFlags fatal_mask)
424 GLogLevelFlags old_flags;
425 register GLogDomain *domain;
430 /* force errors to be fatal */
431 fatal_mask |= G_LOG_LEVEL_ERROR;
432 /* remove bogus flag */
433 fatal_mask &= ~G_LOG_FLAG_FATAL;
435 g_mutex_lock (&g_messages_lock);
437 domain = g_log_find_domain_L (log_domain);
439 domain = g_log_domain_new_L (log_domain);
440 old_flags = domain->fatal_mask;
442 domain->fatal_mask = fatal_mask;
443 g_log_domain_check_free_L (domain);
445 g_mutex_unlock (&g_messages_lock);
452 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
454 * @log_levels: the log levels to apply the log handler for.
455 * To handle fatal and recursive messages as well, combine
456 * the log levels with the #G_LOG_FLAG_FATAL and
457 * #G_LOG_FLAG_RECURSION bit flags.
458 * @log_func: the log handler function
459 * @user_data: data passed to the log handler
461 * Sets the log handler for a domain and a set of log levels.
462 * To handle fatal and recursive messages the @log_levels parameter
463 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
466 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
467 * you want to set a handler for this log level you must combine it with
471 * <title>Adding a log handler for all warning messages in the default
472 * (application) domain</title>
474 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
475 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
480 * <title>Adding a log handler for all critical messages from GTK+</title>
482 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
483 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
488 * <title>Adding a log handler for <emphasis>all</emphasis> messages from
491 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
492 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
496 * Returns: the id of the new handler
499 g_log_set_handler (const gchar *log_domain,
500 GLogLevelFlags log_levels,
504 static guint handler_id = 0;
506 GLogHandler *handler;
508 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
509 g_return_val_if_fail (log_func != NULL, 0);
514 handler = g_new (GLogHandler, 1);
516 g_mutex_lock (&g_messages_lock);
518 domain = g_log_find_domain_L (log_domain);
520 domain = g_log_domain_new_L (log_domain);
522 handler->id = ++handler_id;
523 handler->log_level = log_levels;
524 handler->log_func = log_func;
525 handler->data = user_data;
526 handler->next = domain->handlers;
527 domain->handlers = handler;
529 g_mutex_unlock (&g_messages_lock);
535 * g_log_set_default_handler:
536 * @log_func: the log handler function
537 * @user_data: data passed to the log handler
539 * Installs a default log handler which is used if no
540 * log handler has been set for the particular log domain
541 * and log level combination. By default, GLib uses
542 * g_log_default_handler() as default log handler.
544 * Returns: the previous default log handler
549 g_log_set_default_handler (GLogFunc log_func,
552 GLogFunc old_log_func;
554 g_mutex_lock (&g_messages_lock);
555 old_log_func = default_log_func;
556 default_log_func = log_func;
557 default_log_data = user_data;
558 g_mutex_unlock (&g_messages_lock);
564 * g_test_log_set_fatal_handler:
565 * @log_func: the log handler function.
566 * @user_data: data passed to the log handler.
568 * Installs a non-error fatal log handler which can be
569 * used to decide whether log messages which are counted
570 * as fatal abort the program.
572 * The use case here is that you are running a test case
573 * that depends on particular libraries or circumstances
574 * and cannot prevent certain known critical or warning
575 * messages. So you install a handler that compares the
576 * domain and message to precisely not abort in such a case.
578 * Note that the handler is reset at the beginning of
579 * any test case, so you have to set it inside each test
580 * function which needs the special behavior.
582 * This handler has no effect on g_error messages.
587 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
590 g_mutex_lock (&g_messages_lock);
591 fatal_log_func = log_func;
592 fatal_log_data = user_data;
593 g_mutex_unlock (&g_messages_lock);
597 * g_log_remove_handler:
598 * @log_domain: the log domain
599 * @handler_id: the id of the handler, which was returned
600 * in g_log_set_handler()
602 * Removes the log handler.
605 g_log_remove_handler (const gchar *log_domain,
608 register GLogDomain *domain;
610 g_return_if_fail (handler_id > 0);
615 g_mutex_lock (&g_messages_lock);
616 domain = g_log_find_domain_L (log_domain);
619 GLogHandler *work, *last;
622 work = domain->handlers;
625 if (work->id == handler_id)
628 last->next = work->next;
630 domain->handlers = work->next;
631 g_log_domain_check_free_L (domain);
632 g_mutex_unlock (&g_messages_lock);
640 g_mutex_unlock (&g_messages_lock);
641 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
642 G_STRLOC, handler_id, log_domain);
645 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
647 (wc >= 0x80 && wc < 0xa0)))
650 strdup_convert (const gchar *string,
651 const gchar *charset)
653 if (!g_utf8_validate (string, -1, NULL))
655 GString *gstring = g_string_new ("[Invalid UTF-8] ");
658 for (p = (guchar *)string; *p; p++)
660 if (CHAR_IS_SAFE(*p) &&
661 !(*p == '\r' && *(p + 1) != '\n') &&
663 g_string_append_c (gstring, *p);
665 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
668 return g_string_free (gstring, FALSE);
674 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
679 /* Not thread-safe, but doesn't matter if we print the warning twice
681 static gboolean warned = FALSE;
685 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
689 return g_strdup (string);
694 /* For a radix of 8 we need at most 3 output bytes for 1 input
695 * byte. Additionally we might need up to 2 output bytes for the
696 * readix prefix and 1 byte for the trailing NULL.
698 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
701 format_unsigned (gchar *buf,
709 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
711 if (radix != 8 && radix != 10 && radix != 16)
744 /* Again we can't use g_assert; actually this check should _never_ fail. */
745 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
758 buf[i] = c + 'a' - 10;
765 /* string size big enough to hold level prefix */
766 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
768 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
770 /* these are emitted by the default log handler */
771 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
772 /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
773 #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
776 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
777 GLogLevelFlags log_level)
779 gboolean to_stdout = TRUE;
781 /* we may not call _any_ GLib functions here */
783 switch (log_level & G_LOG_LEVEL_MASK)
785 case G_LOG_LEVEL_ERROR:
786 strcpy (level_prefix, "ERROR");
789 case G_LOG_LEVEL_CRITICAL:
790 strcpy (level_prefix, "CRITICAL");
793 case G_LOG_LEVEL_WARNING:
794 strcpy (level_prefix, "WARNING");
797 case G_LOG_LEVEL_MESSAGE:
798 strcpy (level_prefix, "Message");
801 case G_LOG_LEVEL_INFO:
802 strcpy (level_prefix, "INFO");
804 case G_LOG_LEVEL_DEBUG:
805 strcpy (level_prefix, "DEBUG");
810 strcpy (level_prefix, "LOG-");
811 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
814 strcpy (level_prefix, "LOG");
817 if (log_level & G_LOG_FLAG_RECURSION)
818 strcat (level_prefix, " (recursed)");
819 if (log_level & ALERT_LEVELS)
820 strcat (level_prefix, " **");
823 if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
824 win32_keep_fatal_message = TRUE;
826 return to_stdout ? 1 : 2;
831 GLogLevelFlags log_level;
833 } GTestExpectedMessage;
835 static GSList *expected_messages = NULL;
839 * @log_domain: the log domain
840 * @log_level: the log level
841 * @format: the message format. See the printf() documentation
842 * @args: the parameters to insert into the format string
844 * Logs an error or debugging message.
846 * If the log level has been set as fatal, the abort()
847 * function is called to terminate the program.
850 g_logv (const gchar *log_domain,
851 GLogLevelFlags log_level,
855 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
856 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
857 gchar buffer[1025], *msg, *msg_alloc = NULL;
860 log_level &= G_LOG_LEVEL_MASK;
864 if (log_level & G_LOG_FLAG_RECURSION)
866 /* we use a stack buffer of fixed size, since we're likely
867 * in an out-of-memory situation
869 gsize size G_GNUC_UNUSED;
871 size = _g_vsnprintf (buffer, 1024, format, args);
875 msg = msg_alloc = g_strdup_vprintf (format, args);
877 if (expected_messages)
879 GTestExpectedMessage *expected = expected_messages->data;
881 expected_messages = g_slist_delete_link (expected_messages,
883 if (strcmp (expected->log_domain, log_domain) == 0 &&
884 ((log_level & expected->log_level) == expected->log_level) &&
885 g_pattern_match_simple (expected->pattern, msg))
887 g_free (expected->log_domain);
888 g_free (expected->pattern);
895 gchar level_prefix[STRING_BUFFER_SIZE];
896 gchar *expected_message;
898 mklevel_prefix (level_prefix, expected->log_level);
899 expected_message = g_strdup_printf ("Did not see expected message %s: %s",
900 level_prefix, expected->pattern);
901 g_log_default_handler (log_domain, log_level, expected_message, NULL);
902 g_free (expected_message);
904 log_level |= G_LOG_FLAG_FATAL;
908 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
910 register GLogLevelFlags test_level;
913 if (log_level & test_level)
917 GLogLevelFlags domain_fatal_mask;
918 gpointer data = NULL;
919 gboolean masquerade_fatal = FALSE;
923 test_level |= G_LOG_FLAG_FATAL;
925 test_level |= G_LOG_FLAG_RECURSION;
927 /* check recursion and lookup handler */
928 g_mutex_lock (&g_messages_lock);
929 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
930 domain = g_log_find_domain_L (log_domain ? log_domain : "");
932 test_level |= G_LOG_FLAG_RECURSION;
934 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
935 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
936 test_level |= G_LOG_FLAG_FATAL;
937 if (test_level & G_LOG_FLAG_RECURSION)
938 log_func = _g_log_fallback_handler;
940 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
942 g_mutex_unlock (&g_messages_lock);
944 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
946 log_func (log_domain, test_level, msg, data);
948 if ((test_level & G_LOG_FLAG_FATAL)
949 && !(test_level & G_LOG_LEVEL_ERROR))
951 masquerade_fatal = fatal_log_func
952 && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
955 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
958 if (win32_keep_fatal_message)
960 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
962 MessageBox (NULL, locale_msg, NULL,
963 MB_ICONERROR|MB_SETFOREGROUND);
965 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
970 if (!(test_level & G_LOG_FLAG_RECURSION))
974 #endif /* !G_OS_WIN32 */
978 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
987 * @log_domain: the log domain, usually #G_LOG_DOMAIN
988 * @log_level: the log level, either from #GLogLevelFlags
989 * or a user-defined level
990 * @format: the message format. See the printf() documentation
991 * @...: the parameters to insert into the format string
993 * Logs an error or debugging message.
995 * If the log level has been set as fatal, the abort()
996 * function is called to terminate the program.
999 g_log (const gchar *log_domain,
1000 GLogLevelFlags log_level,
1001 const gchar *format,
1006 va_start (args, format);
1007 g_logv (log_domain, log_level, format, args);
1012 g_return_if_fail_warning (const char *log_domain,
1013 const char *pretty_function,
1014 const char *expression)
1017 G_LOG_LEVEL_CRITICAL,
1018 "%s: assertion `%s' failed",
1024 g_warn_message (const char *domain,
1028 const char *warnexpr)
1031 g_snprintf (lstr, 32, "%d", line);
1033 s = g_strconcat ("(", file, ":", lstr, "):",
1034 func, func[0] ? ":" : "",
1035 " runtime check failed: (", warnexpr, ")", NULL);
1037 s = g_strconcat ("(", file, ":", lstr, "):",
1038 func, func[0] ? ":" : "",
1039 " ", "code should not be reached", NULL);
1040 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
1045 g_assert_warning (const char *log_domain,
1048 const char *pretty_function,
1049 const char *expression)
1054 ? "file %s: line %d (%s): assertion failed: (%s)"
1055 : "file %s: line %d (%s): should not be reached",
1064 * g_test_expect_message:
1065 * @log_domain: the log domain of the message
1066 * @log_level: the log level of the message
1067 * @pattern: a glob-style
1068 * <link linkend="glib-Glob-style-pattern-matching">pattern</link>
1070 * Indicates that a message with the given @log_domain and @log_level,
1071 * with text matching @pattern, is expected to be logged. When this
1072 * message is logged, it will not be printed, and the test case will
1075 * Use g_test_assert_expected_messages() to assert that all
1076 * previously-expected messages have been seen and suppressed.
1078 * You can call this multiple times in a row, if multiple messages are
1079 * expected as a result of a single call. (The messages must appear in
1080 * the same order as the calls to g_test_expect_message().)
1085 * /* g_main_context_push_thread_default() should fail if the
1086 * * context is already owned by another thread.
1088 * g_test_expect_message (G_LOG_DOMAIN,
1089 * G_LOG_LEVEL_CRITICAL,
1090 * "assertion*acquired_context*failed");
1091 * g_main_context_push_thread_default (bad_context);
1092 * g_test_assert_expected_messages ();
1095 * Note that you cannot use this to test g_error() messages, since
1096 * g_error() intentionally never returns even if the program doesn't
1097 * abort; use g_test_trap_fork() in this case.
1102 g_test_expect_message (const gchar *log_domain,
1103 GLogLevelFlags log_level,
1104 const gchar *pattern)
1106 GTestExpectedMessage *expected;
1108 g_return_if_fail (log_domain != NULL);
1109 g_return_if_fail (log_level != 0);
1110 g_return_if_fail (pattern != NULL);
1112 expected = g_new (GTestExpectedMessage, 1);
1113 expected->log_domain = g_strdup (log_domain);
1114 expected->log_level = log_level;
1115 expected->pattern = g_strdup (pattern);
1117 expected_messages = g_slist_append (expected_messages, expected);
1121 g_test_assert_expected_messages_internal (const char *domain,
1126 if (expected_messages)
1128 GTestExpectedMessage *expected;
1129 gchar level_prefix[STRING_BUFFER_SIZE];
1132 expected = expected_messages->data;
1134 mklevel_prefix (level_prefix, expected->log_level);
1135 message = g_strdup_printf ("Did not see expected message %s: %s",
1136 level_prefix, expected->pattern);
1137 g_assertion_message (domain, file, line, func, message);
1143 * g_test_assert_expected_messages:
1145 * Asserts that all messages previously indicated via
1146 * g_test_expect_message() have been seen and suppressed.
1152 _g_log_fallback_handler (const gchar *log_domain,
1153 GLogLevelFlags log_level,
1154 const gchar *message,
1155 gpointer unused_data)
1157 gchar level_prefix[STRING_BUFFER_SIZE];
1159 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1163 /* we cannot call _any_ GLib functions in this fallback handler,
1164 * which is why we skip UTF-8 conversion, etc.
1165 * since we either recursed or ran out of memory, we're in a pretty
1166 * pathologic situation anyways, what we can do is giving the
1167 * the process ID unconditionally however.
1170 fd = mklevel_prefix (level_prefix, log_level);
1172 message = "(NULL) message";
1175 format_unsigned (pid_string, getpid (), 10);
1179 write_string (fd, "\n");
1181 write_string (fd, "\n** ");
1184 write_string (fd, "(process:");
1185 write_string (fd, pid_string);
1186 write_string (fd, "): ");
1191 write_string (fd, log_domain);
1192 write_string (fd, "-");
1194 write_string (fd, level_prefix);
1195 write_string (fd, ": ");
1196 write_string (fd, message);
1200 escape_string (GString *string)
1202 const char *p = string->str;
1205 while (p < string->str + string->len)
1209 wc = g_utf8_get_char_validated (p, -1);
1210 if (wc == (gunichar)-1 || wc == (gunichar)-2)
1215 pos = p - string->str;
1217 /* Emit invalid UTF-8 as hex escapes
1219 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
1220 g_string_erase (string, pos, 1);
1221 g_string_insert (string, pos, tmp);
1223 p = string->str + (pos + 4); /* Skip over escape sequence */
1230 safe = *(p + 1) == '\n';
1234 safe = CHAR_IS_SAFE (wc);
1242 pos = p - string->str;
1244 /* Largest char we escape is 0x0a, so we don't have to worry
1245 * about 8-digit \Uxxxxyyyy
1247 tmp = g_strdup_printf ("\\u%04x", wc);
1248 g_string_erase (string, pos, g_utf8_next_char (p) - p);
1249 g_string_insert (string, pos, tmp);
1252 p = string->str + (pos + 6); /* Skip over escape sequence */
1255 p = g_utf8_next_char (p);
1260 * g_log_default_handler:
1261 * @log_domain: the log domain of the message
1262 * @log_level: the level of the message
1263 * @message: the message
1264 * @unused_data: data passed from g_log() which is unused
1266 * The default log handler set up by GLib; g_log_set_default_handler()
1267 * allows to install an alternate default log handler.
1268 * This is used if no log handler has been set for the particular log
1269 * domain and log level combination. It outputs the message to stderr
1270 * or stdout and if the log level is fatal it calls abort().
1272 * The behavior of this log handler can be influenced by a number of
1273 * environment variables:
1276 * <term><envar>G_MESSAGES_PREFIXED</envar></term>
1278 * A :-separated list of log levels for which messages should
1279 * be prefixed by the program name and PID of the aplication.
1283 * <term><envar>G_MESSAGES_DEBUG</envar></term>
1285 * A space-separated list of log domains for which debug and
1286 * informational messages are printed. By default these
1287 * messages are not printed.
1292 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1293 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1297 g_log_default_handler (const gchar *log_domain,
1298 GLogLevelFlags log_level,
1299 const gchar *message,
1300 gpointer unused_data)
1302 gchar level_prefix[STRING_BUFFER_SIZE], *string;
1305 const gchar *domains;
1307 if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
1310 domains = g_getenv ("G_MESSAGES_DEBUG");
1311 if (((log_level & INFO_LEVELS) == 0) ||
1313 (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
1317 /* we can be called externally with recursion for whatever reason */
1318 if (log_level & G_LOG_FLAG_RECURSION)
1320 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1324 fd = mklevel_prefix (level_prefix, log_level);
1326 gstring = g_string_new (NULL);
1327 if (log_level & ALERT_LEVELS)
1328 g_string_append (gstring, "\n");
1330 g_string_append (gstring, "** ");
1332 if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
1334 const gchar *prg_name = g_get_prgname ();
1337 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1339 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1344 g_string_append (gstring, log_domain);
1345 g_string_append_c (gstring, '-');
1347 g_string_append (gstring, level_prefix);
1349 g_string_append (gstring, ": ");
1351 g_string_append (gstring, "(NULL) message");
1355 const gchar *charset;
1357 msg = g_string_new (message);
1358 escape_string (msg);
1360 if (g_get_charset (&charset))
1361 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1364 string = strdup_convert (msg->str, charset);
1365 g_string_append (gstring, string);
1369 g_string_free (msg, TRUE);
1371 g_string_append (gstring, "\n");
1373 string = g_string_free (gstring, FALSE);
1375 write_string (fd, string);
1380 * g_set_print_handler:
1381 * @func: the new print handler
1383 * Sets the print handler.
1385 * Any messages passed to g_print() will be output via
1386 * the new handler. The default handler simply outputs
1387 * the message to stdout. By providing your own handler
1388 * you can redirect the output, to a GTK+ widget or a
1389 * log file for example.
1391 * Returns: the old print handler
1394 g_set_print_handler (GPrintFunc func)
1396 GPrintFunc old_print_func;
1398 g_mutex_lock (&g_messages_lock);
1399 old_print_func = glib_print_func;
1400 glib_print_func = func;
1401 g_mutex_unlock (&g_messages_lock);
1403 return old_print_func;
1408 * @format: the message format. See the printf() documentation
1409 * @...: the parameters to insert into the format string
1411 * Outputs a formatted message via the print handler.
1412 * The default print handler simply outputs the message to stdout.
1414 * g_print() should not be used from within libraries for debugging
1415 * messages, since it may be redirected by applications to special
1416 * purpose message windows or even files. Instead, libraries should
1417 * use g_log(), or the convenience functions g_message(), g_warning()
1421 g_print (const gchar *format,
1426 GPrintFunc local_glib_print_func;
1428 g_return_if_fail (format != NULL);
1430 va_start (args, format);
1431 string = g_strdup_vprintf (format, args);
1434 g_mutex_lock (&g_messages_lock);
1435 local_glib_print_func = glib_print_func;
1436 g_mutex_unlock (&g_messages_lock);
1438 if (local_glib_print_func)
1439 local_glib_print_func (string);
1442 const gchar *charset;
1444 if (g_get_charset (&charset))
1445 fputs (string, stdout); /* charset is UTF-8 already */
1448 gchar *lstring = strdup_convert (string, charset);
1450 fputs (lstring, stdout);
1459 * g_set_printerr_handler:
1460 * @func: the new error message handler
1462 * Sets the handler for printing error messages.
1464 * Any messages passed to g_printerr() will be output via
1465 * the new handler. The default handler simply outputs the
1466 * message to stderr. By providing your own handler you can
1467 * redirect the output, to a GTK+ widget or a log file for
1470 * Returns: the old error message handler
1473 g_set_printerr_handler (GPrintFunc func)
1475 GPrintFunc old_printerr_func;
1477 g_mutex_lock (&g_messages_lock);
1478 old_printerr_func = glib_printerr_func;
1479 glib_printerr_func = func;
1480 g_mutex_unlock (&g_messages_lock);
1482 return old_printerr_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 error message handler.
1491 * The default handler simply outputs the message to stderr.
1493 * g_printerr() should not be used from within libraries.
1494 * Instead g_log() should be used, or the convenience functions
1495 * g_message(), g_warning() and g_error().
1498 g_printerr (const gchar *format,
1503 GPrintFunc local_glib_printerr_func;
1505 g_return_if_fail (format != NULL);
1507 va_start (args, format);
1508 string = g_strdup_vprintf (format, args);
1511 g_mutex_lock (&g_messages_lock);
1512 local_glib_printerr_func = glib_printerr_func;
1513 g_mutex_unlock (&g_messages_lock);
1515 if (local_glib_printerr_func)
1516 local_glib_printerr_func (string);
1519 const gchar *charset;
1521 if (g_get_charset (&charset))
1522 fputs (string, stderr); /* charset is UTF-8 already */
1525 gchar *lstring = strdup_convert (string, charset);
1527 fputs (lstring, stderr);
1536 * g_printf_string_upper_bound:
1537 * @format: the format string. See the printf() documentation
1538 * @args: the parameters to be inserted into the format string
1540 * Calculates the maximum space needed to store the output
1541 * of the sprintf() function.
1543 * Returns: the maximum space needed to store the formatted string
1546 g_printf_string_upper_bound (const gchar *format,
1550 return _g_vsnprintf (&c, 1, format, args) + 1;