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 "gbacktrace.h"
67 #include "gprintfint.h"
68 #include "gtestutils.h"
70 #include "gthreadprivate.h"
71 #include "gstrfuncs.h"
75 #include <process.h> /* For getpid() */
77 # define STRICT /* Strict typing, please */
78 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
84 /* --- structures --- */
85 typedef struct _GLogDomain GLogDomain;
86 typedef struct _GLogHandler GLogHandler;
90 GLogLevelFlags fatal_mask;
91 GLogHandler *handlers;
97 GLogLevelFlags log_level;
104 /* --- variables --- */
105 static GMutex g_messages_lock = G_MUTEX_INIT;
106 static GLogDomain *g_log_domains = NULL;
107 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
108 static GPrintFunc glib_print_func = NULL;
109 static GPrintFunc glib_printerr_func = NULL;
110 static GPrivate g_log_depth;
111 static gboolean g_log_depth_initialised;
112 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
113 static GLogFunc default_log_func = g_log_default_handler;
114 static gpointer default_log_data = NULL;
115 static GTestLogFatalFunc fatal_log_func = NULL;
116 static gpointer fatal_log_data;
117 static gboolean g_debug_initialized = FALSE;
120 /* --- functions --- */
123 # include <windows.h>
125 static gboolean win32_keep_fatal_message = FALSE;
127 /* This default message will usually be overwritten. */
128 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
129 * called with huge strings, is it?
131 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
132 static gchar *fatal_msg_ptr = fatal_msg_buf;
140 if (win32_keep_fatal_message)
142 memcpy (fatal_msg_ptr, buf, len);
143 fatal_msg_ptr += len;
148 write (fd, buf, len);
152 #define write(fd, buf, len) dowrite(fd, buf, len)
157 write_string (int fd,
160 write (fd, string, strlen (string));
164 g_messages_prefixed_init (void)
166 static gboolean initialized = FALSE;
173 val = g_getenv ("G_MESSAGES_PREFIXED");
177 const GDebugKey keys[] = {
178 { "error", G_LOG_LEVEL_ERROR },
179 { "critical", G_LOG_LEVEL_CRITICAL },
180 { "warning", G_LOG_LEVEL_WARNING },
181 { "message", G_LOG_LEVEL_MESSAGE },
182 { "info", G_LOG_LEVEL_INFO },
183 { "debug", G_LOG_LEVEL_DEBUG }
186 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
195 G_DEBUG_FATAL_WARNINGS = 1 << 0,
196 G_DEBUG_FATAL_CRITICALS = 1 << 1
201 g_debug_initialized = TRUE;
203 val = g_getenv ("G_DEBUG");
206 const GDebugKey keys[] = {
207 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
208 {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
211 flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
214 if (flags & G_DEBUG_FATAL_WARNINGS)
216 GLogLevelFlags fatal_mask;
218 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
219 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
220 g_log_set_always_fatal (fatal_mask);
223 if (flags & G_DEBUG_FATAL_CRITICALS)
225 GLogLevelFlags fatal_mask;
227 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
228 fatal_mask |= G_LOG_LEVEL_CRITICAL;
229 g_log_set_always_fatal (fatal_mask);
234 g_log_find_domain_L (const gchar *log_domain)
236 register GLogDomain *domain;
238 domain = g_log_domains;
241 if (strcmp (domain->log_domain, log_domain) == 0)
243 domain = domain->next;
249 g_log_domain_new_L (const gchar *log_domain)
251 register GLogDomain *domain;
253 domain = g_new (GLogDomain, 1);
254 domain->log_domain = g_strdup (log_domain);
255 domain->fatal_mask = G_LOG_FATAL_MASK;
256 domain->handlers = NULL;
258 domain->next = g_log_domains;
259 g_log_domains = domain;
265 g_log_domain_check_free_L (GLogDomain *domain)
267 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
268 domain->handlers == NULL)
270 register GLogDomain *last, *work;
274 work = g_log_domains;
280 last->next = domain->next;
282 g_log_domains = domain->next;
283 g_free (domain->log_domain);
294 g_log_domain_get_handler_L (GLogDomain *domain,
295 GLogLevelFlags log_level,
298 if (domain && log_level)
300 register GLogHandler *handler;
302 handler = domain->handlers;
305 if ((handler->log_level & log_level) == log_level)
307 *data = handler->data;
308 return handler->log_func;
310 handler = handler->next;
314 *data = default_log_data;
315 return default_log_func;
319 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
321 GLogLevelFlags old_mask;
323 /* restrict the global mask to levels that are known to glib
324 * since this setting applies to all domains
326 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
327 /* force errors to be fatal */
328 fatal_mask |= G_LOG_LEVEL_ERROR;
329 /* remove bogus flag */
330 fatal_mask &= ~G_LOG_FLAG_FATAL;
332 g_mutex_lock (&g_messages_lock);
333 old_mask = g_log_always_fatal;
334 g_log_always_fatal = fatal_mask;
335 g_mutex_unlock (&g_messages_lock);
341 g_log_set_fatal_mask (const gchar *log_domain,
342 GLogLevelFlags fatal_mask)
344 GLogLevelFlags old_flags;
345 register GLogDomain *domain;
350 /* force errors to be fatal */
351 fatal_mask |= G_LOG_LEVEL_ERROR;
352 /* remove bogus flag */
353 fatal_mask &= ~G_LOG_FLAG_FATAL;
355 g_mutex_lock (&g_messages_lock);
357 domain = g_log_find_domain_L (log_domain);
359 domain = g_log_domain_new_L (log_domain);
360 old_flags = domain->fatal_mask;
362 domain->fatal_mask = fatal_mask;
363 g_log_domain_check_free_L (domain);
365 g_mutex_unlock (&g_messages_lock);
371 g_log_set_handler (const gchar *log_domain,
372 GLogLevelFlags log_levels,
376 static guint handler_id = 0;
378 GLogHandler *handler;
380 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
381 g_return_val_if_fail (log_func != NULL, 0);
386 handler = g_new (GLogHandler, 1);
388 g_mutex_lock (&g_messages_lock);
390 domain = g_log_find_domain_L (log_domain);
392 domain = g_log_domain_new_L (log_domain);
394 handler->id = ++handler_id;
395 handler->log_level = log_levels;
396 handler->log_func = log_func;
397 handler->data = user_data;
398 handler->next = domain->handlers;
399 domain->handlers = handler;
401 g_mutex_unlock (&g_messages_lock);
407 g_log_set_default_handler (GLogFunc log_func,
410 GLogFunc old_log_func;
412 g_mutex_lock (&g_messages_lock);
413 old_log_func = default_log_func;
414 default_log_func = log_func;
415 default_log_data = user_data;
416 g_mutex_unlock (&g_messages_lock);
422 * g_test_log_set_fatal_handler:
423 * @log_func: the log handler function.
424 * @user_data: data passed to the log handler.
426 * Installs a non-error fatal log handler which can be
427 * used to decide whether log messages which are counted
428 * as fatal abort the program.
430 * The use case here is that you are running a test case
431 * that depends on particular libraries or circumstances
432 * and cannot prevent certain known critical or warning
433 * messages. So you install a handler that compares the
434 * domain and message to precisely not abort in such a case.
436 * Note that the handler is reset at the beginning of
437 * any test case, so you have to set it inside each test
438 * function which needs the special behavior.
440 * This handler has no effect on g_error messages.
445 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
448 g_mutex_lock (&g_messages_lock);
449 fatal_log_func = log_func;
450 fatal_log_data = user_data;
451 g_mutex_unlock (&g_messages_lock);
455 g_log_remove_handler (const gchar *log_domain,
458 register GLogDomain *domain;
460 g_return_if_fail (handler_id > 0);
465 g_mutex_lock (&g_messages_lock);
466 domain = g_log_find_domain_L (log_domain);
469 GLogHandler *work, *last;
472 work = domain->handlers;
475 if (work->id == handler_id)
478 last->next = work->next;
480 domain->handlers = work->next;
481 g_log_domain_check_free_L (domain);
482 g_mutex_unlock (&g_messages_lock);
490 g_mutex_unlock (&g_messages_lock);
491 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
492 G_STRLOC, handler_id, log_domain);
496 g_logv (const gchar *log_domain,
497 GLogLevelFlags log_level,
501 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
502 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
505 log_level &= G_LOG_LEVEL_MASK;
509 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
511 register GLogLevelFlags test_level;
514 if (log_level & test_level)
518 GLogLevelFlags domain_fatal_mask;
519 gpointer data = NULL;
520 gboolean masquerade_fatal = FALSE;
524 test_level |= G_LOG_FLAG_FATAL;
526 test_level |= G_LOG_FLAG_RECURSION;
528 /* check recursion and lookup handler */
529 g_mutex_lock (&g_messages_lock);
530 if (!g_log_depth_initialised)
532 g_private_init (&g_log_depth, NULL);
533 g_log_depth_initialised = TRUE;
535 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
536 domain = g_log_find_domain_L (log_domain ? log_domain : "");
538 test_level |= G_LOG_FLAG_RECURSION;
540 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
541 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
542 test_level |= G_LOG_FLAG_FATAL;
543 if (test_level & G_LOG_FLAG_RECURSION)
544 log_func = _g_log_fallback_handler;
546 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
548 g_mutex_unlock (&g_messages_lock);
550 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
552 /* had to defer debug initialization until we can keep track of recursion */
553 if (!(test_level & G_LOG_FLAG_RECURSION) && !g_debug_initialized)
555 GLogLevelFlags orig_test_level = test_level;
558 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
559 test_level |= G_LOG_FLAG_FATAL;
560 if (test_level != orig_test_level)
562 /* need a relookup, not nice, but not too bad either */
563 g_mutex_lock (&g_messages_lock);
564 domain = g_log_find_domain_L (log_domain ? log_domain : "");
565 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
567 g_mutex_unlock (&g_messages_lock);
571 if (test_level & G_LOG_FLAG_RECURSION)
573 /* we use a stack buffer of fixed size, since we're likely
574 * in an out-of-memory situation
577 gsize size G_GNUC_UNUSED;
580 G_VA_COPY (args2, args1);
581 size = _g_vsnprintf (buffer, 1024, format, args2);
584 log_func (log_domain, test_level, buffer, data);
591 G_VA_COPY (args2, args1);
592 msg = g_strdup_vprintf (format, args2);
595 log_func (log_domain, test_level, msg, data);
597 if ((test_level & G_LOG_FLAG_FATAL)
598 && !(test_level & G_LOG_LEVEL_ERROR))
600 masquerade_fatal = fatal_log_func
601 && !fatal_log_func (log_domain, test_level, msg, data);
607 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
610 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
612 MessageBox (NULL, locale_msg, NULL,
613 MB_ICONERROR|MB_SETFOREGROUND);
614 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
619 if (!(test_level & G_LOG_FLAG_RECURSION))
623 #endif /* !G_OS_WIN32 */
627 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
633 g_log (const gchar *log_domain,
634 GLogLevelFlags log_level,
640 va_start (args, format);
641 g_logv (log_domain, log_level, format, args);
646 g_return_if_fail_warning (const char *log_domain,
647 const char *pretty_function,
648 const char *expression)
651 G_LOG_LEVEL_CRITICAL,
652 "%s: assertion `%s' failed",
658 g_warn_message (const char *domain,
662 const char *warnexpr)
665 g_snprintf (lstr, 32, "%d", line);
667 s = g_strconcat ("(", file, ":", lstr, "):",
668 func, func[0] ? ":" : "",
669 " runtime check failed: (", warnexpr, ")", NULL);
671 s = g_strconcat ("(", file, ":", lstr, "):",
672 func, func[0] ? ":" : "",
673 " ", "code should not be reached", NULL);
674 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
679 g_assert_warning (const char *log_domain,
682 const char *pretty_function,
683 const char *expression)
688 ? "file %s: line %d (%s): assertion failed: (%s)"
689 : "file %s: line %d (%s): should not be reached",
697 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
699 (wc >= 0x80 && wc < 0xa0)))
702 strdup_convert (const gchar *string,
703 const gchar *charset)
705 if (!g_utf8_validate (string, -1, NULL))
707 GString *gstring = g_string_new ("[Invalid UTF-8] ");
710 for (p = (guchar *)string; *p; p++)
712 if (CHAR_IS_SAFE(*p) &&
713 !(*p == '\r' && *(p + 1) != '\n') &&
715 g_string_append_c (gstring, *p);
717 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
720 return g_string_free (gstring, FALSE);
726 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
731 /* Not thread-safe, but doesn't matter if we print the warning twice
733 static gboolean warned = FALSE;
737 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
741 return g_strdup (string);
746 /* For a radix of 8 we need at most 3 output bytes for 1 input
747 * byte. Additionally we might need up to 2 output bytes for the
748 * readix prefix and 1 byte for the trailing NULL.
750 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
753 format_unsigned (gchar *buf,
761 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
763 if (radix != 8 && radix != 10 && radix != 16)
796 /* Again we can't use g_assert; actually this check should _never_ fail. */
797 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
810 buf[i] = c + 'a' - 10;
817 /* string size big enough to hold level prefix */
818 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
820 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
823 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
824 GLogLevelFlags log_level)
826 gboolean to_stdout = TRUE;
828 /* we may not call _any_ GLib functions here */
830 switch (log_level & G_LOG_LEVEL_MASK)
832 case G_LOG_LEVEL_ERROR:
833 strcpy (level_prefix, "ERROR");
836 case G_LOG_LEVEL_CRITICAL:
837 strcpy (level_prefix, "CRITICAL");
840 case G_LOG_LEVEL_WARNING:
841 strcpy (level_prefix, "WARNING");
844 case G_LOG_LEVEL_MESSAGE:
845 strcpy (level_prefix, "Message");
848 case G_LOG_LEVEL_INFO:
849 strcpy (level_prefix, "INFO");
851 case G_LOG_LEVEL_DEBUG:
852 strcpy (level_prefix, "DEBUG");
857 strcpy (level_prefix, "LOG-");
858 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
861 strcpy (level_prefix, "LOG");
864 if (log_level & G_LOG_FLAG_RECURSION)
865 strcat (level_prefix, " (recursed)");
866 if (log_level & ALERT_LEVELS)
867 strcat (level_prefix, " **");
870 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
872 return to_stdout ? 1 : 2;
876 _g_log_fallback_handler (const gchar *log_domain,
877 GLogLevelFlags log_level,
878 const gchar *message,
879 gpointer unused_data)
881 gchar level_prefix[STRING_BUFFER_SIZE];
883 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
887 /* we cannot call _any_ GLib functions in this fallback handler,
888 * which is why we skip UTF-8 conversion, etc.
889 * since we either recursed or ran out of memory, we're in a pretty
890 * pathologic situation anyways, what we can do is giving the
891 * the process ID unconditionally however.
894 fd = mklevel_prefix (level_prefix, log_level);
896 message = "(NULL) message";
899 format_unsigned (pid_string, getpid (), 10);
903 write_string (fd, "\n");
905 write_string (fd, "\n** ");
908 write_string (fd, "(process:");
909 write_string (fd, pid_string);
910 write_string (fd, "): ");
915 write_string (fd, log_domain);
916 write_string (fd, "-");
918 write_string (fd, level_prefix);
919 write_string (fd, ": ");
920 write_string (fd, message);
924 escape_string (GString *string)
926 const char *p = string->str;
929 while (p < string->str + string->len)
933 wc = g_utf8_get_char_validated (p, -1);
934 if (wc == (gunichar)-1 || wc == (gunichar)-2)
939 pos = p - string->str;
941 /* Emit invalid UTF-8 as hex escapes
943 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
944 g_string_erase (string, pos, 1);
945 g_string_insert (string, pos, tmp);
947 p = string->str + (pos + 4); /* Skip over escape sequence */
954 safe = *(p + 1) == '\n';
958 safe = CHAR_IS_SAFE (wc);
966 pos = p - string->str;
968 /* Largest char we escape is 0x0a, so we don't have to worry
969 * about 8-digit \Uxxxxyyyy
971 tmp = g_strdup_printf ("\\u%04x", wc);
972 g_string_erase (string, pos, g_utf8_next_char (p) - p);
973 g_string_insert (string, pos, tmp);
976 p = string->str + (pos + 6); /* Skip over escape sequence */
979 p = g_utf8_next_char (p);
984 g_log_default_handler (const gchar *log_domain,
985 GLogLevelFlags log_level,
986 const gchar *message,
987 gpointer unused_data)
989 gchar level_prefix[STRING_BUFFER_SIZE], *string;
993 /* we can be called externally with recursion for whatever reason */
994 if (log_level & G_LOG_FLAG_RECURSION)
996 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1000 g_messages_prefixed_init ();
1002 fd = mklevel_prefix (level_prefix, log_level);
1004 gstring = g_string_new (NULL);
1005 if (log_level & ALERT_LEVELS)
1006 g_string_append (gstring, "\n");
1008 g_string_append (gstring, "** ");
1010 if ((g_log_msg_prefix & log_level) == log_level)
1012 const gchar *prg_name = g_get_prgname ();
1015 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1017 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1022 g_string_append (gstring, log_domain);
1023 g_string_append_c (gstring, '-');
1025 g_string_append (gstring, level_prefix);
1027 g_string_append (gstring, ": ");
1029 g_string_append (gstring, "(NULL) message");
1033 const gchar *charset;
1035 msg = g_string_new (message);
1036 escape_string (msg);
1038 if (g_get_charset (&charset))
1039 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1042 string = strdup_convert (msg->str, charset);
1043 g_string_append (gstring, string);
1047 g_string_free (msg, TRUE);
1049 g_string_append (gstring, "\n");
1051 string = g_string_free (gstring, FALSE);
1053 write_string (fd, string);
1058 * g_set_print_handler:
1059 * @func: the new print handler
1061 * Sets the print handler.
1063 * Any messages passed to g_print() will be output via
1064 * the new handler. The default handler simply outputs
1065 * the message to stdout. By providing your own handler
1066 * you can redirect the output, to a GTK+ widget or a
1067 * log file for example.
1069 * Returns: the old print handler
1072 g_set_print_handler (GPrintFunc func)
1074 GPrintFunc old_print_func;
1076 g_mutex_lock (&g_messages_lock);
1077 old_print_func = glib_print_func;
1078 glib_print_func = func;
1079 g_mutex_unlock (&g_messages_lock);
1081 return old_print_func;
1086 * @format: the message format. See the printf() documentation
1087 * @...: the parameters to insert into the format string
1089 * Outputs a formatted message via the print handler.
1090 * The default print handler simply outputs the message to stdout.
1092 * g_print() should not be used from within libraries for debugging
1093 * messages, since it may be redirected by applications to special
1094 * purpose message windows or even files. Instead, libraries should
1095 * use g_log(), or the convenience functions g_message(), g_warning()
1099 g_print (const gchar *format,
1104 GPrintFunc local_glib_print_func;
1106 g_return_if_fail (format != NULL);
1108 va_start (args, format);
1109 string = g_strdup_vprintf (format, args);
1112 g_mutex_lock (&g_messages_lock);
1113 local_glib_print_func = glib_print_func;
1114 g_mutex_unlock (&g_messages_lock);
1116 if (local_glib_print_func)
1117 local_glib_print_func (string);
1120 const gchar *charset;
1122 if (g_get_charset (&charset))
1123 fputs (string, stdout); /* charset is UTF-8 already */
1126 gchar *lstring = strdup_convert (string, charset);
1128 fputs (lstring, stdout);
1137 * g_set_printerr_handler:
1138 * @func: the new error message handler
1140 * Sets the handler for printing error messages.
1142 * Any messages passed to g_printerr() will be output via
1143 * the new handler. The default handler simply outputs the
1144 * message to stderr. By providing your own handler you can
1145 * redirect the output, to a GTK+ widget or a log file for
1148 * Returns: the old error message handler
1151 g_set_printerr_handler (GPrintFunc func)
1153 GPrintFunc old_printerr_func;
1155 g_mutex_lock (&g_messages_lock);
1156 old_printerr_func = glib_printerr_func;
1157 glib_printerr_func = func;
1158 g_mutex_unlock (&g_messages_lock);
1160 return old_printerr_func;
1165 * @format: the message format. See the printf() documentation
1166 * @...: the parameters to insert into the format string
1168 * Outputs a formatted message via the error message handler.
1169 * The default handler simply outputs the message to stderr.
1171 * g_printerr() should not be used from within libraries.
1172 * Instead g_log() should be used, or the convenience functions
1173 * g_message(), g_warning() and g_error().
1176 g_printerr (const gchar *format,
1181 GPrintFunc local_glib_printerr_func;
1183 g_return_if_fail (format != NULL);
1185 va_start (args, format);
1186 string = g_strdup_vprintf (format, args);
1189 g_mutex_lock (&g_messages_lock);
1190 local_glib_printerr_func = glib_printerr_func;
1191 g_mutex_unlock (&g_messages_lock);
1193 if (local_glib_printerr_func)
1194 local_glib_printerr_func (string);
1197 const gchar *charset;
1199 if (g_get_charset (&charset))
1200 fputs (string, stderr); /* charset is UTF-8 already */
1203 gchar *lstring = strdup_convert (string, charset);
1205 fputs (lstring, stderr);
1214 g_printf_string_upper_bound (const gchar *format,
1218 return _g_vsnprintf (&c, 1, format, args) + 1;
1222 _g_messages_thread_init_nomessage (void)
1224 g_messages_prefixed_init ();