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 = NULL;
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 = NULL;
111 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
112 static GLogFunc default_log_func = g_log_default_handler;
113 static gpointer default_log_data = NULL;
114 static GTestLogFatalFunc fatal_log_func = NULL;
115 static gpointer fatal_log_data;
116 static gboolean g_debug_initialized = FALSE;
119 /* --- functions --- */
122 # include <windows.h>
124 static gboolean win32_keep_fatal_message = FALSE;
126 /* This default message will usually be overwritten. */
127 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
128 * called with huge strings, is it?
130 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
131 static gchar *fatal_msg_ptr = fatal_msg_buf;
139 if (win32_keep_fatal_message)
141 memcpy (fatal_msg_ptr, buf, len);
142 fatal_msg_ptr += len;
147 write (fd, buf, len);
151 #define write(fd, buf, len) dowrite(fd, buf, len)
156 write_string (int fd,
159 write (fd, string, strlen (string));
163 g_messages_prefixed_init (void)
165 static gboolean initialized = FALSE;
172 val = g_getenv ("G_MESSAGES_PREFIXED");
176 const GDebugKey keys[] = {
177 { "error", G_LOG_LEVEL_ERROR },
178 { "critical", G_LOG_LEVEL_CRITICAL },
179 { "warning", G_LOG_LEVEL_WARNING },
180 { "message", G_LOG_LEVEL_MESSAGE },
181 { "info", G_LOG_LEVEL_INFO },
182 { "debug", G_LOG_LEVEL_DEBUG }
185 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
194 G_DEBUG_FATAL_WARNINGS = 1 << 0,
195 G_DEBUG_FATAL_CRITICALS = 1 << 1
200 g_debug_initialized = TRUE;
202 val = g_getenv ("G_DEBUG");
205 const GDebugKey keys[] = {
206 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
207 {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
210 flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
213 if (flags & G_DEBUG_FATAL_WARNINGS)
215 GLogLevelFlags fatal_mask;
217 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
218 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
219 g_log_set_always_fatal (fatal_mask);
222 if (flags & G_DEBUG_FATAL_CRITICALS)
224 GLogLevelFlags fatal_mask;
226 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
227 fatal_mask |= G_LOG_LEVEL_CRITICAL;
228 g_log_set_always_fatal (fatal_mask);
233 g_log_find_domain_L (const gchar *log_domain)
235 register GLogDomain *domain;
237 domain = g_log_domains;
240 if (strcmp (domain->log_domain, log_domain) == 0)
242 domain = domain->next;
248 g_log_domain_new_L (const gchar *log_domain)
250 register GLogDomain *domain;
252 domain = g_new (GLogDomain, 1);
253 domain->log_domain = g_strdup (log_domain);
254 domain->fatal_mask = G_LOG_FATAL_MASK;
255 domain->handlers = NULL;
257 domain->next = g_log_domains;
258 g_log_domains = domain;
264 g_log_domain_check_free_L (GLogDomain *domain)
266 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
267 domain->handlers == NULL)
269 register GLogDomain *last, *work;
273 work = g_log_domains;
279 last->next = domain->next;
281 g_log_domains = domain->next;
282 g_free (domain->log_domain);
293 g_log_domain_get_handler_L (GLogDomain *domain,
294 GLogLevelFlags log_level,
297 if (domain && log_level)
299 register GLogHandler *handler;
301 handler = domain->handlers;
304 if ((handler->log_level & log_level) == log_level)
306 *data = handler->data;
307 return handler->log_func;
309 handler = handler->next;
313 *data = default_log_data;
314 return default_log_func;
318 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
320 GLogLevelFlags old_mask;
322 /* restrict the global mask to levels that are known to glib
323 * since this setting applies to all domains
325 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
326 /* force errors to be fatal */
327 fatal_mask |= G_LOG_LEVEL_ERROR;
328 /* remove bogus flag */
329 fatal_mask &= ~G_LOG_FLAG_FATAL;
331 g_mutex_lock (g_messages_lock);
332 old_mask = g_log_always_fatal;
333 g_log_always_fatal = fatal_mask;
334 g_mutex_unlock (g_messages_lock);
340 g_log_set_fatal_mask (const gchar *log_domain,
341 GLogLevelFlags fatal_mask)
343 GLogLevelFlags old_flags;
344 register GLogDomain *domain;
349 /* force errors to be fatal */
350 fatal_mask |= G_LOG_LEVEL_ERROR;
351 /* remove bogus flag */
352 fatal_mask &= ~G_LOG_FLAG_FATAL;
354 g_mutex_lock (g_messages_lock);
356 domain = g_log_find_domain_L (log_domain);
358 domain = g_log_domain_new_L (log_domain);
359 old_flags = domain->fatal_mask;
361 domain->fatal_mask = fatal_mask;
362 g_log_domain_check_free_L (domain);
364 g_mutex_unlock (g_messages_lock);
370 g_log_set_handler (const gchar *log_domain,
371 GLogLevelFlags log_levels,
375 static guint handler_id = 0;
377 GLogHandler *handler;
379 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
380 g_return_val_if_fail (log_func != NULL, 0);
385 handler = g_new (GLogHandler, 1);
387 g_mutex_lock (g_messages_lock);
389 domain = g_log_find_domain_L (log_domain);
391 domain = g_log_domain_new_L (log_domain);
393 handler->id = ++handler_id;
394 handler->log_level = log_levels;
395 handler->log_func = log_func;
396 handler->data = user_data;
397 handler->next = domain->handlers;
398 domain->handlers = handler;
400 g_mutex_unlock (g_messages_lock);
406 g_log_set_default_handler (GLogFunc log_func,
409 GLogFunc old_log_func;
411 g_mutex_lock (g_messages_lock);
412 old_log_func = default_log_func;
413 default_log_func = log_func;
414 default_log_data = user_data;
415 g_mutex_unlock (g_messages_lock);
421 * g_test_log_set_fatal_handler:
422 * @log_func: the log handler function.
423 * @user_data: data passed to the log handler.
425 * Installs a non-error fatal log handler which can be
426 * used to decide whether log messages which are counted
427 * as fatal abort the program.
429 * The use case here is that you are running a test case
430 * that depends on particular libraries or circumstances
431 * and cannot prevent certain known critical or warning
432 * messages. So you install a handler that compares the
433 * domain and message to precisely not abort in such a case.
435 * Note that the handler is reset at the beginning of
436 * any test case, so you have to set it inside each test
437 * function which needs the special behavior.
439 * This handler has no effect on g_error messages.
444 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
447 g_mutex_lock (g_messages_lock);
448 fatal_log_func = log_func;
449 fatal_log_data = user_data;
450 g_mutex_unlock (g_messages_lock);
454 g_log_remove_handler (const gchar *log_domain,
457 register GLogDomain *domain;
459 g_return_if_fail (handler_id > 0);
464 g_mutex_lock (g_messages_lock);
465 domain = g_log_find_domain_L (log_domain);
468 GLogHandler *work, *last;
471 work = domain->handlers;
474 if (work->id == handler_id)
477 last->next = work->next;
479 domain->handlers = work->next;
480 g_log_domain_check_free_L (domain);
481 g_mutex_unlock (g_messages_lock);
489 g_mutex_unlock (g_messages_lock);
490 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
491 G_STRLOC, handler_id, log_domain);
495 g_logv (const gchar *log_domain,
496 GLogLevelFlags log_level,
500 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
501 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
504 log_level &= G_LOG_LEVEL_MASK;
508 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
510 register GLogLevelFlags test_level;
513 if (log_level & test_level)
515 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
518 GLogLevelFlags domain_fatal_mask;
519 gpointer data = NULL;
520 gboolean masquerade_fatal = FALSE;
523 test_level |= G_LOG_FLAG_FATAL;
525 test_level |= G_LOG_FLAG_RECURSION;
527 /* check recursion and lookup handler */
528 g_mutex_lock (g_messages_lock);
529 domain = g_log_find_domain_L (log_domain ? log_domain : "");
531 test_level |= G_LOG_FLAG_RECURSION;
533 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
534 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
535 test_level |= G_LOG_FLAG_FATAL;
536 if (test_level & G_LOG_FLAG_RECURSION)
537 log_func = _g_log_fallback_handler;
539 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
541 g_mutex_unlock (g_messages_lock);
543 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
545 /* had to defer debug initialization until we can keep track of recursion */
546 if (!(test_level & G_LOG_FLAG_RECURSION) && !g_debug_initialized)
548 GLogLevelFlags orig_test_level = test_level;
551 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
552 test_level |= G_LOG_FLAG_FATAL;
553 if (test_level != orig_test_level)
555 /* need a relookup, not nice, but not too bad either */
556 g_mutex_lock (g_messages_lock);
557 domain = g_log_find_domain_L (log_domain ? log_domain : "");
558 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
560 g_mutex_unlock (g_messages_lock);
564 if (test_level & G_LOG_FLAG_RECURSION)
566 /* we use a stack buffer of fixed size, since we're likely
567 * in an out-of-memory situation
570 gsize size G_GNUC_UNUSED;
573 G_VA_COPY (args2, args1);
574 size = _g_vsnprintf (buffer, 1024, format, args2);
577 log_func (log_domain, test_level, buffer, data);
584 G_VA_COPY (args2, args1);
585 msg = g_strdup_vprintf (format, args2);
588 log_func (log_domain, test_level, msg, data);
590 if ((test_level & G_LOG_FLAG_FATAL)
591 && !(test_level & G_LOG_LEVEL_ERROR))
593 masquerade_fatal = fatal_log_func
594 && !fatal_log_func (log_domain, test_level, msg, data);
600 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
603 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
605 MessageBox (NULL, locale_msg, NULL,
606 MB_ICONERROR|MB_SETFOREGROUND);
607 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
612 if (!(test_level & G_LOG_FLAG_RECURSION))
616 #endif /* !G_OS_WIN32 */
620 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
626 g_log (const gchar *log_domain,
627 GLogLevelFlags log_level,
633 va_start (args, format);
634 g_logv (log_domain, log_level, format, args);
639 g_return_if_fail_warning (const char *log_domain,
640 const char *pretty_function,
641 const char *expression)
644 G_LOG_LEVEL_CRITICAL,
645 "%s: assertion `%s' failed",
651 g_warn_message (const char *domain,
655 const char *warnexpr)
658 g_snprintf (lstr, 32, "%d", line);
660 s = g_strconcat ("(", file, ":", lstr, "):",
661 func, func[0] ? ":" : "",
662 " runtime check failed: (", warnexpr, ")", NULL);
664 s = g_strconcat ("(", file, ":", lstr, "):",
665 func, func[0] ? ":" : "",
666 " ", "code should not be reached", NULL);
667 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
672 g_assert_warning (const char *log_domain,
675 const char *pretty_function,
676 const char *expression)
681 ? "file %s: line %d (%s): assertion failed: (%s)"
682 : "file %s: line %d (%s): should not be reached",
690 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
692 (wc >= 0x80 && wc < 0xa0)))
695 strdup_convert (const gchar *string,
696 const gchar *charset)
698 if (!g_utf8_validate (string, -1, NULL))
700 GString *gstring = g_string_new ("[Invalid UTF-8] ");
703 for (p = (guchar *)string; *p; p++)
705 if (CHAR_IS_SAFE(*p) &&
706 !(*p == '\r' && *(p + 1) != '\n') &&
708 g_string_append_c (gstring, *p);
710 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
713 return g_string_free (gstring, FALSE);
719 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
724 /* Not thread-safe, but doesn't matter if we print the warning twice
726 static gboolean warned = FALSE;
730 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
734 return g_strdup (string);
739 /* For a radix of 8 we need at most 3 output bytes for 1 input
740 * byte. Additionally we might need up to 2 output bytes for the
741 * readix prefix and 1 byte for the trailing NULL.
743 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
746 format_unsigned (gchar *buf,
754 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
756 if (radix != 8 && radix != 10 && radix != 16)
789 /* Again we can't use g_assert; actually this check should _never_ fail. */
790 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
803 buf[i] = c + 'a' - 10;
810 /* string size big enough to hold level prefix */
811 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
813 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
816 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
817 GLogLevelFlags log_level)
819 gboolean to_stdout = TRUE;
821 /* we may not call _any_ GLib functions here */
823 switch (log_level & G_LOG_LEVEL_MASK)
825 case G_LOG_LEVEL_ERROR:
826 strcpy (level_prefix, "ERROR");
829 case G_LOG_LEVEL_CRITICAL:
830 strcpy (level_prefix, "CRITICAL");
833 case G_LOG_LEVEL_WARNING:
834 strcpy (level_prefix, "WARNING");
837 case G_LOG_LEVEL_MESSAGE:
838 strcpy (level_prefix, "Message");
841 case G_LOG_LEVEL_INFO:
842 strcpy (level_prefix, "INFO");
844 case G_LOG_LEVEL_DEBUG:
845 strcpy (level_prefix, "DEBUG");
850 strcpy (level_prefix, "LOG-");
851 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
854 strcpy (level_prefix, "LOG");
857 if (log_level & G_LOG_FLAG_RECURSION)
858 strcat (level_prefix, " (recursed)");
859 if (log_level & ALERT_LEVELS)
860 strcat (level_prefix, " **");
863 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
865 return to_stdout ? 1 : 2;
869 _g_log_fallback_handler (const gchar *log_domain,
870 GLogLevelFlags log_level,
871 const gchar *message,
872 gpointer unused_data)
874 gchar level_prefix[STRING_BUFFER_SIZE];
876 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
880 /* we cannot call _any_ GLib functions in this fallback handler,
881 * which is why we skip UTF-8 conversion, etc.
882 * since we either recursed or ran out of memory, we're in a pretty
883 * pathologic situation anyways, what we can do is giving the
884 * the process ID unconditionally however.
887 fd = mklevel_prefix (level_prefix, log_level);
889 message = "(NULL) message";
892 format_unsigned (pid_string, getpid (), 10);
896 write_string (fd, "\n");
898 write_string (fd, "\n** ");
901 write_string (fd, "(process:");
902 write_string (fd, pid_string);
903 write_string (fd, "): ");
908 write_string (fd, log_domain);
909 write_string (fd, "-");
911 write_string (fd, level_prefix);
912 write_string (fd, ": ");
913 write_string (fd, message);
917 escape_string (GString *string)
919 const char *p = string->str;
922 while (p < string->str + string->len)
926 wc = g_utf8_get_char_validated (p, -1);
927 if (wc == (gunichar)-1 || wc == (gunichar)-2)
932 pos = p - string->str;
934 /* Emit invalid UTF-8 as hex escapes
936 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
937 g_string_erase (string, pos, 1);
938 g_string_insert (string, pos, tmp);
940 p = string->str + (pos + 4); /* Skip over escape sequence */
947 safe = *(p + 1) == '\n';
951 safe = CHAR_IS_SAFE (wc);
959 pos = p - string->str;
961 /* Largest char we escape is 0x0a, so we don't have to worry
962 * about 8-digit \Uxxxxyyyy
964 tmp = g_strdup_printf ("\\u%04x", wc);
965 g_string_erase (string, pos, g_utf8_next_char (p) - p);
966 g_string_insert (string, pos, tmp);
969 p = string->str + (pos + 6); /* Skip over escape sequence */
972 p = g_utf8_next_char (p);
977 g_log_default_handler (const gchar *log_domain,
978 GLogLevelFlags log_level,
979 const gchar *message,
980 gpointer unused_data)
982 gchar level_prefix[STRING_BUFFER_SIZE], *string;
986 /* we can be called externally with recursion for whatever reason */
987 if (log_level & G_LOG_FLAG_RECURSION)
989 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
993 g_messages_prefixed_init ();
995 fd = mklevel_prefix (level_prefix, log_level);
997 gstring = g_string_new (NULL);
998 if (log_level & ALERT_LEVELS)
999 g_string_append (gstring, "\n");
1001 g_string_append (gstring, "** ");
1003 if ((g_log_msg_prefix & log_level) == log_level)
1005 const gchar *prg_name = g_get_prgname ();
1008 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1010 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1015 g_string_append (gstring, log_domain);
1016 g_string_append_c (gstring, '-');
1018 g_string_append (gstring, level_prefix);
1020 g_string_append (gstring, ": ");
1022 g_string_append (gstring, "(NULL) message");
1026 const gchar *charset;
1028 msg = g_string_new (message);
1029 escape_string (msg);
1031 if (g_get_charset (&charset))
1032 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1035 string = strdup_convert (msg->str, charset);
1036 g_string_append (gstring, string);
1040 g_string_free (msg, TRUE);
1042 g_string_append (gstring, "\n");
1044 string = g_string_free (gstring, FALSE);
1046 write_string (fd, string);
1051 * g_set_print_handler:
1052 * @func: the new print handler
1054 * Sets the print handler.
1056 * Any messages passed to g_print() will be output via
1057 * the new handler. The default handler simply outputs
1058 * the message to stdout. By providing your own handler
1059 * you can redirect the output, to a GTK+ widget or a
1060 * log file for example.
1062 * Returns: the old print handler
1065 g_set_print_handler (GPrintFunc func)
1067 GPrintFunc old_print_func;
1069 g_mutex_lock (g_messages_lock);
1070 old_print_func = glib_print_func;
1071 glib_print_func = func;
1072 g_mutex_unlock (g_messages_lock);
1074 return old_print_func;
1079 * @format: the message format. See the printf() documentation
1080 * @...: the parameters to insert into the format string
1082 * Outputs a formatted message via the print handler.
1083 * The default print handler simply outputs the message to stdout.
1085 * g_print() should not be used from within libraries for debugging
1086 * messages, since it may be redirected by applications to special
1087 * purpose message windows or even files. Instead, libraries should
1088 * use g_log(), or the convenience functions g_message(), g_warning()
1092 g_print (const gchar *format,
1097 GPrintFunc local_glib_print_func;
1099 g_return_if_fail (format != NULL);
1101 va_start (args, format);
1102 string = g_strdup_vprintf (format, args);
1105 g_mutex_lock (g_messages_lock);
1106 local_glib_print_func = glib_print_func;
1107 g_mutex_unlock (g_messages_lock);
1109 if (local_glib_print_func)
1110 local_glib_print_func (string);
1113 const gchar *charset;
1115 if (g_get_charset (&charset))
1116 fputs (string, stdout); /* charset is UTF-8 already */
1119 gchar *lstring = strdup_convert (string, charset);
1121 fputs (lstring, stdout);
1130 * g_set_printerr_handler:
1131 * @func: the new error message handler
1133 * Sets the handler for printing error messages.
1135 * Any messages passed to g_printerr() will be output via
1136 * the new handler. The default handler simply outputs the
1137 * message to stderr. By providing your own handler you can
1138 * redirect the output, to a GTK+ widget or a log file for
1141 * Returns: the old error message handler
1144 g_set_printerr_handler (GPrintFunc func)
1146 GPrintFunc old_printerr_func;
1148 g_mutex_lock (g_messages_lock);
1149 old_printerr_func = glib_printerr_func;
1150 glib_printerr_func = func;
1151 g_mutex_unlock (g_messages_lock);
1153 return old_printerr_func;
1158 * @format: the message format. See the printf() documentation
1159 * @...: the parameters to insert into the format string
1161 * Outputs a formatted message via the error message handler.
1162 * The default handler simply outputs the message to stderr.
1164 * g_printerr() should not be used from within libraries.
1165 * Instead g_log() should be used, or the convenience functions
1166 * g_message(), g_warning() and g_error().
1169 g_printerr (const gchar *format,
1174 GPrintFunc local_glib_printerr_func;
1176 g_return_if_fail (format != NULL);
1178 va_start (args, format);
1179 string = g_strdup_vprintf (format, args);
1182 g_mutex_lock (g_messages_lock);
1183 local_glib_printerr_func = glib_printerr_func;
1184 g_mutex_unlock (g_messages_lock);
1186 if (local_glib_printerr_func)
1187 local_glib_printerr_func (string);
1190 const gchar *charset;
1192 if (g_get_charset (&charset))
1193 fputs (string, stderr); /* charset is UTF-8 already */
1196 gchar *lstring = strdup_convert (string, charset);
1198 fputs (lstring, stderr);
1207 g_printf_string_upper_bound (const gchar *format,
1211 return _g_vsnprintf (&c, 1, format, args) + 1;
1215 _g_messages_thread_init_nomessage (void)
1217 g_messages_lock = g_mutex_new ();
1218 g_log_depth = g_private_new (NULL);
1219 g_messages_prefixed_init ();