GLib 2.33.14
[platform/upstream/glib.git] / glib / gmessages.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 /*
28  * MT safe
29  */
30
31 /**
32  * SECTION:warnings
33  * @Title: Message Output and Debugging Functions
34  * @Short_description: functions to output messages and help debug applications
35  *
36  * These functions provide support for outputting messages.
37  *
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.
47  */
48
49 #include "config.h"
50
51 #include <stdlib.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <string.h>
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58 #include <signal.h>
59 #include <locale.h>
60 #include <errno.h>
61
62 #include "gmessages.h"
63
64 #include "glib-init.h"
65 #include "gbacktrace.h"
66 #include "gcharset.h"
67 #include "gconvert.h"
68 #include "genviron.h"
69 #include "gmem.h"
70 #include "gprintfint.h"
71 #include "gtestutils.h"
72 #include "gthread.h"
73 #include "gstrfuncs.h"
74 #include "gstring.h"
75 #include "gpattern.h"
76
77 #ifdef G_OS_WIN32
78 #include <process.h>            /* For getpid() */
79 #include <io.h>
80 #  define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
81 #  include <windows.h>
82 #endif
83
84
85 /**
86  * SECTION:messages
87  * @title: Message Logging
88  * @short_description: versatile support for logging messages
89  *     with different levels of importance
90  *
91  * These functions provide support for logging error messages
92  * or messages used for debugging.
93  *
94  * There are several built-in levels of messages, defined in
95  * #GLogLevelFlags. These can be extended with user-defined levels.
96  */
97
98 /**
99  * G_LOG_DOMAIN:
100  *
101  * Defines the log domain.
102  *
103  * For applications, this is typically left as the default %NULL
104  * (or "") domain. Libraries should define this so that any messages
105  * which they log can be differentiated from messages from other
106  * libraries and application code. But be careful not to define
107  * it in any public header files.
108  *
109  * For example, GTK+ uses this in its Makefile.am:
110  * |[
111  * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
112  * ]|
113  */
114
115 /**
116  * G_LOG_FATAL_MASK:
117  *
118  * GLib log levels that are considered fatal by default.
119  */
120
121 /**
122  * GLogFunc:
123  * @log_domain: the log domain of the message
124  * @log_level: the log level of the message (including the
125  *     fatal and recursion flags)
126  * @message: the message to process
127  * @user_data: user data, set in g_log_set_handler()
128  *
129  * Specifies the prototype of log handler functions.
130  */
131
132 /**
133  * GLogLevelFlags:
134  * @G_LOG_FLAG_RECURSION: internal flag
135  * @G_LOG_FLAG_FATAL: internal flag
136  * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
137  *     This level is also used for messages produced by g_assert().
138  * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
139  *     This level is also used for messages produced by g_return_if_fail()
140  *     and g_return_val_if_fail().
141  * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
142  * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
143  * @G_LOG_LEVEL_INFO: log level for informational messages
144  * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
145  * @G_LOG_LEVEL_MASK: a mask including all log levels
146  *
147  * Flags specifying the level of log messages.
148  *
149  * It is possible to change how GLib treats messages of the various
150  * levels using g_log_set_handler() and g_log_set_fatal_mask().
151  */
152
153 /**
154  * g_message:
155  * @...: format string, followed by parameters to insert
156  *     into the format string (as with printf())
157  *
158  * A convenience function/macro to log a normal message.
159  */
160
161 /**
162  * g_warning:
163  * @...: format string, followed by parameters to insert
164  *     into the format string (as with printf())
165  *
166  * A convenience function/macro to log a warning message.
167  *
168  * You can make warnings fatal at runtime by setting the
169  * <envar>G_DEBUG</envar> environment variable (see
170  * <ulink url="glib-running.html">Running GLib Applications</ulink>).
171  */
172
173 /**
174  * g_critical:
175  * @...: format string, followed by parameters to insert
176  *     into the format string (as with printf())
177  *
178  * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
179  * It's more or less application-defined what constitutes
180  * a critical vs. a regular warning. You could call
181  * g_log_set_always_fatal() to make critical warnings exit
182  * the program, then use g_critical() for fatal errors, for
183  * example.
184  *
185  * You can also make critical warnings fatal at runtime by
186  * setting the <envar>G_DEBUG</envar> environment variable (see
187  * <ulink url="glib-running.html">Running GLib Applications</ulink>).
188  */
189
190 /**
191  * g_error:
192  * @...: format string, followed by parameters to insert
193  *     into the format string (as with printf())
194  *
195  * A convenience function/macro to log an error message.
196  *
197  * Error messages are always fatal, resulting in a call to
198  * abort() to terminate the application. This function will
199  * result in a core dump; don't use it for errors you expect.
200  * Using this function indicates a bug in your program, i.e.
201  * an assertion failure.
202  *
203  */
204
205 /**
206  * g_debug:
207  * @...: format string, followed by parameters to insert
208  *     into the format string (as with printf())
209  *
210  * A convenience function/macro to log a debug message.
211  *
212  * Since: 2.6
213  */
214
215 /* --- structures --- */
216 typedef struct _GLogDomain      GLogDomain;
217 typedef struct _GLogHandler     GLogHandler;
218 struct _GLogDomain
219 {
220   gchar         *log_domain;
221   GLogLevelFlags fatal_mask;
222   GLogHandler   *handlers;
223   GLogDomain    *next;
224 };
225 struct _GLogHandler
226 {
227   guint          id;
228   GLogLevelFlags log_level;
229   GLogFunc       log_func;
230   gpointer       data;
231   GLogHandler   *next;
232 };
233
234
235 /* --- variables --- */
236 static GMutex         g_messages_lock;
237 static GLogDomain    *g_log_domains = NULL;
238 static GPrintFunc     glib_print_func = NULL;
239 static GPrintFunc     glib_printerr_func = NULL;
240 static GPrivate       g_log_depth;
241 static GLogFunc       default_log_func = g_log_default_handler;
242 static gpointer       default_log_data = NULL;
243 static GTestLogFatalFunc fatal_log_func = NULL;
244 static gpointer          fatal_log_data;
245
246 /* --- functions --- */
247 #ifdef G_OS_WIN32
248 #  include <windows.h>
249 static gboolean win32_keep_fatal_message = FALSE;
250
251 /* This default message will usually be overwritten. */
252 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
253  * called with huge strings, is it?
254  */
255 static gchar  fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
256 static gchar *fatal_msg_ptr = fatal_msg_buf;
257
258 #undef write
259 static inline int
260 dowrite (int          fd,
261          const void  *buf,
262          unsigned int len)
263 {
264   if (win32_keep_fatal_message)
265     {
266       memcpy (fatal_msg_ptr, buf, len);
267       fatal_msg_ptr += len;
268       *fatal_msg_ptr = 0;
269       return len;
270     }
271
272   write (fd, buf, len);
273
274   return len;
275 }
276 #define write(fd, buf, len) dowrite(fd, buf, len)
277
278 #endif
279
280 static void
281 write_string (int          fd,
282               const gchar *string)
283 {
284   write (fd, string, strlen (string));
285 }
286
287 static GLogDomain*
288 g_log_find_domain_L (const gchar *log_domain)
289 {
290   register GLogDomain *domain;
291   
292   domain = g_log_domains;
293   while (domain)
294     {
295       if (strcmp (domain->log_domain, log_domain) == 0)
296         return domain;
297       domain = domain->next;
298     }
299   return NULL;
300 }
301
302 static GLogDomain*
303 g_log_domain_new_L (const gchar *log_domain)
304 {
305   register GLogDomain *domain;
306
307   domain = g_new (GLogDomain, 1);
308   domain->log_domain = g_strdup (log_domain);
309   domain->fatal_mask = G_LOG_FATAL_MASK;
310   domain->handlers = NULL;
311   
312   domain->next = g_log_domains;
313   g_log_domains = domain;
314   
315   return domain;
316 }
317
318 static void
319 g_log_domain_check_free_L (GLogDomain *domain)
320 {
321   if (domain->fatal_mask == G_LOG_FATAL_MASK &&
322       domain->handlers == NULL)
323     {
324       register GLogDomain *last, *work;
325       
326       last = NULL;  
327
328       work = g_log_domains;
329       while (work)
330         {
331           if (work == domain)
332             {
333               if (last)
334                 last->next = domain->next;
335               else
336                 g_log_domains = domain->next;
337               g_free (domain->log_domain);
338               g_free (domain);
339               break;
340             }
341           last = work;
342           work = last->next;
343         }  
344     }
345 }
346
347 static GLogFunc
348 g_log_domain_get_handler_L (GLogDomain  *domain,
349                             GLogLevelFlags log_level,
350                             gpointer    *data)
351 {
352   if (domain && log_level)
353     {
354       register GLogHandler *handler;
355       
356       handler = domain->handlers;
357       while (handler)
358         {
359           if ((handler->log_level & log_level) == log_level)
360             {
361               *data = handler->data;
362               return handler->log_func;
363             }
364           handler = handler->next;
365         }
366     }
367
368   *data = default_log_data;
369   return default_log_func;
370 }
371
372 /**
373  * g_log_set_always_fatal:
374  * @fatal_mask: the mask containing bits set for each level
375  *     of error which is to be fatal
376  *
377  * Sets the message levels which are always fatal, in any log domain.
378  * When a message with any of these levels is logged the program terminates.
379  * You can only set the levels defined by GLib to be fatal.
380  * %G_LOG_LEVEL_ERROR is always fatal.
381  *
382  * You can also make some message levels fatal at runtime by setting
383  * the <envar>G_DEBUG</envar> environment variable (see
384  * <ulink url="glib-running.html">Running GLib Applications</ulink>).
385  *
386  * Returns: the old fatal mask
387  */
388 GLogLevelFlags
389 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
390 {
391   GLogLevelFlags old_mask;
392
393   /* restrict the global mask to levels that are known to glib
394    * since this setting applies to all domains
395    */
396   fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
397   /* force errors to be fatal */
398   fatal_mask |= G_LOG_LEVEL_ERROR;
399   /* remove bogus flag */
400   fatal_mask &= ~G_LOG_FLAG_FATAL;
401
402   g_mutex_lock (&g_messages_lock);
403   old_mask = g_log_always_fatal;
404   g_log_always_fatal = fatal_mask;
405   g_mutex_unlock (&g_messages_lock);
406
407   return old_mask;
408 }
409
410 /**
411  * g_log_set_fatal_mask:
412  * @log_domain: the log domain
413  * @fatal_mask: the new fatal mask
414  *
415  * Sets the log levels which are fatal in the given domain.
416  * %G_LOG_LEVEL_ERROR is always fatal.
417  *
418  * Returns: the old fatal mask for the log domain
419  */
420 GLogLevelFlags
421 g_log_set_fatal_mask (const gchar   *log_domain,
422                       GLogLevelFlags fatal_mask)
423 {
424   GLogLevelFlags old_flags;
425   register GLogDomain *domain;
426   
427   if (!log_domain)
428     log_domain = "";
429   
430   /* force errors to be fatal */
431   fatal_mask |= G_LOG_LEVEL_ERROR;
432   /* remove bogus flag */
433   fatal_mask &= ~G_LOG_FLAG_FATAL;
434   
435   g_mutex_lock (&g_messages_lock);
436
437   domain = g_log_find_domain_L (log_domain);
438   if (!domain)
439     domain = g_log_domain_new_L (log_domain);
440   old_flags = domain->fatal_mask;
441   
442   domain->fatal_mask = fatal_mask;
443   g_log_domain_check_free_L (domain);
444
445   g_mutex_unlock (&g_messages_lock);
446
447   return old_flags;
448 }
449
450 /**
451  * g_log_set_handler:
452  * @log_domain: (allow-none): the log domain, or %NULL for the default ""
453  *     application domain
454  * @log_levels: the log levels to apply the log handler for.
455  *     To handle fatal and recursive messages as well, combine
456  *     the log levels with the #G_LOG_FLAG_FATAL and
457  *     #G_LOG_FLAG_RECURSION bit flags.
458  * @log_func: the log handler function
459  * @user_data: data passed to the log handler
460  *
461  * Sets the log handler for a domain and a set of log levels.
462  * To handle fatal and recursive messages the @log_levels parameter
463  * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
464  * bit flags.
465  *
466  * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
467  * you want to set a handler for this log level you must combine it with
468  * #G_LOG_FLAG_FATAL.
469  *
470  * <example>
471  * <title>Adding a log handler for all warning messages in the default
472  * (application) domain</title>
473  * <programlisting>
474  * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
475  *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
476  * </programlisting>
477  * </example>
478  *
479  * <example>
480  * <title>Adding a log handler for all critical messages from GTK+</title>
481  * <programlisting>
482  * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
483  *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
484  * </programlisting>
485  * </example>
486  *
487  * <example>
488  * <title>Adding a log handler for <emphasis>all</emphasis> messages from
489  * GLib</title>
490  * <programlisting>
491  * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
492  *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
493  * </programlisting>
494  * </example>
495  *
496  * Returns: the id of the new handler
497  */
498 guint
499 g_log_set_handler (const gchar   *log_domain,
500                    GLogLevelFlags log_levels,
501                    GLogFunc       log_func,
502                    gpointer       user_data)
503 {
504   static guint handler_id = 0;
505   GLogDomain *domain;
506   GLogHandler *handler;
507   
508   g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
509   g_return_val_if_fail (log_func != NULL, 0);
510   
511   if (!log_domain)
512     log_domain = "";
513
514   handler = g_new (GLogHandler, 1);
515
516   g_mutex_lock (&g_messages_lock);
517
518   domain = g_log_find_domain_L (log_domain);
519   if (!domain)
520     domain = g_log_domain_new_L (log_domain);
521   
522   handler->id = ++handler_id;
523   handler->log_level = log_levels;
524   handler->log_func = log_func;
525   handler->data = user_data;
526   handler->next = domain->handlers;
527   domain->handlers = handler;
528
529   g_mutex_unlock (&g_messages_lock);
530   
531   return handler_id;
532 }
533
534 /**
535  * g_log_set_default_handler:
536  * @log_func: the log handler function
537  * @user_data: data passed to the log handler
538  *
539  * Installs a default log handler which is used if no
540  * log handler has been set for the particular log domain
541  * and log level combination. By default, GLib uses
542  * g_log_default_handler() as default log handler.
543  *
544  * Returns: the previous default log handler
545  *
546  * Since: 2.6
547  */
548 GLogFunc
549 g_log_set_default_handler (GLogFunc log_func,
550                            gpointer user_data)
551 {
552   GLogFunc old_log_func;
553   
554   g_mutex_lock (&g_messages_lock);
555   old_log_func = default_log_func;
556   default_log_func = log_func;
557   default_log_data = user_data;
558   g_mutex_unlock (&g_messages_lock);
559   
560   return old_log_func;
561 }
562
563 /**
564  * g_test_log_set_fatal_handler:
565  * @log_func: the log handler function.
566  * @user_data: data passed to the log handler.
567  *
568  * Installs a non-error fatal log handler which can be
569  * used to decide whether log messages which are counted
570  * as fatal abort the program.
571  *
572  * The use case here is that you are running a test case
573  * that depends on particular libraries or circumstances
574  * and cannot prevent certain known critical or warning
575  * messages. So you install a handler that compares the
576  * domain and message to precisely not abort in such a case.
577  *
578  * Note that the handler is reset at the beginning of
579  * any test case, so you have to set it inside each test
580  * function which needs the special behavior.
581  *
582  * This handler has no effect on g_error messages.
583  *
584  * Since: 2.22
585  **/
586 void
587 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
588                               gpointer          user_data)
589 {
590   g_mutex_lock (&g_messages_lock);
591   fatal_log_func = log_func;
592   fatal_log_data = user_data;
593   g_mutex_unlock (&g_messages_lock);
594 }
595
596 /**
597  * g_log_remove_handler:
598  * @log_domain: the log domain
599  * @handler_id: the id of the handler, which was returned
600  *     in g_log_set_handler()
601  *
602  * Removes the log handler.
603  */
604 void
605 g_log_remove_handler (const gchar *log_domain,
606                       guint        handler_id)
607 {
608   register GLogDomain *domain;
609   
610   g_return_if_fail (handler_id > 0);
611   
612   if (!log_domain)
613     log_domain = "";
614   
615   g_mutex_lock (&g_messages_lock);
616   domain = g_log_find_domain_L (log_domain);
617   if (domain)
618     {
619       GLogHandler *work, *last;
620       
621       last = NULL;
622       work = domain->handlers;
623       while (work)
624         {
625           if (work->id == handler_id)
626             {
627               if (last)
628                 last->next = work->next;
629               else
630                 domain->handlers = work->next;
631               g_log_domain_check_free_L (domain); 
632               g_mutex_unlock (&g_messages_lock);
633               g_free (work);
634               return;
635             }
636           last = work;
637           work = last->next;
638         }
639     } 
640   g_mutex_unlock (&g_messages_lock);
641   g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
642              G_STRLOC, handler_id, log_domain);
643 }
644
645 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
646                             (wc == 0x7f) || \
647                             (wc >= 0x80 && wc < 0xa0)))
648      
649 static gchar*
650 strdup_convert (const gchar *string,
651                 const gchar *charset)
652 {
653   if (!g_utf8_validate (string, -1, NULL))
654     {
655       GString *gstring = g_string_new ("[Invalid UTF-8] ");
656       guchar *p;
657
658       for (p = (guchar *)string; *p; p++)
659         {
660           if (CHAR_IS_SAFE(*p) &&
661               !(*p == '\r' && *(p + 1) != '\n') &&
662               *p < 0x80)
663             g_string_append_c (gstring, *p);
664           else
665             g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
666         }
667       
668       return g_string_free (gstring, FALSE);
669     }
670   else
671     {
672       GError *err = NULL;
673       
674       gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
675       if (result)
676         return result;
677       else
678         {
679           /* Not thread-safe, but doesn't matter if we print the warning twice
680            */
681           static gboolean warned = FALSE; 
682           if (!warned)
683             {
684               warned = TRUE;
685               _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
686             }
687           g_error_free (err);
688           
689           return g_strdup (string);
690         }
691     }
692 }
693
694 /* For a radix of 8 we need at most 3 output bytes for 1 input
695  * byte. Additionally we might need up to 2 output bytes for the
696  * readix prefix and 1 byte for the trailing NULL.
697  */
698 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
699
700 static void
701 format_unsigned (gchar  *buf,
702                  gulong  num,
703                  guint   radix)
704 {
705   gulong tmp;
706   gchar c;
707   gint i, n;
708
709   /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
710
711   if (radix != 8 && radix != 10 && radix != 16)
712     {
713       *buf = '\000';
714       return;
715     }
716   
717   if (!num)
718     {
719       *buf++ = '0';
720       *buf = '\000';
721       return;
722     } 
723   
724   if (radix == 16)
725     {
726       *buf++ = '0';
727       *buf++ = 'x';
728     }
729   else if (radix == 8)
730     {
731       *buf++ = '0';
732     }
733         
734   n = 0;
735   tmp = num;
736   while (tmp)
737     {
738       tmp /= radix;
739       n++;
740     }
741
742   i = n;
743
744   /* Again we can't use g_assert; actually this check should _never_ fail. */
745   if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
746     {
747       *buf = '\000';
748       return;
749     }
750
751   while (num)
752     {
753       i--;
754       c = (num % radix);
755       if (c < 10)
756         buf[i] = c + '0';
757       else
758         buf[i] = c + 'a' - 10;
759       num /= radix;
760     }
761   
762   buf[n] = '\000';
763 }
764
765 /* string size big enough to hold level prefix */
766 #define STRING_BUFFER_SIZE      (FORMAT_UNSIGNED_BUFSIZE + 32)
767
768 #define ALERT_LEVELS            (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
769
770 /* these are emitted by the default log handler */
771 #define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
772 /* these are filtered by G_MESSAGES_DEBUG by the default log handler */
773 #define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
774
775 static int
776 mklevel_prefix (gchar          level_prefix[STRING_BUFFER_SIZE],
777                 GLogLevelFlags log_level)
778 {
779   gboolean to_stdout = TRUE;
780
781   /* we may not call _any_ GLib functions here */
782
783   switch (log_level & G_LOG_LEVEL_MASK)
784     {
785     case G_LOG_LEVEL_ERROR:
786       strcpy (level_prefix, "ERROR");
787       to_stdout = FALSE;
788       break;
789     case G_LOG_LEVEL_CRITICAL:
790       strcpy (level_prefix, "CRITICAL");
791       to_stdout = FALSE;
792       break;
793     case G_LOG_LEVEL_WARNING:
794       strcpy (level_prefix, "WARNING");
795       to_stdout = FALSE;
796       break;
797     case G_LOG_LEVEL_MESSAGE:
798       strcpy (level_prefix, "Message");
799       to_stdout = FALSE;
800       break;
801     case G_LOG_LEVEL_INFO:
802       strcpy (level_prefix, "INFO");
803       break;
804     case G_LOG_LEVEL_DEBUG:
805       strcpy (level_prefix, "DEBUG");
806       break;
807     default:
808       if (log_level)
809         {
810           strcpy (level_prefix, "LOG-");
811           format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
812         }
813       else
814         strcpy (level_prefix, "LOG");
815       break;
816     }
817   if (log_level & G_LOG_FLAG_RECURSION)
818     strcat (level_prefix, " (recursed)");
819   if (log_level & ALERT_LEVELS)
820     strcat (level_prefix, " **");
821
822 #ifdef G_OS_WIN32
823   win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
824 #endif
825   return to_stdout ? 1 : 2;
826 }
827
828 typedef struct {
829   gchar          *log_domain;
830   GLogLevelFlags  log_level;
831   gchar          *pattern;
832 } GTestExpectedMessage;
833
834 static GSList *expected_messages = NULL;
835
836 /**
837  * g_logv:
838  * @log_domain: the log domain
839  * @log_level: the log level
840  * @format: the message format. See the printf() documentation
841  * @args: the parameters to insert into the format string
842  *
843  * Logs an error or debugging message.
844  *
845  * If the log level has been set as fatal, the abort()
846  * function is called to terminate the program.
847  */
848 void
849 g_logv (const gchar   *log_domain,
850         GLogLevelFlags log_level,
851         const gchar   *format,
852         va_list        args)
853 {
854   gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
855   gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
856   gchar buffer[1025], *msg, *msg_alloc = NULL;
857   gint i;
858
859   log_level &= G_LOG_LEVEL_MASK;
860   if (!log_level)
861     return;
862
863   if (log_level & G_LOG_FLAG_RECURSION)
864     {
865       /* we use a stack buffer of fixed size, since we're likely
866        * in an out-of-memory situation
867        */
868       gsize size G_GNUC_UNUSED;
869
870       size = _g_vsnprintf (buffer, 1024, format, args);
871       msg = buffer;
872     }
873   else
874     msg = msg_alloc = g_strdup_vprintf (format, args);
875
876   if (expected_messages)
877     {
878       GTestExpectedMessage *expected = expected_messages->data;
879
880       expected_messages = g_slist_delete_link (expected_messages,
881                                                expected_messages);
882       if (strcmp (expected->log_domain, log_domain) == 0 &&
883           ((log_level & expected->log_level) == expected->log_level) &&
884           g_pattern_match_simple (expected->pattern, msg))
885         {
886           g_free (expected->log_domain);
887           g_free (expected->pattern);
888           g_free (expected);
889           g_free (msg_alloc);
890           return;
891         }
892       else
893         {
894           gchar level_prefix[STRING_BUFFER_SIZE];
895           gchar *expected_message;
896
897           mklevel_prefix (level_prefix, expected->log_level);
898           expected_message = g_strdup_printf ("Did not see expected message %s: %s",
899                                               level_prefix, expected->pattern);
900           g_log_default_handler (log_domain, log_level, expected_message, NULL);
901           g_free (expected_message);
902
903           log_level |= G_LOG_FLAG_FATAL;
904         }
905     }
906
907   for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
908     {
909       register GLogLevelFlags test_level;
910
911       test_level = 1 << i;
912       if (log_level & test_level)
913         {
914           GLogDomain *domain;
915           GLogFunc log_func;
916           GLogLevelFlags domain_fatal_mask;
917           gpointer data = NULL;
918           gboolean masquerade_fatal = FALSE;
919           guint depth;
920
921           if (was_fatal)
922             test_level |= G_LOG_FLAG_FATAL;
923           if (was_recursion)
924             test_level |= G_LOG_FLAG_RECURSION;
925
926           /* check recursion and lookup handler */
927           g_mutex_lock (&g_messages_lock);
928           depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
929           domain = g_log_find_domain_L (log_domain ? log_domain : "");
930           if (depth)
931             test_level |= G_LOG_FLAG_RECURSION;
932           depth++;
933           domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
934           if ((domain_fatal_mask | g_log_always_fatal) & test_level)
935             test_level |= G_LOG_FLAG_FATAL;
936           if (test_level & G_LOG_FLAG_RECURSION)
937             log_func = _g_log_fallback_handler;
938           else
939             log_func = g_log_domain_get_handler_L (domain, test_level, &data);
940           domain = NULL;
941           g_mutex_unlock (&g_messages_lock);
942
943           g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
944
945           log_func (log_domain, test_level, msg, data);
946
947           if ((test_level & G_LOG_FLAG_FATAL)
948               && !(test_level & G_LOG_LEVEL_ERROR))
949             {
950               masquerade_fatal = fatal_log_func
951                 && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
952             }
953
954           if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
955             {
956 #ifdef G_OS_WIN32
957               gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
958               
959               MessageBox (NULL, locale_msg, NULL,
960                           MB_ICONERROR|MB_SETFOREGROUND);
961               if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
962                 G_BREAKPOINT ();
963               else
964                 abort ();
965 #else
966               if (!(test_level & G_LOG_FLAG_RECURSION))
967                 G_BREAKPOINT ();
968               else
969                 abort ();
970 #endif /* !G_OS_WIN32 */
971             }
972           
973           depth--;
974           g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
975         }
976     }
977
978   g_free (msg_alloc);
979 }
980
981 /**
982  * g_log:
983  * @log_domain: the log domain, usually #G_LOG_DOMAIN
984  * @log_level: the log level, either from #GLogLevelFlags
985  *     or a user-defined level
986  * @format: the message format. See the printf() documentation
987  * @...: the parameters to insert into the format string
988  *
989  * Logs an error or debugging message.
990  *
991  * If the log level has been set as fatal, the abort()
992  * function is called to terminate the program.
993  */
994 void
995 g_log (const gchar   *log_domain,
996        GLogLevelFlags log_level,
997        const gchar   *format,
998        ...)
999 {
1000   va_list args;
1001   
1002   va_start (args, format);
1003   g_logv (log_domain, log_level, format, args);
1004   va_end (args);
1005 }
1006
1007 void
1008 g_return_if_fail_warning (const char *log_domain,
1009                           const char *pretty_function,
1010                           const char *expression)
1011 {
1012   g_log (log_domain,
1013          G_LOG_LEVEL_CRITICAL,
1014          "%s: assertion `%s' failed",
1015          pretty_function,
1016          expression);
1017 }
1018
1019 void
1020 g_warn_message (const char     *domain,
1021                 const char     *file,
1022                 int             line,
1023                 const char     *func,
1024                 const char     *warnexpr)
1025 {
1026   char *s, lstr[32];
1027   g_snprintf (lstr, 32, "%d", line);
1028   if (warnexpr)
1029     s = g_strconcat ("(", file, ":", lstr, "):",
1030                      func, func[0] ? ":" : "",
1031                      " runtime check failed: (", warnexpr, ")", NULL);
1032   else
1033     s = g_strconcat ("(", file, ":", lstr, "):",
1034                      func, func[0] ? ":" : "",
1035                      " ", "code should not be reached", NULL);
1036   g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
1037   g_free (s);
1038 }
1039
1040 void
1041 g_assert_warning (const char *log_domain,
1042                   const char *file,
1043                   const int   line,
1044                   const char *pretty_function,
1045                   const char *expression)
1046 {
1047   g_log (log_domain,
1048          G_LOG_LEVEL_ERROR,
1049          expression 
1050          ? "file %s: line %d (%s): assertion failed: (%s)"
1051          : "file %s: line %d (%s): should not be reached",
1052          file, 
1053          line, 
1054          pretty_function,
1055          expression);
1056   abort ();
1057 }
1058
1059 /**
1060  * g_test_expect_message:
1061  * @log_domain: the log domain of the message
1062  * @log_level: the log level of the message
1063  * @pattern: a glob-style
1064  *     <link linkend="glib-Glob-style-pattern-matching">pattern</link>
1065  *
1066  * Indicates that a message with the given @log_domain and @log_level,
1067  * with text matching @pattern, is expected to be logged. When this
1068  * message is logged, it will not be printed, and the test case will
1069  * not abort.
1070  *
1071  * Use g_test_assert_expected_messages() to assert that all
1072  * previously-expected messages have been seen and suppressed.
1073  *
1074  * You can call this multiple times in a row, if multiple messages are
1075  * expected as a result of a single call. (The messages must appear in
1076  * the same order as the calls to g_test_expect_message().)
1077  *
1078  * For example:
1079  *
1080  * |[
1081  *   /&ast; g_main_context_push_thread_default() should fail if the
1082  *    &ast; context is already owned by another thread.
1083  *    &ast;/
1084  *   g_test_expect_message (G_LOG_DOMAIN,
1085  *                          G_LOG_LEVEL_CRITICIAL,
1086  *                          "assertion.*acquired_context.*failed");
1087  *   g_main_context_push_thread_default (bad_context);
1088  *   g_test_assert_expected_messages ();
1089  * ]|
1090  *
1091  * Note that you cannot use this to test g_error() messages, since
1092  * g_error() intentionally never returns even if the program doesn't
1093  * abort; use g_test_trap_fork() in this case.
1094  *
1095  * Since: 2.34
1096  */
1097 void
1098 g_test_expect_message (const gchar    *log_domain,
1099                        GLogLevelFlags  log_level,
1100                        const gchar    *pattern)
1101 {
1102   GTestExpectedMessage *expected;
1103
1104   g_return_if_fail (log_domain != NULL);
1105   g_return_if_fail (log_level != 0);
1106   g_return_if_fail (pattern != NULL);
1107
1108   expected = g_new (GTestExpectedMessage, 1);
1109   expected->log_domain = g_strdup (log_domain);
1110   expected->log_level = log_level;
1111   expected->pattern = g_strdup (pattern);
1112
1113   expected_messages = g_slist_append (expected_messages, expected);
1114 }
1115
1116 void
1117 g_test_assert_expected_messages_internal (const char     *domain,
1118                                           const char     *file,
1119                                           int             line,
1120                                           const char     *func)
1121 {
1122   if (expected_messages)
1123     {
1124       GTestExpectedMessage *expected;
1125       gchar level_prefix[STRING_BUFFER_SIZE];
1126       gchar *message;
1127
1128       expected = expected_messages->data;
1129
1130       mklevel_prefix (level_prefix, expected->log_level);
1131       message = g_strdup_printf ("Did not see expected message %s: %s",
1132                                  level_prefix, expected->pattern);
1133       g_assertion_message (domain, file, line, func, message);
1134       g_free (message);
1135     }
1136 }
1137
1138 /**
1139  * g_test_assert_expected_messages:
1140  *
1141  * Asserts that all messages previously indicated via
1142  * g_test_expect_message() have been seen and suppressed.
1143  *
1144  * Since: 2.34
1145  */
1146
1147 void
1148 _g_log_fallback_handler (const gchar   *log_domain,
1149                          GLogLevelFlags log_level,
1150                          const gchar   *message,
1151                          gpointer       unused_data)
1152 {
1153   gchar level_prefix[STRING_BUFFER_SIZE];
1154 #ifndef G_OS_WIN32
1155   gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1156 #endif
1157   int fd;
1158
1159   /* we cannot call _any_ GLib functions in this fallback handler,
1160    * which is why we skip UTF-8 conversion, etc.
1161    * since we either recursed or ran out of memory, we're in a pretty
1162    * pathologic situation anyways, what we can do is giving the
1163    * the process ID unconditionally however.
1164    */
1165
1166   fd = mklevel_prefix (level_prefix, log_level);
1167   if (!message)
1168     message = "(NULL) message";
1169
1170 #ifndef G_OS_WIN32
1171   format_unsigned (pid_string, getpid (), 10);
1172 #endif
1173
1174   if (log_domain)
1175     write_string (fd, "\n");
1176   else
1177     write_string (fd, "\n** ");
1178
1179 #ifndef G_OS_WIN32
1180   write_string (fd, "(process:");
1181   write_string (fd, pid_string);
1182   write_string (fd, "): ");
1183 #endif
1184
1185   if (log_domain)
1186     {
1187       write_string (fd, log_domain);
1188       write_string (fd, "-");
1189     }
1190   write_string (fd, level_prefix);
1191   write_string (fd, ": ");
1192   write_string (fd, message);
1193 }
1194
1195 static void
1196 escape_string (GString *string)
1197 {
1198   const char *p = string->str;
1199   gunichar wc;
1200
1201   while (p < string->str + string->len)
1202     {
1203       gboolean safe;
1204             
1205       wc = g_utf8_get_char_validated (p, -1);
1206       if (wc == (gunichar)-1 || wc == (gunichar)-2)  
1207         {
1208           gchar *tmp;
1209           guint pos;
1210
1211           pos = p - string->str;
1212
1213           /* Emit invalid UTF-8 as hex escapes 
1214            */
1215           tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
1216           g_string_erase (string, pos, 1);
1217           g_string_insert (string, pos, tmp);
1218
1219           p = string->str + (pos + 4); /* Skip over escape sequence */
1220
1221           g_free (tmp);
1222           continue;
1223         }
1224       if (wc == '\r')
1225         {
1226           safe = *(p + 1) == '\n';
1227         }
1228       else
1229         {
1230           safe = CHAR_IS_SAFE (wc);
1231         }
1232       
1233       if (!safe)
1234         {
1235           gchar *tmp;
1236           guint pos;
1237
1238           pos = p - string->str;
1239           
1240           /* Largest char we escape is 0x0a, so we don't have to worry
1241            * about 8-digit \Uxxxxyyyy
1242            */
1243           tmp = g_strdup_printf ("\\u%04x", wc); 
1244           g_string_erase (string, pos, g_utf8_next_char (p) - p);
1245           g_string_insert (string, pos, tmp);
1246           g_free (tmp);
1247
1248           p = string->str + (pos + 6); /* Skip over escape sequence */
1249         }
1250       else
1251         p = g_utf8_next_char (p);
1252     }
1253 }
1254
1255 /**
1256  * g_log_default_handler:
1257  * @log_domain: the log domain of the message
1258  * @log_level: the level of the message
1259  * @message: the message
1260  * @unused_data: data passed from g_log() which is unused
1261  *
1262  * The default log handler set up by GLib; g_log_set_default_handler()
1263  * allows to install an alternate default log handler.
1264  * This is used if no log handler has been set for the particular log
1265  * domain and log level combination. It outputs the message to stderr
1266  * or stdout and if the log level is fatal it calls abort().
1267  *
1268  * The behavior of this log handler can be influenced by a number of
1269  * environment variables:
1270  * <variablelist>
1271  *   <varlistentry>
1272  *     <term><envar>G_MESSAGES_PREFIXED</envar></term>
1273  *     <listitem>
1274  *       A :-separated list of log levels for which messages should
1275  *       be prefixed by the program name and PID of the aplication.
1276  *     </listitem>
1277  *   </varlistentry>
1278  *   <varlistentry>
1279  *     <term><envar>G_MESSAGES_DEBUG</envar></term>
1280  *     <listitem>
1281  *       A space-separated list of log domains for which debug and
1282  *       informational messages are printed. By default these
1283  *       messages are not printed.
1284  *     </listitem>
1285  *   </varlistentry>
1286  * </variablelist>
1287  *
1288  * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1289  * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1290  * the rest.
1291  */
1292 void
1293 g_log_default_handler (const gchar   *log_domain,
1294                        GLogLevelFlags log_level,
1295                        const gchar   *message,
1296                        gpointer       unused_data)
1297 {
1298   gchar level_prefix[STRING_BUFFER_SIZE], *string;
1299   GString *gstring;
1300   int fd;
1301   const gchar *domains;
1302
1303   if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
1304     goto emit;
1305
1306   domains = g_getenv ("G_MESSAGES_DEBUG");
1307   if (((log_level & INFO_LEVELS) == 0) ||
1308       domains == NULL ||
1309       (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
1310     return;
1311
1312  emit:
1313   /* we can be called externally with recursion for whatever reason */
1314   if (log_level & G_LOG_FLAG_RECURSION)
1315     {
1316       _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1317       return;
1318     }
1319
1320   fd = mklevel_prefix (level_prefix, log_level);
1321
1322   gstring = g_string_new (NULL);
1323   if (log_level & ALERT_LEVELS)
1324     g_string_append (gstring, "\n");
1325   if (!log_domain)
1326     g_string_append (gstring, "** ");
1327
1328   if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
1329     {
1330       const gchar *prg_name = g_get_prgname ();
1331       
1332       if (!prg_name)
1333         g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1334       else
1335         g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1336     }
1337
1338   if (log_domain)
1339     {
1340       g_string_append (gstring, log_domain);
1341       g_string_append_c (gstring, '-');
1342     }
1343   g_string_append (gstring, level_prefix);
1344
1345   g_string_append (gstring, ": ");
1346   if (!message)
1347     g_string_append (gstring, "(NULL) message");
1348   else
1349     {
1350       GString *msg;
1351       const gchar *charset;
1352
1353       msg = g_string_new (message);
1354       escape_string (msg);
1355
1356       if (g_get_charset (&charset))
1357         g_string_append (gstring, msg->str);    /* charset is UTF-8 already */
1358       else
1359         {
1360           string = strdup_convert (msg->str, charset);
1361           g_string_append (gstring, string);
1362           g_free (string);
1363         }
1364
1365       g_string_free (msg, TRUE);
1366     }
1367   g_string_append (gstring, "\n");
1368
1369   string = g_string_free (gstring, FALSE);
1370
1371   write_string (fd, string);
1372   g_free (string);
1373 }
1374
1375 /**
1376  * g_set_print_handler:
1377  * @func: the new print handler
1378  *
1379  * Sets the print handler.
1380  *
1381  * Any messages passed to g_print() will be output via
1382  * the new handler. The default handler simply outputs
1383  * the message to stdout. By providing your own handler
1384  * you can redirect the output, to a GTK+ widget or a
1385  * log file for example.
1386  *
1387  * Returns: the old print handler
1388  */
1389 GPrintFunc
1390 g_set_print_handler (GPrintFunc func)
1391 {
1392   GPrintFunc old_print_func;
1393
1394   g_mutex_lock (&g_messages_lock);
1395   old_print_func = glib_print_func;
1396   glib_print_func = func;
1397   g_mutex_unlock (&g_messages_lock);
1398
1399   return old_print_func;
1400 }
1401
1402 /**
1403  * g_print:
1404  * @format: the message format. See the printf() documentation
1405  * @...: the parameters to insert into the format string
1406  *
1407  * Outputs a formatted message via the print handler.
1408  * The default print handler simply outputs the message to stdout.
1409  *
1410  * g_print() should not be used from within libraries for debugging
1411  * messages, since it may be redirected by applications to special
1412  * purpose message windows or even files. Instead, libraries should
1413  * use g_log(), or the convenience functions g_message(), g_warning()
1414  * and g_error().
1415  */
1416 void
1417 g_print (const gchar *format,
1418          ...)
1419 {
1420   va_list args;
1421   gchar *string;
1422   GPrintFunc local_glib_print_func;
1423
1424   g_return_if_fail (format != NULL);
1425
1426   va_start (args, format);
1427   string = g_strdup_vprintf (format, args);
1428   va_end (args);
1429
1430   g_mutex_lock (&g_messages_lock);
1431   local_glib_print_func = glib_print_func;
1432   g_mutex_unlock (&g_messages_lock);
1433
1434   if (local_glib_print_func)
1435     local_glib_print_func (string);
1436   else
1437     {
1438       const gchar *charset;
1439
1440       if (g_get_charset (&charset))
1441         fputs (string, stdout); /* charset is UTF-8 already */
1442       else
1443         {
1444           gchar *lstring = strdup_convert (string, charset);
1445
1446           fputs (lstring, stdout);
1447           g_free (lstring);
1448         }
1449       fflush (stdout);
1450     }
1451   g_free (string);
1452 }
1453
1454 /**
1455  * g_set_printerr_handler:
1456  * @func: the new error message handler
1457  *
1458  * Sets the handler for printing error messages.
1459  *
1460  * Any messages passed to g_printerr() will be output via
1461  * the new handler. The default handler simply outputs the
1462  * message to stderr. By providing your own handler you can
1463  * redirect the output, to a GTK+ widget or a log file for
1464  * example.
1465  *
1466  * Returns: the old error message handler
1467  */
1468 GPrintFunc
1469 g_set_printerr_handler (GPrintFunc func)
1470 {
1471   GPrintFunc old_printerr_func;
1472
1473   g_mutex_lock (&g_messages_lock);
1474   old_printerr_func = glib_printerr_func;
1475   glib_printerr_func = func;
1476   g_mutex_unlock (&g_messages_lock);
1477
1478   return old_printerr_func;
1479 }
1480
1481 /**
1482  * g_printerr:
1483  * @format: the message format. See the printf() documentation
1484  * @...: the parameters to insert into the format string
1485  *
1486  * Outputs a formatted message via the error message handler.
1487  * The default handler simply outputs the message to stderr.
1488  *
1489  * g_printerr() should not be used from within libraries.
1490  * Instead g_log() should be used, or the convenience functions
1491  * g_message(), g_warning() and g_error().
1492  */
1493 void
1494 g_printerr (const gchar *format,
1495             ...)
1496 {
1497   va_list args;
1498   gchar *string;
1499   GPrintFunc local_glib_printerr_func;
1500
1501   g_return_if_fail (format != NULL);
1502
1503   va_start (args, format);
1504   string = g_strdup_vprintf (format, args);
1505   va_end (args);
1506
1507   g_mutex_lock (&g_messages_lock);
1508   local_glib_printerr_func = glib_printerr_func;
1509   g_mutex_unlock (&g_messages_lock);
1510
1511   if (local_glib_printerr_func)
1512     local_glib_printerr_func (string);
1513   else
1514     {
1515       const gchar *charset;
1516
1517       if (g_get_charset (&charset))
1518         fputs (string, stderr); /* charset is UTF-8 already */
1519       else
1520         {
1521           gchar *lstring = strdup_convert (string, charset);
1522
1523           fputs (lstring, stderr);
1524           g_free (lstring);
1525         }
1526       fflush (stderr);
1527     }
1528   g_free (string);
1529 }
1530
1531 /**
1532  * g_printf_string_upper_bound:
1533  * @format: the format string. See the printf() documentation
1534  * @args: the parameters to be inserted into the format string
1535  *
1536  * Calculates the maximum space needed to store the output
1537  * of the sprintf() function.
1538  *
1539  * Returns: the maximum space needed to store the formatted string
1540  */
1541 gsize
1542 g_printf_string_upper_bound (const gchar *format,
1543                              va_list      args)
1544 {
1545   gchar c;
1546   return _g_vsnprintf (&c, 1, format, args) + 1;
1547 }