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 "gstrfuncs.h"
74 #include <process.h> /* For getpid() */
76 # define STRICT /* Strict typing, please */
77 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
83 /* --- structures --- */
84 typedef struct _GLogDomain GLogDomain;
85 typedef struct _GLogHandler GLogHandler;
89 GLogLevelFlags fatal_mask;
90 GLogHandler *handlers;
96 GLogLevelFlags log_level;
103 /* --- variables --- */
104 static GMutex g_messages_lock;
105 static GLogDomain *g_log_domains = NULL;
106 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
107 static GPrintFunc glib_print_func = NULL;
108 static GPrintFunc glib_printerr_func = NULL;
109 static GPrivate g_log_depth;
110 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
111 static GLogFunc default_log_func = g_log_default_handler;
112 static gpointer default_log_data = NULL;
113 static GTestLogFatalFunc fatal_log_func = NULL;
114 static gpointer fatal_log_data;
115 static gboolean g_debug_initialized = FALSE;
118 /* --- functions --- */
121 # include <windows.h>
123 static gboolean win32_keep_fatal_message = FALSE;
125 /* This default message will usually be overwritten. */
126 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
127 * called with huge strings, is it?
129 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
130 static gchar *fatal_msg_ptr = fatal_msg_buf;
138 if (win32_keep_fatal_message)
140 memcpy (fatal_msg_ptr, buf, len);
141 fatal_msg_ptr += len;
146 write (fd, buf, len);
150 #define write(fd, buf, len) dowrite(fd, buf, len)
155 write_string (int fd,
158 write (fd, string, strlen (string));
162 g_messages_prefixed_init (void)
164 static gboolean initialized = FALSE;
171 val = g_getenv ("G_MESSAGES_PREFIXED");
175 const GDebugKey keys[] = {
176 { "error", G_LOG_LEVEL_ERROR },
177 { "critical", G_LOG_LEVEL_CRITICAL },
178 { "warning", G_LOG_LEVEL_WARNING },
179 { "message", G_LOG_LEVEL_MESSAGE },
180 { "info", G_LOG_LEVEL_INFO },
181 { "debug", G_LOG_LEVEL_DEBUG }
184 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
190 g_parse_debug_envvar (const gchar *envvar,
191 const GDebugKey *keys,
197 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
200 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
205 value = getenv (envvar);
208 return g_parse_debug_string (value, keys, n_keys);
214 const GDebugKey keys[] = {
215 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
216 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
218 GLogLevelFlags flags;
220 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys));
222 g_mutex_lock (&g_messages_lock);
223 g_log_always_fatal |= flags;
224 g_mutex_unlock (&g_messages_lock);
226 g_debug_initialized = TRUE;
230 g_log_find_domain_L (const gchar *log_domain)
232 register GLogDomain *domain;
234 domain = g_log_domains;
237 if (strcmp (domain->log_domain, log_domain) == 0)
239 domain = domain->next;
245 g_log_domain_new_L (const gchar *log_domain)
247 register GLogDomain *domain;
249 domain = g_new (GLogDomain, 1);
250 domain->log_domain = g_strdup (log_domain);
251 domain->fatal_mask = G_LOG_FATAL_MASK;
252 domain->handlers = NULL;
254 domain->next = g_log_domains;
255 g_log_domains = domain;
261 g_log_domain_check_free_L (GLogDomain *domain)
263 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
264 domain->handlers == NULL)
266 register GLogDomain *last, *work;
270 work = g_log_domains;
276 last->next = domain->next;
278 g_log_domains = domain->next;
279 g_free (domain->log_domain);
290 g_log_domain_get_handler_L (GLogDomain *domain,
291 GLogLevelFlags log_level,
294 if (domain && log_level)
296 register GLogHandler *handler;
298 handler = domain->handlers;
301 if ((handler->log_level & log_level) == log_level)
303 *data = handler->data;
304 return handler->log_func;
306 handler = handler->next;
310 *data = default_log_data;
311 return default_log_func;
315 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
317 GLogLevelFlags old_mask;
319 /* restrict the global mask to levels that are known to glib
320 * since this setting applies to all domains
322 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
323 /* force errors to be fatal */
324 fatal_mask |= G_LOG_LEVEL_ERROR;
325 /* remove bogus flag */
326 fatal_mask &= ~G_LOG_FLAG_FATAL;
328 g_mutex_lock (&g_messages_lock);
329 old_mask = g_log_always_fatal;
330 g_log_always_fatal = fatal_mask;
331 g_mutex_unlock (&g_messages_lock);
337 g_log_set_fatal_mask (const gchar *log_domain,
338 GLogLevelFlags fatal_mask)
340 GLogLevelFlags old_flags;
341 register GLogDomain *domain;
346 /* force errors to be fatal */
347 fatal_mask |= G_LOG_LEVEL_ERROR;
348 /* remove bogus flag */
349 fatal_mask &= ~G_LOG_FLAG_FATAL;
351 g_mutex_lock (&g_messages_lock);
353 domain = g_log_find_domain_L (log_domain);
355 domain = g_log_domain_new_L (log_domain);
356 old_flags = domain->fatal_mask;
358 domain->fatal_mask = fatal_mask;
359 g_log_domain_check_free_L (domain);
361 g_mutex_unlock (&g_messages_lock);
367 g_log_set_handler (const gchar *log_domain,
368 GLogLevelFlags log_levels,
372 static guint handler_id = 0;
374 GLogHandler *handler;
376 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
377 g_return_val_if_fail (log_func != NULL, 0);
382 handler = g_new (GLogHandler, 1);
384 g_mutex_lock (&g_messages_lock);
386 domain = g_log_find_domain_L (log_domain);
388 domain = g_log_domain_new_L (log_domain);
390 handler->id = ++handler_id;
391 handler->log_level = log_levels;
392 handler->log_func = log_func;
393 handler->data = user_data;
394 handler->next = domain->handlers;
395 domain->handlers = handler;
397 g_mutex_unlock (&g_messages_lock);
403 g_log_set_default_handler (GLogFunc log_func,
406 GLogFunc old_log_func;
408 g_mutex_lock (&g_messages_lock);
409 old_log_func = default_log_func;
410 default_log_func = log_func;
411 default_log_data = user_data;
412 g_mutex_unlock (&g_messages_lock);
418 * g_test_log_set_fatal_handler:
419 * @log_func: the log handler function.
420 * @user_data: data passed to the log handler.
422 * Installs a non-error fatal log handler which can be
423 * used to decide whether log messages which are counted
424 * as fatal abort the program.
426 * The use case here is that you are running a test case
427 * that depends on particular libraries or circumstances
428 * and cannot prevent certain known critical or warning
429 * messages. So you install a handler that compares the
430 * domain and message to precisely not abort in such a case.
432 * Note that the handler is reset at the beginning of
433 * any test case, so you have to set it inside each test
434 * function which needs the special behavior.
436 * This handler has no effect on g_error messages.
441 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
444 g_mutex_lock (&g_messages_lock);
445 fatal_log_func = log_func;
446 fatal_log_data = user_data;
447 g_mutex_unlock (&g_messages_lock);
451 g_log_remove_handler (const gchar *log_domain,
454 register GLogDomain *domain;
456 g_return_if_fail (handler_id > 0);
461 g_mutex_lock (&g_messages_lock);
462 domain = g_log_find_domain_L (log_domain);
465 GLogHandler *work, *last;
468 work = domain->handlers;
471 if (work->id == handler_id)
474 last->next = work->next;
476 domain->handlers = work->next;
477 g_log_domain_check_free_L (domain);
478 g_mutex_unlock (&g_messages_lock);
486 g_mutex_unlock (&g_messages_lock);
487 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
488 G_STRLOC, handler_id, log_domain);
492 g_logv (const gchar *log_domain,
493 GLogLevelFlags log_level,
497 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
498 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
501 if (!g_debug_initialized)
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)
517 GLogLevelFlags domain_fatal_mask;
518 gpointer data = NULL;
519 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 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
530 domain = g_log_find_domain_L (log_domain ? log_domain : "");
532 test_level |= G_LOG_FLAG_RECURSION;
534 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
535 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
536 test_level |= G_LOG_FLAG_FATAL;
537 if (test_level & G_LOG_FLAG_RECURSION)
538 log_func = _g_log_fallback_handler;
540 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
542 g_mutex_unlock (&g_messages_lock);
544 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
547 if (test_level & G_LOG_FLAG_RECURSION)
549 /* we use a stack buffer of fixed size, since we're likely
550 * in an out-of-memory situation
553 gsize size G_GNUC_UNUSED;
556 G_VA_COPY (args2, args1);
557 size = _g_vsnprintf (buffer, 1024, format, args2);
560 log_func (log_domain, test_level, buffer, data);
567 G_VA_COPY (args2, args1);
568 msg = g_strdup_vprintf (format, args2);
571 log_func (log_domain, test_level, msg, data);
573 if ((test_level & G_LOG_FLAG_FATAL)
574 && !(test_level & G_LOG_LEVEL_ERROR))
576 masquerade_fatal = fatal_log_func
577 && !fatal_log_func (log_domain, test_level, msg, data);
583 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
586 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
588 MessageBox (NULL, locale_msg, NULL,
589 MB_ICONERROR|MB_SETFOREGROUND);
590 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
595 if (!(test_level & G_LOG_FLAG_RECURSION))
599 #endif /* !G_OS_WIN32 */
603 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
609 g_log (const gchar *log_domain,
610 GLogLevelFlags log_level,
616 va_start (args, format);
617 g_logv (log_domain, log_level, format, args);
622 g_return_if_fail_warning (const char *log_domain,
623 const char *pretty_function,
624 const char *expression)
627 G_LOG_LEVEL_CRITICAL,
628 "%s: assertion `%s' failed",
634 g_warn_message (const char *domain,
638 const char *warnexpr)
641 g_snprintf (lstr, 32, "%d", line);
643 s = g_strconcat ("(", file, ":", lstr, "):",
644 func, func[0] ? ":" : "",
645 " runtime check failed: (", warnexpr, ")", NULL);
647 s = g_strconcat ("(", file, ":", lstr, "):",
648 func, func[0] ? ":" : "",
649 " ", "code should not be reached", NULL);
650 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
655 g_assert_warning (const char *log_domain,
658 const char *pretty_function,
659 const char *expression)
664 ? "file %s: line %d (%s): assertion failed: (%s)"
665 : "file %s: line %d (%s): should not be reached",
673 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
675 (wc >= 0x80 && wc < 0xa0)))
678 strdup_convert (const gchar *string,
679 const gchar *charset)
681 if (!g_utf8_validate (string, -1, NULL))
683 GString *gstring = g_string_new ("[Invalid UTF-8] ");
686 for (p = (guchar *)string; *p; p++)
688 if (CHAR_IS_SAFE(*p) &&
689 !(*p == '\r' && *(p + 1) != '\n') &&
691 g_string_append_c (gstring, *p);
693 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
696 return g_string_free (gstring, FALSE);
702 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
707 /* Not thread-safe, but doesn't matter if we print the warning twice
709 static gboolean warned = FALSE;
713 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
717 return g_strdup (string);
722 /* For a radix of 8 we need at most 3 output bytes for 1 input
723 * byte. Additionally we might need up to 2 output bytes for the
724 * readix prefix and 1 byte for the trailing NULL.
726 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
729 format_unsigned (gchar *buf,
737 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
739 if (radix != 8 && radix != 10 && radix != 16)
772 /* Again we can't use g_assert; actually this check should _never_ fail. */
773 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
786 buf[i] = c + 'a' - 10;
793 /* string size big enough to hold level prefix */
794 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
796 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
799 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
800 GLogLevelFlags log_level)
802 gboolean to_stdout = TRUE;
804 /* we may not call _any_ GLib functions here */
806 switch (log_level & G_LOG_LEVEL_MASK)
808 case G_LOG_LEVEL_ERROR:
809 strcpy (level_prefix, "ERROR");
812 case G_LOG_LEVEL_CRITICAL:
813 strcpy (level_prefix, "CRITICAL");
816 case G_LOG_LEVEL_WARNING:
817 strcpy (level_prefix, "WARNING");
820 case G_LOG_LEVEL_MESSAGE:
821 strcpy (level_prefix, "Message");
824 case G_LOG_LEVEL_INFO:
825 strcpy (level_prefix, "INFO");
827 case G_LOG_LEVEL_DEBUG:
828 strcpy (level_prefix, "DEBUG");
833 strcpy (level_prefix, "LOG-");
834 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
837 strcpy (level_prefix, "LOG");
840 if (log_level & G_LOG_FLAG_RECURSION)
841 strcat (level_prefix, " (recursed)");
842 if (log_level & ALERT_LEVELS)
843 strcat (level_prefix, " **");
846 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
848 return to_stdout ? 1 : 2;
852 _g_log_fallback_handler (const gchar *log_domain,
853 GLogLevelFlags log_level,
854 const gchar *message,
855 gpointer unused_data)
857 gchar level_prefix[STRING_BUFFER_SIZE];
859 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
863 /* we cannot call _any_ GLib functions in this fallback handler,
864 * which is why we skip UTF-8 conversion, etc.
865 * since we either recursed or ran out of memory, we're in a pretty
866 * pathologic situation anyways, what we can do is giving the
867 * the process ID unconditionally however.
870 fd = mklevel_prefix (level_prefix, log_level);
872 message = "(NULL) message";
875 format_unsigned (pid_string, getpid (), 10);
879 write_string (fd, "\n");
881 write_string (fd, "\n** ");
884 write_string (fd, "(process:");
885 write_string (fd, pid_string);
886 write_string (fd, "): ");
891 write_string (fd, log_domain);
892 write_string (fd, "-");
894 write_string (fd, level_prefix);
895 write_string (fd, ": ");
896 write_string (fd, message);
900 escape_string (GString *string)
902 const char *p = string->str;
905 while (p < string->str + string->len)
909 wc = g_utf8_get_char_validated (p, -1);
910 if (wc == (gunichar)-1 || wc == (gunichar)-2)
915 pos = p - string->str;
917 /* Emit invalid UTF-8 as hex escapes
919 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
920 g_string_erase (string, pos, 1);
921 g_string_insert (string, pos, tmp);
923 p = string->str + (pos + 4); /* Skip over escape sequence */
930 safe = *(p + 1) == '\n';
934 safe = CHAR_IS_SAFE (wc);
942 pos = p - string->str;
944 /* Largest char we escape is 0x0a, so we don't have to worry
945 * about 8-digit \Uxxxxyyyy
947 tmp = g_strdup_printf ("\\u%04x", wc);
948 g_string_erase (string, pos, g_utf8_next_char (p) - p);
949 g_string_insert (string, pos, tmp);
952 p = string->str + (pos + 6); /* Skip over escape sequence */
955 p = g_utf8_next_char (p);
960 g_log_default_handler (const gchar *log_domain,
961 GLogLevelFlags log_level,
962 const gchar *message,
963 gpointer unused_data)
965 gchar level_prefix[STRING_BUFFER_SIZE], *string;
969 /* we can be called externally with recursion for whatever reason */
970 if (log_level & G_LOG_FLAG_RECURSION)
972 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
976 g_messages_prefixed_init ();
978 fd = mklevel_prefix (level_prefix, log_level);
980 gstring = g_string_new (NULL);
981 if (log_level & ALERT_LEVELS)
982 g_string_append (gstring, "\n");
984 g_string_append (gstring, "** ");
986 if ((g_log_msg_prefix & log_level) == log_level)
988 const gchar *prg_name = g_get_prgname ();
991 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
993 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
998 g_string_append (gstring, log_domain);
999 g_string_append_c (gstring, '-');
1001 g_string_append (gstring, level_prefix);
1003 g_string_append (gstring, ": ");
1005 g_string_append (gstring, "(NULL) message");
1009 const gchar *charset;
1011 msg = g_string_new (message);
1012 escape_string (msg);
1014 if (g_get_charset (&charset))
1015 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1018 string = strdup_convert (msg->str, charset);
1019 g_string_append (gstring, string);
1023 g_string_free (msg, TRUE);
1025 g_string_append (gstring, "\n");
1027 string = g_string_free (gstring, FALSE);
1029 write_string (fd, string);
1034 * g_set_print_handler:
1035 * @func: the new print handler
1037 * Sets the print handler.
1039 * Any messages passed to g_print() will be output via
1040 * the new handler. The default handler simply outputs
1041 * the message to stdout. By providing your own handler
1042 * you can redirect the output, to a GTK+ widget or a
1043 * log file for example.
1045 * Returns: the old print handler
1048 g_set_print_handler (GPrintFunc func)
1050 GPrintFunc old_print_func;
1052 g_mutex_lock (&g_messages_lock);
1053 old_print_func = glib_print_func;
1054 glib_print_func = func;
1055 g_mutex_unlock (&g_messages_lock);
1057 return old_print_func;
1062 * @format: the message format. See the printf() documentation
1063 * @...: the parameters to insert into the format string
1065 * Outputs a formatted message via the print handler.
1066 * The default print handler simply outputs the message to stdout.
1068 * g_print() should not be used from within libraries for debugging
1069 * messages, since it may be redirected by applications to special
1070 * purpose message windows or even files. Instead, libraries should
1071 * use g_log(), or the convenience functions g_message(), g_warning()
1075 g_print (const gchar *format,
1080 GPrintFunc local_glib_print_func;
1082 g_return_if_fail (format != NULL);
1084 va_start (args, format);
1085 string = g_strdup_vprintf (format, args);
1088 g_mutex_lock (&g_messages_lock);
1089 local_glib_print_func = glib_print_func;
1090 g_mutex_unlock (&g_messages_lock);
1092 if (local_glib_print_func)
1093 local_glib_print_func (string);
1096 const gchar *charset;
1098 if (g_get_charset (&charset))
1099 fputs (string, stdout); /* charset is UTF-8 already */
1102 gchar *lstring = strdup_convert (string, charset);
1104 fputs (lstring, stdout);
1113 * g_set_printerr_handler:
1114 * @func: the new error message handler
1116 * Sets the handler for printing error messages.
1118 * Any messages passed to g_printerr() will be output via
1119 * the new handler. The default handler simply outputs the
1120 * message to stderr. By providing your own handler you can
1121 * redirect the output, to a GTK+ widget or a log file for
1124 * Returns: the old error message handler
1127 g_set_printerr_handler (GPrintFunc func)
1129 GPrintFunc old_printerr_func;
1131 g_mutex_lock (&g_messages_lock);
1132 old_printerr_func = glib_printerr_func;
1133 glib_printerr_func = func;
1134 g_mutex_unlock (&g_messages_lock);
1136 return old_printerr_func;
1141 * @format: the message format. See the printf() documentation
1142 * @...: the parameters to insert into the format string
1144 * Outputs a formatted message via the error message handler.
1145 * The default handler simply outputs the message to stderr.
1147 * g_printerr() should not be used from within libraries.
1148 * Instead g_log() should be used, or the convenience functions
1149 * g_message(), g_warning() and g_error().
1152 g_printerr (const gchar *format,
1157 GPrintFunc local_glib_printerr_func;
1159 g_return_if_fail (format != NULL);
1161 va_start (args, format);
1162 string = g_strdup_vprintf (format, args);
1165 g_mutex_lock (&g_messages_lock);
1166 local_glib_printerr_func = glib_printerr_func;
1167 g_mutex_unlock (&g_messages_lock);
1169 if (local_glib_printerr_func)
1170 local_glib_printerr_func (string);
1173 const gchar *charset;
1175 if (g_get_charset (&charset))
1176 fputs (string, stderr); /* charset is UTF-8 already */
1179 gchar *lstring = strdup_convert (string, charset);
1181 fputs (lstring, stderr);
1190 * g_printf_string_upper_bound:
1191 * @format: the format string. See the printf() documentation
1192 * @args: the parameters to be inserted into the format string
1194 * Calculates the maximum space needed to store the output
1195 * of the sprintf() function.
1197 * Returns: the maximum space needed to store the formatted string
1200 g_printf_string_upper_bound (const gchar *format,
1204 return _g_vsnprintf (&c, 1, format, args) + 1;
1208 _g_messages_thread_init_nomessage (void)
1210 g_messages_prefixed_init ();