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 g_return family of macros (g_return_if_fail(),
37 * g_return_val_if_fail(), g_return_if_reached(),
38 * g_return_val_if_reached()) should only be used for programming
39 * errors, a typical use case is checking for invalid parameters at
40 * the beginning of a public function. They should not be used if
41 * you just mean "if (error) return", they should only be used if
42 * you mean "if (bug in program) return". The program behavior is
43 * generally considered undefined after one of these checks fails.
44 * They are not intended for normal control flow, only to give a
45 * perhaps-helpful warning before giving up.
58 #include "glib-init.h"
59 #include "gbacktrace.h"
64 #include "gprintfint.h"
65 #include "gtestutils.h"
67 #include "gstrfuncs.h"
76 #include <process.h> /* For getpid() */
78 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
85 * @title: Message Logging
86 * @short_description: versatile support for logging messages
87 * with different levels of importance
89 * These functions provide support for logging error messages
90 * or messages used for debugging.
92 * There are several built-in levels of messages, defined in
93 * #GLogLevelFlags. These can be extended with user-defined levels.
99 * Defines the log domain.
101 * For applications, this is typically left as the default %NULL
102 * (or "") domain. Libraries should define this so that any messages
103 * which they log can be differentiated from messages from other
104 * libraries and application code. But be careful not to define
105 * it in any public header files.
107 * For example, GTK+ uses this in its Makefile.am:
109 * AM_CPPFLAGS = -DG_LOG_DOMAIN=\"Gtk\"
116 * GLib log levels that are considered fatal by default.
121 * @log_domain: the log domain of the message
122 * @log_level: the log level of the message (including the
123 * fatal and recursion flags)
124 * @message: the message to process
125 * @user_data: user data, set in g_log_set_handler()
127 * Specifies the prototype of log handler functions.
129 * The default log handler, g_log_default_handler(), automatically appends a
130 * new-line character to @message when printing it. It is advised that any
131 * custom log handler functions behave similarly, so that logging calls in user
132 * code do not need modifying to add a new-line character to the message if the
133 * log handler is changed.
138 * @G_LOG_FLAG_RECURSION: internal flag
139 * @G_LOG_FLAG_FATAL: internal flag
140 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
141 * This level is also used for messages produced by g_assert().
142 * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
143 * This level is also used for messages produced by g_return_if_fail()
144 * and g_return_val_if_fail().
145 * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
146 * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
147 * @G_LOG_LEVEL_INFO: log level for informational messages, see g_info()
148 * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
149 * @G_LOG_LEVEL_MASK: a mask including all log levels
151 * Flags specifying the level of log messages.
153 * It is possible to change how GLib treats messages of the various
154 * levels using g_log_set_handler() and g_log_set_fatal_mask().
158 * G_LOG_LEVEL_USER_SHIFT:
160 * Log levels below 1<<G_LOG_LEVEL_USER_SHIFT are used by GLib.
161 * Higher bits can be used for user-defined log levels.
166 * @...: format string, followed by parameters to insert
167 * into the format string (as with printf())
169 * A convenience function/macro to log a normal message.
171 * If g_log_default_handler() is used as the log handler function, a new-line
172 * character will automatically be appended to @..., and need not be entered
178 * @...: format string, followed by parameters to insert
179 * into the format string (as with printf())
181 * A convenience function/macro to log a warning message.
183 * This is not intended for end user error reporting. Use of #GError is
184 * preferred for that instead, as it allows calling functions to perform actions
185 * conditional on the type of error.
187 * You can make warnings fatal at runtime by setting the `G_DEBUG`
188 * environment variable (see
189 * [Running GLib Applications](glib-running.html)).
191 * If g_log_default_handler() is used as the log handler function,
192 * a newline character will automatically be appended to @..., and
193 * need not be entered manually.
198 * @...: format string, followed by parameters to insert
199 * into the format string (as with printf())
201 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
202 * It's more or less application-defined what constitutes
203 * a critical vs. a regular warning. You could call
204 * g_log_set_always_fatal() to make critical warnings exit
205 * the program, then use g_critical() for fatal errors, for
208 * You can also make critical warnings fatal at runtime by
209 * setting the `G_DEBUG` environment variable (see
210 * [Running GLib Applications](glib-running.html)).
212 * If g_log_default_handler() is used as the log handler function, a new-line
213 * character will automatically be appended to @..., and need not be entered
219 * @...: format string, followed by parameters to insert
220 * into the format string (as with printf())
222 * A convenience function/macro to log an error message.
224 * This is not intended for end user error reporting. Use of #GError is
225 * preferred for that instead, as it allows calling functions to perform actions
226 * conditional on the type of error.
228 * Error messages are always fatal, resulting in a call to
229 * abort() to terminate the application. This function will
230 * result in a core dump; don't use it for errors you expect.
231 * Using this function indicates a bug in your program, i.e.
232 * an assertion failure.
234 * If g_log_default_handler() is used as the log handler function, a new-line
235 * character will automatically be appended to @..., and need not be entered
242 * @...: format string, followed by parameters to insert
243 * into the format string (as with printf())
245 * A convenience function/macro to log an informational message. Seldom used.
247 * If g_log_default_handler() is used as the log handler function, a new-line
248 * character will automatically be appended to @..., and need not be entered
251 * Such messages are suppressed by the g_log_default_handler() unless
252 * the G_MESSAGES_DEBUG environment variable is set appropriately.
259 * @...: format string, followed by parameters to insert
260 * into the format string (as with printf())
262 * A convenience function/macro to log a debug message.
264 * If g_log_default_handler() is used as the log handler function, a new-line
265 * character will automatically be appended to @..., and need not be entered
268 * Such messages are suppressed by the g_log_default_handler() unless
269 * the G_MESSAGES_DEBUG environment variable is set appropriately.
274 /* --- structures --- */
275 typedef struct _GLogDomain GLogDomain
;
276 typedef struct _GLogHandler GLogHandler
;
280 GLogLevelFlags fatal_mask
;
281 GLogHandler
*handlers
;
287 GLogLevelFlags log_level
;
290 GDestroyNotify destroy
;
295 /* --- variables --- */
296 static GMutex g_messages_lock
;
297 static GLogDomain
*g_log_domains
= NULL
;
298 static GPrintFunc glib_print_func
= NULL
;
299 static GPrintFunc glib_printerr_func
= NULL
;
300 static GPrivate g_log_depth
;
301 static GLogFunc default_log_func
= g_log_default_handler
;
302 static gpointer default_log_data
= NULL
;
303 static GTestLogFatalFunc fatal_log_func
= NULL
;
304 static gpointer fatal_log_data
;
306 /* --- functions --- */
308 static void _g_log_abort (gboolean breakpoint
);
311 _g_log_abort (gboolean breakpoint
)
313 if (g_test_subprocess ())
315 /* If this is a test case subprocess then it probably caused
316 * this error message on purpose, so just exit() rather than
317 * abort()ing, to avoid triggering any system crash-reporting
330 # include <windows.h>
331 static gboolean win32_keep_fatal_message
= FALSE
;
333 /* This default message will usually be overwritten. */
334 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
335 * called with huge strings, is it?
337 static gchar fatal_msg_buf
[1000] = "Unspecified fatal error encountered, aborting.";
338 static gchar
*fatal_msg_ptr
= fatal_msg_buf
;
346 if (win32_keep_fatal_message
)
348 memcpy (fatal_msg_ptr
, buf
, len
);
349 fatal_msg_ptr
+= len
;
354 write (fd
, buf
, len
);
358 #define write(fd, buf, len) dowrite(fd, buf, len)
363 write_string (FILE *stream
,
366 fputs (string
, stream
);
370 g_log_find_domain_L (const gchar
*log_domain
)
374 domain
= g_log_domains
;
377 if (strcmp (domain
->log_domain
, log_domain
) == 0)
379 domain
= domain
->next
;
385 g_log_domain_new_L (const gchar
*log_domain
)
389 domain
= g_new (GLogDomain
, 1);
390 domain
->log_domain
= g_strdup (log_domain
);
391 domain
->fatal_mask
= G_LOG_FATAL_MASK
;
392 domain
->handlers
= NULL
;
394 domain
->next
= g_log_domains
;
395 g_log_domains
= domain
;
401 g_log_domain_check_free_L (GLogDomain
*domain
)
403 if (domain
->fatal_mask
== G_LOG_FATAL_MASK
&&
404 domain
->handlers
== NULL
)
406 GLogDomain
*last
, *work
;
410 work
= g_log_domains
;
416 last
->next
= domain
->next
;
418 g_log_domains
= domain
->next
;
419 g_free (domain
->log_domain
);
430 g_log_domain_get_handler_L (GLogDomain
*domain
,
431 GLogLevelFlags log_level
,
434 if (domain
&& log_level
)
436 GLogHandler
*handler
;
438 handler
= domain
->handlers
;
441 if ((handler
->log_level
& log_level
) == log_level
)
443 *data
= handler
->data
;
444 return handler
->log_func
;
446 handler
= handler
->next
;
450 *data
= default_log_data
;
451 return default_log_func
;
455 * g_log_set_always_fatal:
456 * @fatal_mask: the mask containing bits set for each level
457 * of error which is to be fatal
459 * Sets the message levels which are always fatal, in any log domain.
460 * When a message with any of these levels is logged the program terminates.
461 * You can only set the levels defined by GLib to be fatal.
462 * %G_LOG_LEVEL_ERROR is always fatal.
464 * You can also make some message levels fatal at runtime by setting
465 * the `G_DEBUG` environment variable (see
466 * [Running GLib Applications](glib-running.html)).
468 * Returns: the old fatal mask
471 g_log_set_always_fatal (GLogLevelFlags fatal_mask
)
473 GLogLevelFlags old_mask
;
475 /* restrict the global mask to levels that are known to glib
476 * since this setting applies to all domains
478 fatal_mask
&= (1 << G_LOG_LEVEL_USER_SHIFT
) - 1;
479 /* force errors to be fatal */
480 fatal_mask
|= G_LOG_LEVEL_ERROR
;
481 /* remove bogus flag */
482 fatal_mask
&= ~G_LOG_FLAG_FATAL
;
484 g_mutex_lock (&g_messages_lock
);
485 old_mask
= g_log_always_fatal
;
486 g_log_always_fatal
= fatal_mask
;
487 g_mutex_unlock (&g_messages_lock
);
493 * g_log_set_fatal_mask:
494 * @log_domain: the log domain
495 * @fatal_mask: the new fatal mask
497 * Sets the log levels which are fatal in the given domain.
498 * %G_LOG_LEVEL_ERROR is always fatal.
500 * Returns: the old fatal mask for the log domain
503 g_log_set_fatal_mask (const gchar
*log_domain
,
504 GLogLevelFlags fatal_mask
)
506 GLogLevelFlags old_flags
;
512 /* force errors to be fatal */
513 fatal_mask
|= G_LOG_LEVEL_ERROR
;
514 /* remove bogus flag */
515 fatal_mask
&= ~G_LOG_FLAG_FATAL
;
517 g_mutex_lock (&g_messages_lock
);
519 domain
= g_log_find_domain_L (log_domain
);
521 domain
= g_log_domain_new_L (log_domain
);
522 old_flags
= domain
->fatal_mask
;
524 domain
->fatal_mask
= fatal_mask
;
525 g_log_domain_check_free_L (domain
);
527 g_mutex_unlock (&g_messages_lock
);
534 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
536 * @log_levels: the log levels to apply the log handler for.
537 * To handle fatal and recursive messages as well, combine
538 * the log levels with the #G_LOG_FLAG_FATAL and
539 * #G_LOG_FLAG_RECURSION bit flags.
540 * @log_func: the log handler function
541 * @user_data: data passed to the log handler
543 * Sets the log handler for a domain and a set of log levels.
544 * To handle fatal and recursive messages the @log_levels parameter
545 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
548 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
549 * you want to set a handler for this log level you must combine it with
552 * Here is an example for adding a log handler for all warning messages
553 * in the default domain:
554 * |[<!-- language="C" -->
555 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
556 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
559 * This example adds a log handler for all critical messages from GTK+:
560 * |[<!-- language="C" -->
561 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
562 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
565 * This example adds a log handler for all messages from GLib:
566 * |[<!-- language="C" -->
567 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
568 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
571 * Returns: the id of the new handler
574 g_log_set_handler (const gchar
*log_domain
,
575 GLogLevelFlags log_levels
,
579 return g_log_set_handler_full (log_domain
, log_levels
, log_func
, user_data
, NULL
);
583 * g_log_set_handler_full: (rename-to g_log_set_handler)
584 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
586 * @log_levels: the log levels to apply the log handler for.
587 * To handle fatal and recursive messages as well, combine
588 * the log levels with the #G_LOG_FLAG_FATAL and
589 * #G_LOG_FLAG_RECURSION bit flags.
590 * @log_func: the log handler function
591 * @user_data: data passed to the log handler
592 * @destroy: destroy notify for @user_data, or %NULL
594 * Like g_log_sets_handler(), but takes a destroy notify for the @user_data.
596 * Returns: the id of the new handler
601 g_log_set_handler_full (const gchar
*log_domain
,
602 GLogLevelFlags log_levels
,
605 GDestroyNotify destroy
)
607 static guint handler_id
= 0;
609 GLogHandler
*handler
;
611 g_return_val_if_fail ((log_levels
& G_LOG_LEVEL_MASK
) != 0, 0);
612 g_return_val_if_fail (log_func
!= NULL
, 0);
617 handler
= g_new (GLogHandler
, 1);
619 g_mutex_lock (&g_messages_lock
);
621 domain
= g_log_find_domain_L (log_domain
);
623 domain
= g_log_domain_new_L (log_domain
);
625 handler
->id
= ++handler_id
;
626 handler
->log_level
= log_levels
;
627 handler
->log_func
= log_func
;
628 handler
->data
= user_data
;
629 handler
->destroy
= destroy
;
630 handler
->next
= domain
->handlers
;
631 domain
->handlers
= handler
;
633 g_mutex_unlock (&g_messages_lock
);
639 * g_log_set_default_handler:
640 * @log_func: the log handler function
641 * @user_data: data passed to the log handler
643 * Installs a default log handler which is used if no
644 * log handler has been set for the particular log domain
645 * and log level combination. By default, GLib uses
646 * g_log_default_handler() as default log handler.
648 * Returns: the previous default log handler
653 g_log_set_default_handler (GLogFunc log_func
,
656 GLogFunc old_log_func
;
658 g_mutex_lock (&g_messages_lock
);
659 old_log_func
= default_log_func
;
660 default_log_func
= log_func
;
661 default_log_data
= user_data
;
662 g_mutex_unlock (&g_messages_lock
);
668 * g_test_log_set_fatal_handler:
669 * @log_func: the log handler function.
670 * @user_data: data passed to the log handler.
672 * Installs a non-error fatal log handler which can be
673 * used to decide whether log messages which are counted
674 * as fatal abort the program.
676 * The use case here is that you are running a test case
677 * that depends on particular libraries or circumstances
678 * and cannot prevent certain known critical or warning
679 * messages. So you install a handler that compares the
680 * domain and message to precisely not abort in such a case.
682 * Note that the handler is reset at the beginning of
683 * any test case, so you have to set it inside each test
684 * function which needs the special behavior.
686 * This handler has no effect on g_error messages.
691 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func
,
694 g_mutex_lock (&g_messages_lock
);
695 fatal_log_func
= log_func
;
696 fatal_log_data
= user_data
;
697 g_mutex_unlock (&g_messages_lock
);
701 * g_log_remove_handler:
702 * @log_domain: the log domain
703 * @handler_id: the id of the handler, which was returned
704 * in g_log_set_handler()
706 * Removes the log handler.
709 g_log_remove_handler (const gchar
*log_domain
,
714 g_return_if_fail (handler_id
> 0);
719 g_mutex_lock (&g_messages_lock
);
720 domain
= g_log_find_domain_L (log_domain
);
723 GLogHandler
*work
, *last
;
726 work
= domain
->handlers
;
729 if (work
->id
== handler_id
)
732 last
->next
= work
->next
;
734 domain
->handlers
= work
->next
;
735 g_log_domain_check_free_L (domain
);
736 g_mutex_unlock (&g_messages_lock
);
738 work
->destroy (work
->data
);
746 g_mutex_unlock (&g_messages_lock
);
747 g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
748 G_STRLOC
, handler_id
, log_domain
);
751 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
753 (wc >= 0x80 && wc < 0xa0)))
756 strdup_convert (const gchar
*string
,
757 const gchar
*charset
)
759 if (!g_utf8_validate (string
, -1, NULL
))
761 GString
*gstring
= g_string_new ("[Invalid UTF-8] ");
764 for (p
= (guchar
*)string
; *p
; p
++)
766 if (CHAR_IS_SAFE(*p
) &&
767 !(*p
== '\r' && *(p
+ 1) != '\n') &&
769 g_string_append_c (gstring
, *p
);
771 g_string_append_printf (gstring
, "\\x%02x", (guint
)(guchar
)*p
);
774 return g_string_free (gstring
, FALSE
);
780 gchar
*result
= g_convert_with_fallback (string
, -1, charset
, "UTF-8", "?", NULL
, NULL
, &err
);
785 /* Not thread-safe, but doesn't matter if we print the warning twice
787 static gboolean warned
= FALSE
;
791 _g_fprintf (stderr
, "GLib: Cannot convert message: %s\n", err
->message
);
795 return g_strdup (string
);
800 /* For a radix of 8 we need at most 3 output bytes for 1 input
801 * byte. Additionally we might need up to 2 output bytes for the
802 * readix prefix and 1 byte for the trailing NULL.
804 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
807 format_unsigned (gchar
*buf
,
815 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
817 if (radix
!= 8 && radix
!= 10 && radix
!= 16)
850 /* Again we can't use g_assert; actually this check should _never_ fail. */
851 if (n
> FORMAT_UNSIGNED_BUFSIZE
- 3)
864 buf
[i
] = c
+ 'a' - 10;
871 /* string size big enough to hold level prefix */
872 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
874 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
876 /* these are emitted by the default log handler */
877 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
878 /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
879 #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
882 mklevel_prefix (gchar level_prefix
[STRING_BUFFER_SIZE
],
883 GLogLevelFlags log_level
)
885 gboolean to_stdout
= TRUE
;
887 /* we may not call _any_ GLib functions here */
889 switch (log_level
& G_LOG_LEVEL_MASK
)
891 case G_LOG_LEVEL_ERROR
:
892 strcpy (level_prefix
, "ERROR");
895 case G_LOG_LEVEL_CRITICAL
:
896 strcpy (level_prefix
, "CRITICAL");
899 case G_LOG_LEVEL_WARNING
:
900 strcpy (level_prefix
, "WARNING");
903 case G_LOG_LEVEL_MESSAGE
:
904 strcpy (level_prefix
, "Message");
907 case G_LOG_LEVEL_INFO
:
908 strcpy (level_prefix
, "INFO");
910 case G_LOG_LEVEL_DEBUG
:
911 strcpy (level_prefix
, "DEBUG");
916 strcpy (level_prefix
, "LOG-");
917 format_unsigned (level_prefix
+ 4, log_level
& G_LOG_LEVEL_MASK
, 16);
920 strcpy (level_prefix
, "LOG");
923 if (log_level
& G_LOG_FLAG_RECURSION
)
924 strcat (level_prefix
, " (recursed)");
925 if (log_level
& ALERT_LEVELS
)
926 strcat (level_prefix
, " **");
929 if ((log_level
& G_LOG_FLAG_FATAL
) != 0 && !g_test_initialized ())
930 win32_keep_fatal_message
= TRUE
;
932 return to_stdout
? stdout
: stderr
;
937 GLogLevelFlags log_level
;
939 } GTestExpectedMessage
;
941 static GSList
*expected_messages
= NULL
;
945 * @log_domain: (nullable): the log domain, or %NULL for the default ""
947 * @log_level: the log level
948 * @format: the message format. See the printf() documentation
949 * @args: the parameters to insert into the format string
951 * Logs an error or debugging message.
953 * If the log level has been set as fatal, the abort()
954 * function is called to terminate the program.
956 * If g_log_default_handler() is used as the log handler function, a new-line
957 * character will automatically be appended to @..., and need not be entered
961 g_logv (const gchar
*log_domain
,
962 GLogLevelFlags log_level
,
966 gboolean was_fatal
= (log_level
& G_LOG_FLAG_FATAL
) != 0;
967 gboolean was_recursion
= (log_level
& G_LOG_FLAG_RECURSION
) != 0;
968 gchar buffer
[1025], *msg
, *msg_alloc
= NULL
;
971 log_level
&= G_LOG_LEVEL_MASK
;
975 if (log_level
& G_LOG_FLAG_RECURSION
)
977 /* we use a stack buffer of fixed size, since we're likely
978 * in an out-of-memory situation
980 gsize size G_GNUC_UNUSED
;
982 size
= _g_vsnprintf (buffer
, 1024, format
, args
);
986 msg
= msg_alloc
= g_strdup_vprintf (format
, args
);
988 if (expected_messages
)
990 GTestExpectedMessage
*expected
= expected_messages
->data
;
992 if (g_strcmp0 (expected
->log_domain
, log_domain
) == 0 &&
993 ((log_level
& expected
->log_level
) == expected
->log_level
) &&
994 g_pattern_match_simple (expected
->pattern
, msg
))
996 expected_messages
= g_slist_delete_link (expected_messages
,
998 g_free (expected
->log_domain
);
999 g_free (expected
->pattern
);
1004 else if ((log_level
& G_LOG_LEVEL_DEBUG
) != G_LOG_LEVEL_DEBUG
)
1006 gchar level_prefix
[STRING_BUFFER_SIZE
];
1007 gchar
*expected_message
;
1009 mklevel_prefix (level_prefix
, expected
->log_level
);
1010 expected_message
= g_strdup_printf ("Did not see expected message %s-%s: %s",
1011 expected
->log_domain
? expected
->log_domain
: "**",
1012 level_prefix
, expected
->pattern
);
1013 g_log_default_handler (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, expected_message
, NULL
);
1014 g_free (expected_message
);
1016 log_level
|= G_LOG_FLAG_FATAL
;
1020 for (i
= g_bit_nth_msf (log_level
, -1); i
>= 0; i
= g_bit_nth_msf (log_level
, i
))
1022 GLogLevelFlags test_level
;
1024 test_level
= 1 << i
;
1025 if (log_level
& test_level
)
1029 GLogLevelFlags domain_fatal_mask
;
1030 gpointer data
= NULL
;
1031 gboolean masquerade_fatal
= FALSE
;
1035 test_level
|= G_LOG_FLAG_FATAL
;
1037 test_level
|= G_LOG_FLAG_RECURSION
;
1039 /* check recursion and lookup handler */
1040 g_mutex_lock (&g_messages_lock
);
1041 depth
= GPOINTER_TO_UINT (g_private_get (&g_log_depth
));
1042 domain
= g_log_find_domain_L (log_domain
? log_domain
: "");
1044 test_level
|= G_LOG_FLAG_RECURSION
;
1046 domain_fatal_mask
= domain
? domain
->fatal_mask
: G_LOG_FATAL_MASK
;
1047 if ((domain_fatal_mask
| g_log_always_fatal
) & test_level
)
1048 test_level
|= G_LOG_FLAG_FATAL
;
1049 if (test_level
& G_LOG_FLAG_RECURSION
)
1050 log_func
= _g_log_fallback_handler
;
1052 log_func
= g_log_domain_get_handler_L (domain
, test_level
, &data
);
1054 g_mutex_unlock (&g_messages_lock
);
1056 g_private_set (&g_log_depth
, GUINT_TO_POINTER (depth
));
1058 log_func (log_domain
, test_level
, msg
, data
);
1060 if ((test_level
& G_LOG_FLAG_FATAL
)
1061 && !(test_level
& G_LOG_LEVEL_ERROR
))
1063 masquerade_fatal
= fatal_log_func
1064 && !fatal_log_func (log_domain
, test_level
, msg
, fatal_log_data
);
1067 if ((test_level
& G_LOG_FLAG_FATAL
) && !masquerade_fatal
)
1070 if (win32_keep_fatal_message
)
1072 gchar
*locale_msg
= g_locale_from_utf8 (fatal_msg_buf
, -1, NULL
, NULL
, NULL
);
1074 MessageBox (NULL
, locale_msg
, NULL
,
1075 MB_ICONERROR
|MB_SETFOREGROUND
);
1077 _g_log_abort (IsDebuggerPresent () && !(test_level
& G_LOG_FLAG_RECURSION
));
1079 _g_log_abort (!(test_level
& G_LOG_FLAG_RECURSION
));
1080 #endif /* !G_OS_WIN32 */
1084 g_private_set (&g_log_depth
, GUINT_TO_POINTER (depth
));
1093 * @log_domain: (nullable): the log domain, usually #G_LOG_DOMAIN, or %NULL
1095 * @log_level: the log level, either from #GLogLevelFlags
1096 * or a user-defined level
1097 * @format: the message format. See the printf() documentation
1098 * @...: the parameters to insert into the format string
1100 * Logs an error or debugging message.
1102 * If the log level has been set as fatal, the abort()
1103 * function is called to terminate the program.
1105 * If g_log_default_handler() is used as the log handler function, a new-line
1106 * character will automatically be appended to @..., and need not be entered
1110 g_log (const gchar
*log_domain
,
1111 GLogLevelFlags log_level
,
1112 const gchar
*format
,
1117 va_start (args
, format
);
1118 g_logv (log_domain
, log_level
, format
, args
);
1123 * g_return_if_fail_warning: (skip)
1124 * @log_domain: (nullable):
1126 * @expression: (nullable):
1129 g_return_if_fail_warning (const char *log_domain
,
1130 const char *pretty_function
,
1131 const char *expression
)
1134 G_LOG_LEVEL_CRITICAL
,
1135 "%s: assertion '%s' failed",
1141 * g_warn_message: (skip)
1142 * @domain: (nullable):
1146 * @warnexpr: (nullable):
1149 g_warn_message (const char *domain
,
1153 const char *warnexpr
)
1156 g_snprintf (lstr
, 32, "%d", line
);
1158 s
= g_strconcat ("(", file
, ":", lstr
, "):",
1159 func
, func
[0] ? ":" : "",
1160 " runtime check failed: (", warnexpr
, ")", NULL
);
1162 s
= g_strconcat ("(", file
, ":", lstr
, "):",
1163 func
, func
[0] ? ":" : "",
1164 " ", "code should not be reached", NULL
);
1165 g_log (domain
, G_LOG_LEVEL_WARNING
, "%s", s
);
1170 g_assert_warning (const char *log_domain
,
1173 const char *pretty_function
,
1174 const char *expression
)
1179 "file %s: line %d (%s): assertion failed: (%s)",
1187 "file %s: line %d (%s): should not be reached",
1191 _g_log_abort (FALSE
);
1196 * g_test_expect_message:
1197 * @log_domain: (allow-none): the log domain of the message
1198 * @log_level: the log level of the message
1199 * @pattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
1201 * Indicates that a message with the given @log_domain and @log_level,
1202 * with text matching @pattern, is expected to be logged. When this
1203 * message is logged, it will not be printed, and the test case will
1206 * Use g_test_assert_expected_messages() to assert that all
1207 * previously-expected messages have been seen and suppressed.
1209 * You can call this multiple times in a row, if multiple messages are
1210 * expected as a result of a single call. (The messages must appear in
1211 * the same order as the calls to g_test_expect_message().)
1215 * |[<!-- language="C" -->
1216 * // g_main_context_push_thread_default() should fail if the
1217 * // context is already owned by another thread.
1218 * g_test_expect_message (G_LOG_DOMAIN,
1219 * G_LOG_LEVEL_CRITICAL,
1220 * "assertion*acquired_context*failed");
1221 * g_main_context_push_thread_default (bad_context);
1222 * g_test_assert_expected_messages ();
1225 * Note that you cannot use this to test g_error() messages, since
1226 * g_error() intentionally never returns even if the program doesn't
1227 * abort; use g_test_trap_subprocess() in this case.
1229 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1230 * expected via g_test_expect_message() then they will be ignored.
1235 g_test_expect_message (const gchar
*log_domain
,
1236 GLogLevelFlags log_level
,
1237 const gchar
*pattern
)
1239 GTestExpectedMessage
*expected
;
1241 g_return_if_fail (log_level
!= 0);
1242 g_return_if_fail (pattern
!= NULL
);
1243 g_return_if_fail (~log_level
& G_LOG_LEVEL_ERROR
);
1245 expected
= g_new (GTestExpectedMessage
, 1);
1246 expected
->log_domain
= g_strdup (log_domain
);
1247 expected
->log_level
= log_level
;
1248 expected
->pattern
= g_strdup (pattern
);
1250 expected_messages
= g_slist_append (expected_messages
, expected
);
1254 g_test_assert_expected_messages_internal (const char *domain
,
1259 if (expected_messages
)
1261 GTestExpectedMessage
*expected
;
1262 gchar level_prefix
[STRING_BUFFER_SIZE
];
1265 expected
= expected_messages
->data
;
1267 mklevel_prefix (level_prefix
, expected
->log_level
);
1268 message
= g_strdup_printf ("Did not see expected message %s-%s: %s",
1269 expected
->log_domain
? expected
->log_domain
: "**",
1270 level_prefix
, expected
->pattern
);
1271 g_assertion_message (G_LOG_DOMAIN
, file
, line
, func
, message
);
1277 * g_test_assert_expected_messages:
1279 * Asserts that all messages previously indicated via
1280 * g_test_expect_message() have been seen and suppressed.
1282 * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
1283 * expected via g_test_expect_message() then they will be ignored.
1289 _g_log_fallback_handler (const gchar
*log_domain
,
1290 GLogLevelFlags log_level
,
1291 const gchar
*message
,
1292 gpointer unused_data
)
1294 gchar level_prefix
[STRING_BUFFER_SIZE
];
1296 gchar pid_string
[FORMAT_UNSIGNED_BUFSIZE
];
1300 /* we cannot call _any_ GLib functions in this fallback handler,
1301 * which is why we skip UTF-8 conversion, etc.
1302 * since we either recursed or ran out of memory, we're in a pretty
1303 * pathologic situation anyways, what we can do is giving the
1304 * the process ID unconditionally however.
1307 stream
= mklevel_prefix (level_prefix
, log_level
);
1309 message
= "(NULL) message";
1312 format_unsigned (pid_string
, getpid (), 10);
1316 write_string (stream
, "\n");
1318 write_string (stream
, "\n** ");
1321 write_string (stream
, "(process:");
1322 write_string (stream
, pid_string
);
1323 write_string (stream
, "): ");
1328 write_string (stream
, log_domain
);
1329 write_string (stream
, "-");
1331 write_string (stream
, level_prefix
);
1332 write_string (stream
, ": ");
1333 write_string (stream
, message
);
1337 escape_string (GString
*string
)
1339 const char *p
= string
->str
;
1342 while (p
< string
->str
+ string
->len
)
1346 wc
= g_utf8_get_char_validated (p
, -1);
1347 if (wc
== (gunichar
)-1 || wc
== (gunichar
)-2)
1352 pos
= p
- string
->str
;
1354 /* Emit invalid UTF-8 as hex escapes
1356 tmp
= g_strdup_printf ("\\x%02x", (guint
)(guchar
)*p
);
1357 g_string_erase (string
, pos
, 1);
1358 g_string_insert (string
, pos
, tmp
);
1360 p
= string
->str
+ (pos
+ 4); /* Skip over escape sequence */
1367 safe
= *(p
+ 1) == '\n';
1371 safe
= CHAR_IS_SAFE (wc
);
1379 pos
= p
- string
->str
;
1381 /* Largest char we escape is 0x0a, so we don't have to worry
1382 * about 8-digit \Uxxxxyyyy
1384 tmp
= g_strdup_printf ("\\u%04x", wc
);
1385 g_string_erase (string
, pos
, g_utf8_next_char (p
) - p
);
1386 g_string_insert (string
, pos
, tmp
);
1389 p
= string
->str
+ (pos
+ 6); /* Skip over escape sequence */
1392 p
= g_utf8_next_char (p
);
1397 * g_log_default_handler:
1398 * @log_domain: (nullable): the log domain of the message, or %NULL for the
1399 * default "" application domain
1400 * @log_level: the level of the message
1401 * @message: (nullable): the message
1402 * @unused_data: (nullable): data passed from g_log() which is unused
1404 * The default log handler set up by GLib; g_log_set_default_handler()
1405 * allows to install an alternate default log handler.
1406 * This is used if no log handler has been set for the particular log
1407 * domain and log level combination. It outputs the message to stderr
1408 * or stdout and if the log level is fatal it calls abort(). It automatically
1409 * prints a new-line character after the message, so one does not need to be
1410 * manually included in @message.
1412 * The behavior of this log handler can be influenced by a number of
1413 * environment variables:
1415 * - `G_MESSAGES_PREFIXED`: A :-separated list of log levels for which
1416 * messages should be prefixed by the program name and PID of the
1419 * - `G_MESSAGES_DEBUG`: A space-separated list of log domains for
1420 * which debug and informational messages are printed. By default
1421 * these messages are not printed.
1423 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1424 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1428 g_log_default_handler (const gchar
*log_domain
,
1429 GLogLevelFlags log_level
,
1430 const gchar
*message
,
1431 gpointer unused_data
)
1433 gchar level_prefix
[STRING_BUFFER_SIZE
], *string
;
1436 const gchar
*domains
;
1438 if ((log_level
& DEFAULT_LEVELS
) || (log_level
>> G_LOG_LEVEL_USER_SHIFT
))
1441 domains
= g_getenv ("G_MESSAGES_DEBUG");
1442 if (((log_level
& INFO_LEVELS
) == 0) ||
1444 (strcmp (domains
, "all") != 0 && (!log_domain
|| !strstr (domains
, log_domain
))))
1448 /* we can be called externally with recursion for whatever reason */
1449 if (log_level
& G_LOG_FLAG_RECURSION
)
1451 _g_log_fallback_handler (log_domain
, log_level
, message
, unused_data
);
1455 stream
= mklevel_prefix (level_prefix
, log_level
);
1457 gstring
= g_string_new (NULL
);
1458 if (log_level
& ALERT_LEVELS
)
1459 g_string_append (gstring
, "\n");
1461 g_string_append (gstring
, "** ");
1463 if ((g_log_msg_prefix
& (log_level
& G_LOG_LEVEL_MASK
)) == (log_level
& G_LOG_LEVEL_MASK
))
1465 const gchar
*prg_name
= g_get_prgname ();
1468 g_string_append_printf (gstring
, "(process:%lu): ", (gulong
)getpid ());
1470 g_string_append_printf (gstring
, "(%s:%lu): ", prg_name
, (gulong
)getpid ());
1475 g_string_append (gstring
, log_domain
);
1476 g_string_append_c (gstring
, '-');
1478 g_string_append (gstring
, level_prefix
);
1480 g_string_append (gstring
, ": ");
1482 g_string_append (gstring
, "(NULL) message");
1486 const gchar
*charset
;
1488 msg
= g_string_new (message
);
1489 escape_string (msg
);
1491 if (g_get_charset (&charset
))
1492 g_string_append (gstring
, msg
->str
); /* charset is UTF-8 already */
1495 string
= strdup_convert (msg
->str
, charset
);
1496 g_string_append (gstring
, string
);
1500 g_string_free (msg
, TRUE
);
1502 g_string_append (gstring
, "\n");
1504 string
= g_string_free (gstring
, FALSE
);
1506 write_string (stream
, string
);
1511 * g_set_print_handler:
1512 * @func: the new print handler
1514 * Sets the print handler.
1516 * Any messages passed to g_print() will be output via
1517 * the new handler. The default handler simply outputs
1518 * the message to stdout. By providing your own handler
1519 * you can redirect the output, to a GTK+ widget or a
1520 * log file for example.
1522 * Returns: the old print handler
1525 g_set_print_handler (GPrintFunc func
)
1527 GPrintFunc old_print_func
;
1529 g_mutex_lock (&g_messages_lock
);
1530 old_print_func
= glib_print_func
;
1531 glib_print_func
= func
;
1532 g_mutex_unlock (&g_messages_lock
);
1534 return old_print_func
;
1539 * @format: the message format. See the printf() documentation
1540 * @...: the parameters to insert into the format string
1542 * Outputs a formatted message via the print handler.
1543 * The default print handler simply outputs the message to stdout, without
1544 * appending a trailing new-line character. Typically, @format should end with
1545 * its own new-line character.
1547 * g_print() should not be used from within libraries for debugging
1548 * messages, since it may be redirected by applications to special
1549 * purpose message windows or even files. Instead, libraries should
1550 * use g_log(), or the convenience functions g_message(), g_warning()
1554 g_print (const gchar
*format
,
1559 GPrintFunc local_glib_print_func
;
1561 g_return_if_fail (format
!= NULL
);
1563 va_start (args
, format
);
1564 string
= g_strdup_vprintf (format
, args
);
1567 g_mutex_lock (&g_messages_lock
);
1568 local_glib_print_func
= glib_print_func
;
1569 g_mutex_unlock (&g_messages_lock
);
1571 if (local_glib_print_func
)
1572 local_glib_print_func (string
);
1575 const gchar
*charset
;
1577 if (g_get_charset (&charset
))
1578 fputs (string
, stdout
); /* charset is UTF-8 already */
1581 gchar
*lstring
= strdup_convert (string
, charset
);
1583 fputs (lstring
, stdout
);
1592 * g_set_printerr_handler:
1593 * @func: the new error message handler
1595 * Sets the handler for printing error messages.
1597 * Any messages passed to g_printerr() will be output via
1598 * the new handler. The default handler simply outputs the
1599 * message to stderr. By providing your own handler you can
1600 * redirect the output, to a GTK+ widget or a log file for
1603 * Returns: the old error message handler
1606 g_set_printerr_handler (GPrintFunc func
)
1608 GPrintFunc old_printerr_func
;
1610 g_mutex_lock (&g_messages_lock
);
1611 old_printerr_func
= glib_printerr_func
;
1612 glib_printerr_func
= func
;
1613 g_mutex_unlock (&g_messages_lock
);
1615 return old_printerr_func
;
1620 * @format: the message format. See the printf() documentation
1621 * @...: the parameters to insert into the format string
1623 * Outputs a formatted message via the error message handler.
1624 * The default handler simply outputs the message to stderr, without appending
1625 * a trailing new-line character. Typically, @format should end with its own
1626 * new-line character.
1628 * g_printerr() should not be used from within libraries.
1629 * Instead g_log() should be used, or the convenience functions
1630 * g_message(), g_warning() and g_error().
1633 g_printerr (const gchar
*format
,
1638 GPrintFunc local_glib_printerr_func
;
1640 g_return_if_fail (format
!= NULL
);
1642 va_start (args
, format
);
1643 string
= g_strdup_vprintf (format
, args
);
1646 g_mutex_lock (&g_messages_lock
);
1647 local_glib_printerr_func
= glib_printerr_func
;
1648 g_mutex_unlock (&g_messages_lock
);
1650 if (local_glib_printerr_func
)
1651 local_glib_printerr_func (string
);
1654 const gchar
*charset
;
1656 if (g_get_charset (&charset
))
1657 fputs (string
, stderr
); /* charset is UTF-8 already */
1660 gchar
*lstring
= strdup_convert (string
, charset
);
1662 fputs (lstring
, stderr
);
1671 * g_printf_string_upper_bound:
1672 * @format: the format string. See the printf() documentation
1673 * @args: the parameters to be inserted into the format string
1675 * Calculates the maximum space needed to store the output
1676 * of the sprintf() function.
1678 * Returns: the maximum space needed to store the formatted string
1681 g_printf_string_upper_bound (const gchar
*format
,
1685 return _g_vsnprintf (&c
, 1, format
, args
) + 1;