Updated Italian translation
[glib.git] / glib / gmessages.c
blob87a7e6579b6d6980f5e4e9f24fcebc3ad38e1f9c
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/.
26 * MT safe
29 /**
30 * SECTION:warnings
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.
48 #include "config.h"
50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <locale.h>
56 #include <errno.h>
58 #include "glib-init.h"
59 #include "gbacktrace.h"
60 #include "gcharset.h"
61 #include "gconvert.h"
62 #include "genviron.h"
63 #include "gmem.h"
64 #include "gprintfint.h"
65 #include "gtestutils.h"
66 #include "gthread.h"
67 #include "gstrfuncs.h"
68 #include "gstring.h"
69 #include "gpattern.h"
71 #ifdef G_OS_UNIX
72 #include <unistd.h>
73 #endif
75 #ifdef G_OS_WIN32
76 #include <process.h> /* For getpid() */
77 #include <io.h>
78 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
79 # include <windows.h>
80 #endif
83 /**
84 * SECTION:messages
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.
96 /**
97 * G_LOG_DOMAIN:
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:
108 * |[
109 * AM_CPPFLAGS = -DG_LOG_DOMAIN=\"Gtk\"
110 * ]|
114 * G_LOG_FATAL_MASK:
116 * GLib log levels that are considered fatal by default.
120 * GLogFunc:
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.
137 * GLogLevelFlags:
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.
165 * g_message:
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
173 * manually.
177 * g_warning:
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.
197 * g_critical:
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
206 * example.
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
214 * manually.
218 * g_error:
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
236 * manually.
241 * g_info:
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
249 * manually.
251 * Such messages are suppressed by the g_log_default_handler() unless
252 * the G_MESSAGES_DEBUG environment variable is set appropriately.
254 * Since: 2.40
258 * g_debug:
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
266 * manually.
268 * Such messages are suppressed by the g_log_default_handler() unless
269 * the G_MESSAGES_DEBUG environment variable is set appropriately.
271 * Since: 2.6
274 /* --- structures --- */
275 typedef struct _GLogDomain GLogDomain;
276 typedef struct _GLogHandler GLogHandler;
277 struct _GLogDomain
279 gchar *log_domain;
280 GLogLevelFlags fatal_mask;
281 GLogHandler *handlers;
282 GLogDomain *next;
284 struct _GLogHandler
286 guint id;
287 GLogLevelFlags log_level;
288 GLogFunc log_func;
289 gpointer data;
290 GDestroyNotify destroy;
291 GLogHandler *next;
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);
310 static void
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
318 * daemon.
320 _exit (1);
323 if (breakpoint)
324 G_BREAKPOINT ();
325 else
326 abort ();
329 #ifdef G_OS_WIN32
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;
340 #undef write
341 static inline int
342 dowrite (int fd,
343 const void *buf,
344 unsigned int len)
346 if (win32_keep_fatal_message)
348 memcpy (fatal_msg_ptr, buf, len);
349 fatal_msg_ptr += len;
350 *fatal_msg_ptr = 0;
351 return len;
354 write (fd, buf, len);
356 return len;
358 #define write(fd, buf, len) dowrite(fd, buf, len)
360 #endif
362 static void
363 write_string (FILE *stream,
364 const gchar *string)
366 fputs (string, stream);
369 static GLogDomain*
370 g_log_find_domain_L (const gchar *log_domain)
372 GLogDomain *domain;
374 domain = g_log_domains;
375 while (domain)
377 if (strcmp (domain->log_domain, log_domain) == 0)
378 return domain;
379 domain = domain->next;
381 return NULL;
384 static GLogDomain*
385 g_log_domain_new_L (const gchar *log_domain)
387 GLogDomain *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;
397 return domain;
400 static void
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;
408 last = NULL;
410 work = g_log_domains;
411 while (work)
413 if (work == domain)
415 if (last)
416 last->next = domain->next;
417 else
418 g_log_domains = domain->next;
419 g_free (domain->log_domain);
420 g_free (domain);
421 break;
423 last = work;
424 work = last->next;
429 static GLogFunc
430 g_log_domain_get_handler_L (GLogDomain *domain,
431 GLogLevelFlags log_level,
432 gpointer *data)
434 if (domain && log_level)
436 GLogHandler *handler;
438 handler = domain->handlers;
439 while (handler)
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
470 GLogLevelFlags
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);
489 return old_mask;
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
502 GLogLevelFlags
503 g_log_set_fatal_mask (const gchar *log_domain,
504 GLogLevelFlags fatal_mask)
506 GLogLevelFlags old_flags;
507 GLogDomain *domain;
509 if (!log_domain)
510 log_domain = "";
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);
520 if (!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);
529 return old_flags;
533 * g_log_set_handler:
534 * @log_domain: (allow-none): the log domain, or %NULL for the default ""
535 * application domain
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
546 * bit flags.
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
550 * #G_LOG_FLAG_FATAL.
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);
557 * ]|
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);
563 * ]|
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);
569 * ]|
571 * Returns: the id of the new handler
573 guint
574 g_log_set_handler (const gchar *log_domain,
575 GLogLevelFlags log_levels,
576 GLogFunc log_func,
577 gpointer user_data)
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 ""
585 * application domain
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
598 * Since: 2.46
600 guint
601 g_log_set_handler_full (const gchar *log_domain,
602 GLogLevelFlags log_levels,
603 GLogFunc log_func,
604 gpointer user_data,
605 GDestroyNotify destroy)
607 static guint handler_id = 0;
608 GLogDomain *domain;
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);
614 if (!log_domain)
615 log_domain = "";
617 handler = g_new (GLogHandler, 1);
619 g_mutex_lock (&g_messages_lock);
621 domain = g_log_find_domain_L (log_domain);
622 if (!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);
635 return handler_id;
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
650 * Since: 2.6
652 GLogFunc
653 g_log_set_default_handler (GLogFunc log_func,
654 gpointer user_data)
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);
664 return old_log_func;
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.
688 * Since: 2.22
690 void
691 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
692 gpointer user_data)
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.
708 void
709 g_log_remove_handler (const gchar *log_domain,
710 guint handler_id)
712 GLogDomain *domain;
714 g_return_if_fail (handler_id > 0);
716 if (!log_domain)
717 log_domain = "";
719 g_mutex_lock (&g_messages_lock);
720 domain = g_log_find_domain_L (log_domain);
721 if (domain)
723 GLogHandler *work, *last;
725 last = NULL;
726 work = domain->handlers;
727 while (work)
729 if (work->id == handler_id)
731 if (last)
732 last->next = work->next;
733 else
734 domain->handlers = work->next;
735 g_log_domain_check_free_L (domain);
736 g_mutex_unlock (&g_messages_lock);
737 if (work->destroy)
738 work->destroy (work->data);
739 g_free (work);
740 return;
742 last = work;
743 work = last->next;
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') || \
752 (wc == 0x7f) || \
753 (wc >= 0x80 && wc < 0xa0)))
755 static gchar*
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] ");
762 guchar *p;
764 for (p = (guchar *)string; *p; p++)
766 if (CHAR_IS_SAFE(*p) &&
767 !(*p == '\r' && *(p + 1) != '\n') &&
768 *p < 0x80)
769 g_string_append_c (gstring, *p);
770 else
771 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
774 return g_string_free (gstring, FALSE);
776 else
778 GError *err = NULL;
780 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
781 if (result)
782 return result;
783 else
785 /* Not thread-safe, but doesn't matter if we print the warning twice
787 static gboolean warned = FALSE;
788 if (!warned)
790 warned = TRUE;
791 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
793 g_error_free (err);
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)
806 static void
807 format_unsigned (gchar *buf,
808 gulong num,
809 guint radix)
811 gulong tmp;
812 gchar c;
813 gint i, n;
815 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
817 if (radix != 8 && radix != 10 && radix != 16)
819 *buf = '\000';
820 return;
823 if (!num)
825 *buf++ = '0';
826 *buf = '\000';
827 return;
830 if (radix == 16)
832 *buf++ = '0';
833 *buf++ = 'x';
835 else if (radix == 8)
837 *buf++ = '0';
840 n = 0;
841 tmp = num;
842 while (tmp)
844 tmp /= radix;
845 n++;
848 i = n;
850 /* Again we can't use g_assert; actually this check should _never_ fail. */
851 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
853 *buf = '\000';
854 return;
857 while (num)
859 i--;
860 c = (num % radix);
861 if (c < 10)
862 buf[i] = c + '0';
863 else
864 buf[i] = c + 'a' - 10;
865 num /= radix;
868 buf[n] = '\000';
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)
881 static FILE *
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");
893 to_stdout = FALSE;
894 break;
895 case G_LOG_LEVEL_CRITICAL:
896 strcpy (level_prefix, "CRITICAL");
897 to_stdout = FALSE;
898 break;
899 case G_LOG_LEVEL_WARNING:
900 strcpy (level_prefix, "WARNING");
901 to_stdout = FALSE;
902 break;
903 case G_LOG_LEVEL_MESSAGE:
904 strcpy (level_prefix, "Message");
905 to_stdout = FALSE;
906 break;
907 case G_LOG_LEVEL_INFO:
908 strcpy (level_prefix, "INFO");
909 break;
910 case G_LOG_LEVEL_DEBUG:
911 strcpy (level_prefix, "DEBUG");
912 break;
913 default:
914 if (log_level)
916 strcpy (level_prefix, "LOG-");
917 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
919 else
920 strcpy (level_prefix, "LOG");
921 break;
923 if (log_level & G_LOG_FLAG_RECURSION)
924 strcat (level_prefix, " (recursed)");
925 if (log_level & ALERT_LEVELS)
926 strcat (level_prefix, " **");
928 #ifdef G_OS_WIN32
929 if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
930 win32_keep_fatal_message = TRUE;
931 #endif
932 return to_stdout ? stdout : stderr;
935 typedef struct {
936 gchar *log_domain;
937 GLogLevelFlags log_level;
938 gchar *pattern;
939 } GTestExpectedMessage;
941 static GSList *expected_messages = NULL;
944 * g_logv:
945 * @log_domain: (nullable): the log domain, or %NULL for the default ""
946 * application domain
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
958 * manually.
960 void
961 g_logv (const gchar *log_domain,
962 GLogLevelFlags log_level,
963 const gchar *format,
964 va_list args)
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;
969 gint i;
971 log_level &= G_LOG_LEVEL_MASK;
972 if (!log_level)
973 return;
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);
983 msg = buffer;
985 else
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,
997 expected_messages);
998 g_free (expected->log_domain);
999 g_free (expected->pattern);
1000 g_free (expected);
1001 g_free (msg_alloc);
1002 return;
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)
1027 GLogDomain *domain;
1028 GLogFunc log_func;
1029 GLogLevelFlags domain_fatal_mask;
1030 gpointer data = NULL;
1031 gboolean masquerade_fatal = FALSE;
1032 guint depth;
1034 if (was_fatal)
1035 test_level |= G_LOG_FLAG_FATAL;
1036 if (was_recursion)
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 : "");
1043 if (depth)
1044 test_level |= G_LOG_FLAG_RECURSION;
1045 depth++;
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;
1051 else
1052 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
1053 domain = NULL;
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)
1069 #ifdef G_OS_WIN32
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));
1078 #else
1079 _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
1080 #endif /* !G_OS_WIN32 */
1083 depth--;
1084 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
1088 g_free (msg_alloc);
1092 * g_log:
1093 * @log_domain: (nullable): the log domain, usually #G_LOG_DOMAIN, or %NULL
1094 * for the default
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
1107 * manually.
1109 void
1110 g_log (const gchar *log_domain,
1111 GLogLevelFlags log_level,
1112 const gchar *format,
1113 ...)
1115 va_list args;
1117 va_start (args, format);
1118 g_logv (log_domain, log_level, format, args);
1119 va_end (args);
1123 * g_return_if_fail_warning: (skip)
1124 * @log_domain: (nullable):
1125 * @pretty_function:
1126 * @expression: (nullable):
1128 void
1129 g_return_if_fail_warning (const char *log_domain,
1130 const char *pretty_function,
1131 const char *expression)
1133 g_log (log_domain,
1134 G_LOG_LEVEL_CRITICAL,
1135 "%s: assertion '%s' failed",
1136 pretty_function,
1137 expression);
1141 * g_warn_message: (skip)
1142 * @domain: (nullable):
1143 * @file:
1144 * @line:
1145 * @func:
1146 * @warnexpr: (nullable):
1148 void
1149 g_warn_message (const char *domain,
1150 const char *file,
1151 int line,
1152 const char *func,
1153 const char *warnexpr)
1155 char *s, lstr[32];
1156 g_snprintf (lstr, 32, "%d", line);
1157 if (warnexpr)
1158 s = g_strconcat ("(", file, ":", lstr, "):",
1159 func, func[0] ? ":" : "",
1160 " runtime check failed: (", warnexpr, ")", NULL);
1161 else
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);
1166 g_free (s);
1169 void
1170 g_assert_warning (const char *log_domain,
1171 const char *file,
1172 const int line,
1173 const char *pretty_function,
1174 const char *expression)
1176 if (expression)
1177 g_log (log_domain,
1178 G_LOG_LEVEL_ERROR,
1179 "file %s: line %d (%s): assertion failed: (%s)",
1180 file,
1181 line,
1182 pretty_function,
1183 expression);
1184 else
1185 g_log (log_domain,
1186 G_LOG_LEVEL_ERROR,
1187 "file %s: line %d (%s): should not be reached",
1188 file,
1189 line,
1190 pretty_function);
1191 _g_log_abort (FALSE);
1192 abort ();
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
1204 * not abort.
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().)
1213 * For example:
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 ();
1223 * ]|
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.
1232 * Since: 2.34
1234 void
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);
1253 void
1254 g_test_assert_expected_messages_internal (const char *domain,
1255 const char *file,
1256 int line,
1257 const char *func)
1259 if (expected_messages)
1261 GTestExpectedMessage *expected;
1262 gchar level_prefix[STRING_BUFFER_SIZE];
1263 gchar *message;
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);
1272 g_free (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.
1285 * Since: 2.34
1288 void
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];
1295 #ifndef G_OS_WIN32
1296 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1297 #endif
1298 FILE *stream;
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);
1308 if (!message)
1309 message = "(NULL) message";
1311 #ifndef G_OS_WIN32
1312 format_unsigned (pid_string, getpid (), 10);
1313 #endif
1315 if (log_domain)
1316 write_string (stream, "\n");
1317 else
1318 write_string (stream, "\n** ");
1320 #ifndef G_OS_WIN32
1321 write_string (stream, "(process:");
1322 write_string (stream, pid_string);
1323 write_string (stream, "): ");
1324 #endif
1326 if (log_domain)
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);
1336 static void
1337 escape_string (GString *string)
1339 const char *p = string->str;
1340 gunichar wc;
1342 while (p < string->str + string->len)
1344 gboolean safe;
1346 wc = g_utf8_get_char_validated (p, -1);
1347 if (wc == (gunichar)-1 || wc == (gunichar)-2)
1349 gchar *tmp;
1350 guint pos;
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 */
1362 g_free (tmp);
1363 continue;
1365 if (wc == '\r')
1367 safe = *(p + 1) == '\n';
1369 else
1371 safe = CHAR_IS_SAFE (wc);
1374 if (!safe)
1376 gchar *tmp;
1377 guint pos;
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);
1387 g_free (tmp);
1389 p = string->str + (pos + 6); /* Skip over escape sequence */
1391 else
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
1417 * aplication.
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
1425 * the rest.
1427 void
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;
1434 GString *gstring;
1435 FILE *stream;
1436 const gchar *domains;
1438 if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
1439 goto emit;
1441 domains = g_getenv ("G_MESSAGES_DEBUG");
1442 if (((log_level & INFO_LEVELS) == 0) ||
1443 domains == NULL ||
1444 (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
1445 return;
1447 emit:
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);
1452 return;
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");
1460 if (!log_domain)
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 ();
1467 if (!prg_name)
1468 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1469 else
1470 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1473 if (log_domain)
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, ": ");
1481 if (!message)
1482 g_string_append (gstring, "(NULL) message");
1483 else
1485 GString *msg;
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 */
1493 else
1495 string = strdup_convert (msg->str, charset);
1496 g_string_append (gstring, string);
1497 g_free (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);
1507 g_free (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
1524 GPrintFunc
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;
1538 * g_print:
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()
1551 * and g_error().
1553 void
1554 g_print (const gchar *format,
1555 ...)
1557 va_list args;
1558 gchar *string;
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);
1565 va_end (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);
1573 else
1575 const gchar *charset;
1577 if (g_get_charset (&charset))
1578 fputs (string, stdout); /* charset is UTF-8 already */
1579 else
1581 gchar *lstring = strdup_convert (string, charset);
1583 fputs (lstring, stdout);
1584 g_free (lstring);
1586 fflush (stdout);
1588 g_free (string);
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
1601 * example.
1603 * Returns: the old error message handler
1605 GPrintFunc
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;
1619 * g_printerr:
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().
1632 void
1633 g_printerr (const gchar *format,
1634 ...)
1636 va_list args;
1637 gchar *string;
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);
1644 va_end (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);
1652 else
1654 const gchar *charset;
1656 if (g_get_charset (&charset))
1657 fputs (string, stderr); /* charset is UTF-8 already */
1658 else
1660 gchar *lstring = strdup_convert (string, charset);
1662 fputs (lstring, stderr);
1663 g_free (lstring);
1665 fflush (stderr);
1667 g_free (string);
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
1680 gsize
1681 g_printf_string_upper_bound (const gchar *format,
1682 va_list args)
1684 gchar c;
1685 return _g_vsnprintf (&c, 1, format, args) + 1;