New function to install an alternate default log handler. (#66387, Darin
[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 #include "config.h"
32
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <signal.h>
41 #include <locale.h>
42 #include <errno.h>
43
44 #include "glib.h"
45 #include "gdebug.h"
46 #include "gprintfint.h"
47 #include "gthreadinit.h"
48
49 #ifdef G_OS_WIN32
50 #include <io.h>
51 typedef FILE* GFileDescriptor;
52 #else
53 typedef gint GFileDescriptor;
54 #endif
55
56 /* --- structures --- */
57 typedef struct _GLogDomain      GLogDomain;
58 typedef struct _GLogHandler     GLogHandler;
59 struct _GLogDomain
60 {
61   gchar         *log_domain;
62   GLogLevelFlags fatal_mask;
63   GLogHandler   *handlers;
64   GLogDomain    *next;
65 };
66 struct _GLogHandler
67 {
68   guint          id;
69   GLogLevelFlags log_level;
70   GLogFunc       log_func;
71   gpointer       data;
72   GLogHandler   *next;
73 };
74
75
76 /* --- variables --- */
77 static GMutex        *g_messages_lock = NULL;
78 static GLogDomain    *g_log_domains = NULL;
79 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
80 static GPrintFunc     glib_print_func = NULL;
81 static GPrintFunc     glib_printerr_func = NULL;
82 static GPrivate      *g_log_depth = NULL;
83 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
84 static GLogFunc       default_log_func = g_log_default_handler;
85 static gpointer       default_log_data = NULL;
86
87 /* --- functions --- */
88 #ifdef G_OS_WIN32
89 #  define STRICT
90 #  include <windows.h>
91 #  undef STRICT
92 #  include <process.h>          /* For _getpid() */
93 static gboolean win32_keep_fatal_message = FALSE;
94
95 /* This default message will usually be overwritten. */
96 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
97  * with huge strings, is it?
98  */
99 static gchar  fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
100 static gchar *fatal_msg_ptr = fatal_msg_buf;
101
102 /* Just use stdio. If we're out of memory, we're hosed anyway. */
103 #undef write
104 static inline int
105 dowrite (GFileDescriptor fd,
106          const void  *buf,
107          unsigned int len)
108 {
109   if (win32_keep_fatal_message)
110     {
111       memcpy (fatal_msg_ptr, buf, len);
112       fatal_msg_ptr += len;
113       *fatal_msg_ptr = 0;
114       return len;
115     }
116
117   fwrite (buf, len, 1, fd);
118   fflush (fd);
119
120   return len;
121 }
122 #define write(fd, buf, len) dowrite(fd, buf, len)
123
124 static void
125 ensure_stdout_valid (void)
126 {
127   static gboolean alloc_console_called = FALSE;
128   HANDLE handle;
129
130   if (win32_keep_fatal_message)
131     return;
132
133   if (!alloc_console_called)
134     {
135       handle = (HANDLE) _get_osfhandle (fileno (stdout)); 
136   
137       if (handle == INVALID_HANDLE_VALUE)
138         {
139           AllocConsole ();
140           alloc_console_called = TRUE;
141           freopen ("CONOUT$", "w", stdout);
142         }
143     }
144 }
145
146 static void
147 ensure_stderr_valid (void)
148 {
149   static gboolean alloc_console_called = FALSE;
150   HANDLE handle;
151
152   if (win32_keep_fatal_message)
153     return;
154
155   if (!alloc_console_called)
156     {
157       handle = (HANDLE) _get_osfhandle (fileno (stderr)); 
158
159       if (handle == INVALID_HANDLE_VALUE)
160         {
161           AllocConsole ();
162           alloc_console_called = TRUE;
163           freopen ("CONOUT$", "w", stderr);
164         }
165     }
166 }
167
168 #else
169 #define ensure_stdout_valid()   /* Define as empty */
170 #define ensure_stderr_valid()
171 #endif
172
173 static void
174 write_string (GFileDescriptor fd,
175               const gchar    *string)
176 {
177   write (fd, string, strlen (string));
178 }
179
180 static void
181 g_messages_prefixed_init (void)
182 {
183   static gboolean initialized = FALSE;
184
185   if (!initialized)
186     {
187       const gchar *val;
188
189       initialized = TRUE;
190       val = g_getenv ("G_MESSAGES_PREFIXED");
191       
192       if (val)
193         {
194           static const GDebugKey keys[] = {
195             { "error", G_LOG_LEVEL_ERROR },
196             { "critical", G_LOG_LEVEL_CRITICAL },
197             { "warning", G_LOG_LEVEL_WARNING },
198             { "message", G_LOG_LEVEL_MESSAGE },
199             { "info", G_LOG_LEVEL_INFO },
200             { "debug", G_LOG_LEVEL_DEBUG }
201           };
202           
203           g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
204         }
205     }
206 }
207
208 static GLogDomain*
209 g_log_find_domain_L (const gchar *log_domain)
210 {
211   register GLogDomain *domain;
212   
213   domain = g_log_domains;
214   while (domain)
215     {
216       if (strcmp (domain->log_domain, log_domain) == 0)
217         return domain;
218       domain = domain->next;
219     }
220   return NULL;
221 }
222
223 static GLogDomain*
224 g_log_domain_new_L (const gchar *log_domain)
225 {
226   register GLogDomain *domain;
227
228   domain = g_new (GLogDomain, 1);
229   domain->log_domain = g_strdup (log_domain);
230   domain->fatal_mask = G_LOG_FATAL_MASK;
231   domain->handlers = NULL;
232   
233   domain->next = g_log_domains;
234   g_log_domains = domain;
235   
236   return domain;
237 }
238
239 static void
240 g_log_domain_check_free_L (GLogDomain *domain)
241 {
242   if (domain->fatal_mask == G_LOG_FATAL_MASK &&
243       domain->handlers == NULL)
244     {
245       register GLogDomain *last, *work;
246       
247       last = NULL;  
248
249       work = g_log_domains;
250       while (work)
251         {
252           if (work == domain)
253             {
254               if (last)
255                 last->next = domain->next;
256               else
257                 g_log_domains = domain->next;
258               g_free (domain->log_domain);
259               g_free (domain);
260               break;
261             }
262           last = work;
263           work = last->next;
264         }  
265     }
266 }
267
268 static GLogFunc
269 g_log_domain_get_handler_L (GLogDomain  *domain,
270                             GLogLevelFlags log_level,
271                             gpointer    *data)
272 {
273   if (domain && log_level)
274     {
275       register GLogHandler *handler;
276       
277       handler = domain->handlers;
278       while (handler)
279         {
280           if ((handler->log_level & log_level) == log_level)
281             {
282               *data = handler->data;
283               return handler->log_func;
284             }
285           handler = handler->next;
286         }
287     }
288
289   *data = default_log_data;
290   return default_log_func;
291 }
292
293 GLogLevelFlags
294 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
295 {
296   GLogLevelFlags old_mask;
297
298   /* restrict the global mask to levels that are known to glib
299    * since this setting applies to all domains
300    */
301   fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
302   /* force errors to be fatal */
303   fatal_mask |= G_LOG_LEVEL_ERROR;
304   /* remove bogus flag */
305   fatal_mask &= ~G_LOG_FLAG_FATAL;
306
307   g_mutex_lock (g_messages_lock);
308   old_mask = g_log_always_fatal;
309   g_log_always_fatal = fatal_mask;
310   g_mutex_unlock (g_messages_lock);
311
312   return old_mask;
313 }
314
315 GLogLevelFlags
316 g_log_set_fatal_mask (const gchar   *log_domain,
317                       GLogLevelFlags fatal_mask)
318 {
319   GLogLevelFlags old_flags;
320   register GLogDomain *domain;
321   
322   if (!log_domain)
323     log_domain = "";
324   
325   /* force errors to be fatal */
326   fatal_mask |= G_LOG_LEVEL_ERROR;
327   /* remove bogus flag */
328   fatal_mask &= ~G_LOG_FLAG_FATAL;
329   
330   g_mutex_lock (g_messages_lock);
331
332   domain = g_log_find_domain_L (log_domain);
333   if (!domain)
334     domain = g_log_domain_new_L (log_domain);
335   old_flags = domain->fatal_mask;
336   
337   domain->fatal_mask = fatal_mask;
338   g_log_domain_check_free_L (domain);
339
340   g_mutex_unlock (g_messages_lock);
341
342   return old_flags;
343 }
344
345 guint
346 g_log_set_handler (const gchar   *log_domain,
347                    GLogLevelFlags log_levels,
348                    GLogFunc       log_func,
349                    gpointer       user_data)
350 {
351   static guint handler_id = 0;
352   GLogDomain *domain;
353   GLogHandler *handler;
354   
355   g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
356   g_return_val_if_fail (log_func != NULL, 0);
357   
358   if (!log_domain)
359     log_domain = "";
360
361   handler = g_new (GLogHandler, 1);
362
363   g_mutex_lock (g_messages_lock);
364
365   domain = g_log_find_domain_L (log_domain);
366   if (!domain)
367     domain = g_log_domain_new_L (log_domain);
368   
369   handler->id = ++handler_id;
370   handler->log_level = log_levels;
371   handler->log_func = log_func;
372   handler->data = user_data;
373   handler->next = domain->handlers;
374   domain->handlers = handler;
375
376   g_mutex_unlock (g_messages_lock);
377   
378   return handler_id;
379 }
380
381 GLogFunc
382 g_log_set_default_handler (GLogFunc log_func,
383                            gpointer user_data)
384 {
385   GLogFunc old_log_func;
386   
387   g_mutex_lock (g_messages_lock);
388   old_log_func = default_log_func;
389   default_log_func = log_func;
390   default_log_data = user_data;
391   g_mutex_unlock (g_messages_lock);
392   
393   return old_log_func;
394 }
395
396 void
397 g_log_remove_handler (const gchar *log_domain,
398                       guint        handler_id)
399 {
400   register GLogDomain *domain;
401   
402   g_return_if_fail (handler_id > 0);
403   
404   if (!log_domain)
405     log_domain = "";
406   
407   g_mutex_lock (g_messages_lock);
408   domain = g_log_find_domain_L (log_domain);
409   if (domain)
410     {
411       GLogHandler *work, *last;
412       
413       last = NULL;
414       work = domain->handlers;
415       while (work)
416         {
417           if (work->id == handler_id)
418             {
419               if (last)
420                 last->next = work->next;
421               else
422                 domain->handlers = work->next;
423               g_log_domain_check_free_L (domain); 
424               g_mutex_unlock (g_messages_lock);
425               g_free (work);
426               return;
427             }
428           last = work;
429           work = last->next;
430         }
431     } 
432   g_mutex_unlock (g_messages_lock);
433   g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
434              G_STRLOC, handler_id, log_domain);
435 }
436
437 void
438 g_logv (const gchar   *log_domain,
439         GLogLevelFlags log_level,
440         const gchar   *format,
441         va_list        args1)
442 {
443   gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
444   gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
445   gint i;
446
447   log_level &= G_LOG_LEVEL_MASK;
448   if (!log_level)
449     return;
450   
451   for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
452     {
453       register GLogLevelFlags test_level;
454       
455       test_level = 1 << i;
456       if (log_level & test_level)
457         {
458           guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
459           GLogDomain *domain;
460           GLogFunc log_func;
461           guint domain_fatal_mask;
462           gpointer data = NULL;
463
464           if (was_fatal)
465             test_level |= G_LOG_FLAG_FATAL;
466           if (was_recursion)
467             test_level |= G_LOG_FLAG_RECURSION;
468
469           /* check recursion and lookup handler */
470           g_mutex_lock (g_messages_lock);
471           domain = g_log_find_domain_L (log_domain ? log_domain : "");
472           if (depth)
473             test_level |= G_LOG_FLAG_RECURSION;
474           depth++;
475           domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
476           if ((domain_fatal_mask | g_log_always_fatal) & test_level)
477             test_level |= G_LOG_FLAG_FATAL;
478           if (test_level & G_LOG_FLAG_RECURSION)
479             log_func = _g_log_fallback_handler;
480           else
481             log_func = g_log_domain_get_handler_L (domain, test_level, &data);
482           domain = NULL;
483           g_mutex_unlock (g_messages_lock);
484
485           g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
486
487           /* had to defer debug initialization until we can keep track of recursion */
488           if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
489             {
490               guint orig_test_level = test_level;
491
492               _g_debug_init ();
493               if ((domain_fatal_mask | g_log_always_fatal) & test_level)
494                 test_level |= G_LOG_FLAG_FATAL;
495               if (test_level != orig_test_level)
496                 {
497                   /* need a relookup, not nice, but not too bad either */
498                   g_mutex_lock (g_messages_lock);
499                   domain = g_log_find_domain_L (log_domain ? log_domain : "");
500                   log_func = g_log_domain_get_handler_L (domain, test_level, &data);
501                   domain = NULL;
502                   g_mutex_unlock (g_messages_lock);
503                 }
504             }
505
506           if (test_level & G_LOG_FLAG_RECURSION)
507             {
508               /* we use a stack buffer of fixed size, since we're likely
509                * in an out-of-memory situation
510                */
511               gchar buffer[1025];
512               gint size;
513               size = _g_vsnprintf (buffer, 1024, format, args1);
514
515               log_func (log_domain, test_level, buffer, data);
516             }
517           else
518             {
519               gchar *msg = g_strdup_vprintf (format, args1);
520
521               log_func (log_domain, test_level, msg, data);
522
523               g_free (msg);
524             }
525
526           if (test_level & G_LOG_FLAG_FATAL)
527             {
528 #ifdef G_OS_WIN32
529               gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
530               
531               MessageBox (NULL, locale_msg, NULL,
532                           MB_ICONERROR|MB_SETFOREGROUND);
533 #endif
534 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
535               if (!(test_level & G_LOG_FLAG_RECURSION))
536                 G_BREAKPOINT ();
537               else
538                 abort ();
539 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
540               abort ();
541 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
542             }
543           
544           depth--;
545           g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
546         }
547     }
548 }
549
550 void
551 g_log (const gchar   *log_domain,
552        GLogLevelFlags log_level,
553        const gchar   *format,
554        ...)
555 {
556   va_list args;
557   
558   va_start (args, format);
559   g_logv (log_domain, log_level, format, args);
560   va_end (args);
561 }
562
563 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
564                             (wc == 0x7f) || \
565                             (wc >= 0x80 && wc < 0xa0)))
566      
567 static gchar*
568 strdup_convert (const gchar *string,
569                 const gchar *charset)
570 {
571   if (!g_utf8_validate (string, -1, NULL))
572     {
573       GString *gstring = g_string_new ("[Invalid UTF-8] ");
574       guchar *p;
575
576       for (p = (guchar *)string; *p; p++)
577         {
578           if (CHAR_IS_SAFE(*p) &&
579               !(*p == '\r' && *(p + 1) != '\n') &&
580               *p < 0x80)
581             g_string_append_c (gstring, *p);
582           else
583             g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
584         }
585       
586       return g_string_free (gstring, FALSE);
587     }
588   else
589     {
590       GError *err = NULL;
591       
592       gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
593       if (result)
594         return result;
595       else
596         {
597           /* Not thread-safe, but doesn't matter if we print the warning twice
598            */
599           static gboolean warned = FALSE; 
600           if (!warned)
601             {
602               warned = TRUE;
603               ensure_stderr_valid ();
604               _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
605             }
606           g_error_free (err);
607           
608           return g_strdup (string);
609         }
610     }
611 }
612
613 /* For a radix of 8 we need at most 3 output bytes for 1 input
614  * byte. Additionally we might need up to 2 output bytes for the
615  * readix prefix and 1 byte for the trailing NULL.
616  */
617 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
618
619 static void
620 format_unsigned (gchar  *buf,
621                  gulong  num,
622                  guint   radix)
623 {
624   gulong tmp;
625   gchar c;
626   gint i, n;
627
628   /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
629
630   if (radix != 8 && radix != 10 && radix != 16)
631     {
632       *buf = '\000';
633       return;
634     }
635   
636   if (!num)
637     {
638       *buf++ = '0';
639       *buf = '\000';
640       return;
641     } 
642   
643   if (radix == 16)
644     {
645       *buf++ = '0';
646       *buf++ = 'x';
647     }
648   else if (radix == 8)
649     {
650       *buf++ = '0';
651     }
652         
653   n = 0;
654   tmp = num;
655   while (tmp)
656     {
657       tmp /= radix;
658       n++;
659     }
660
661   i = n;
662
663   /* Again we can't use g_assert; actually this check should _never_ fail. */
664   if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
665     {
666       *buf = '\000';
667       return;
668     }
669
670   while (num)
671     {
672       i--;
673       c = (num % radix);
674       if (c < 10)
675         buf[i] = c + '0';
676       else
677         buf[i] = c + 'a' - 10;
678       num /= radix;
679     }
680   
681   buf[n] = '\000';
682 }
683
684 /* string size big enough to hold level prefix */
685 #define STRING_BUFFER_SIZE      (FORMAT_UNSIGNED_BUFSIZE + 32)
686
687 #define ALERT_LEVELS            (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
688
689 static GFileDescriptor
690 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
691                 guint log_level)
692 {
693   gboolean to_stdout = TRUE;
694
695   /* we may not call _any_ GLib functions here */
696
697   switch (log_level & G_LOG_LEVEL_MASK)
698     {
699     case G_LOG_LEVEL_ERROR:
700       strcpy (level_prefix, "ERROR");
701       to_stdout = FALSE;
702       break;
703     case G_LOG_LEVEL_CRITICAL:
704       strcpy (level_prefix, "CRITICAL");
705       to_stdout = FALSE;
706       break;
707     case G_LOG_LEVEL_WARNING:
708       strcpy (level_prefix, "WARNING");
709       to_stdout = FALSE;
710       break;
711     case G_LOG_LEVEL_MESSAGE:
712       strcpy (level_prefix, "Message");
713       to_stdout = FALSE;
714       break;
715     case G_LOG_LEVEL_INFO:
716       strcpy (level_prefix, "INFO");
717       break;
718     case G_LOG_LEVEL_DEBUG:
719       strcpy (level_prefix, "DEBUG");
720       break;
721     default:
722       if (log_level)
723         {
724           strcpy (level_prefix, "LOG-");
725           format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
726         }
727       else
728         strcpy (level_prefix, "LOG");
729       break;
730     }
731   if (log_level & G_LOG_FLAG_RECURSION)
732     strcat (level_prefix, " (recursed)");
733   if (log_level & ALERT_LEVELS)
734     strcat (level_prefix, " **");
735
736 #ifdef G_OS_WIN32
737   win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
738
739   if (to_stdout)
740     {
741       ensure_stdout_valid ();
742       return stdout;
743     }
744   else
745     {
746       ensure_stderr_valid ();
747       return stderr;
748     }
749 #else
750   return to_stdout ? 1 : 2;
751 #endif
752 }
753
754 void
755 _g_log_fallback_handler (const gchar   *log_domain,
756                          GLogLevelFlags log_level,
757                          const gchar   *message,
758                          gpointer       unused_data)
759 {
760   gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
761   gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
762   GFileDescriptor fd;
763
764   /* we can not call _any_ GLib functions in this fallback handler,
765    * which is why we skip UTF-8 conversion, etc.
766    * since we either recursed or ran out of memory, we're in a pretty
767    * pathologic situation anyways, what we can do is giving the
768    * the process ID unconditionally however.
769    */
770
771   fd = mklevel_prefix (level_prefix, log_level);
772   if (!message)
773     message = "(NULL) message";
774
775   format_unsigned (pid_string, getpid (), 10);
776
777   if (log_domain)
778     write_string (fd, "\n");
779   else
780     write_string (fd, "\n** ");
781   write_string (fd, "(process:");
782   write_string (fd, pid_string);
783   write_string (fd, "): ");
784   if (log_domain)
785     {
786       write_string (fd, log_domain);
787       write_string (fd, "-");
788     }
789   write_string (fd, level_prefix);
790   write_string (fd, ": ");
791   write_string (fd, message);
792   if (is_fatal)
793     write_string (fd, "\naborting...\n");
794   else
795     write_string (fd, "\n");
796 }
797
798 static void
799 escape_string (GString *string)
800 {
801   const char *p = string->str;
802   gunichar wc;
803
804   while (p < string->str + string->len)
805     {
806       gboolean safe;
807             
808       wc = g_utf8_get_char_validated (p, -1);
809       if (wc == (gunichar)-1 || wc == (gunichar)-2)  
810         {
811           gchar *tmp;
812           guint pos;
813
814           pos = p - string->str;
815
816           /* Emit invalid UTF-8 as hex escapes 
817            */
818           tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
819           g_string_erase (string, pos, 1);
820           g_string_insert (string, pos, tmp);
821
822           p = string->str + (pos + 4); /* Skip over escape sequence */
823
824           g_free (tmp);
825           continue;
826         }
827       if (wc == '\r')
828         {
829           safe = *(p + 1) == '\n';
830         }
831       else
832         {
833           safe = CHAR_IS_SAFE (wc);
834         }
835       
836       if (!safe)
837         {
838           gchar *tmp;
839           guint pos;
840
841           pos = p - string->str;
842           
843           /* Largest char we escape is 0x0a, so we don't have to worry
844            * about 8-digit \Uxxxxyyyy
845            */
846           tmp = g_strdup_printf ("\\u%04x", wc); 
847           g_string_erase (string, pos, g_utf8_next_char (p) - p);
848           g_string_insert (string, pos, tmp);
849           g_free (tmp);
850
851           p = string->str + (pos + 6); /* Skip over escape sequence */
852         }
853       else
854         p = g_utf8_next_char (p);
855     }
856 }
857
858 void
859 g_log_default_handler (const gchar   *log_domain,
860                        GLogLevelFlags log_level,
861                        const gchar   *message,
862                        gpointer       unused_data)
863 {
864   gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
865   gchar level_prefix[STRING_BUFFER_SIZE], *string;
866   GString *gstring;
867   GFileDescriptor fd;
868
869   /* we can be called externally with recursion for whatever reason */
870   if (log_level & G_LOG_FLAG_RECURSION)
871     {
872       _g_log_fallback_handler (log_domain, log_level, message, unused_data);
873       return;
874     }
875
876   g_messages_prefixed_init ();
877
878   fd = mklevel_prefix (level_prefix, log_level);
879
880   gstring = g_string_new (NULL);
881   if (log_level & ALERT_LEVELS)
882     g_string_append (gstring, "\n");
883   if (!log_domain)
884     g_string_append (gstring, "** ");
885
886   if ((g_log_msg_prefix & log_level) == log_level)
887     {
888       const gchar *prg_name = g_get_prgname ();
889       
890       if (!prg_name)
891         g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
892       else
893         g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
894     }
895
896   if (log_domain)
897     {
898       g_string_append (gstring, log_domain);
899       g_string_append_c (gstring, '-');
900     }
901   g_string_append (gstring, level_prefix);
902
903   g_string_append (gstring, ": ");
904   if (!message)
905     g_string_append (gstring, "(NULL) message");
906   else
907     {
908       GString *msg;
909       const gchar *charset;
910
911       msg = g_string_new (message);
912       escape_string (msg);
913
914       if (g_get_charset (&charset))
915         g_string_append (gstring, msg->str);    /* charset is UTF-8 already */
916       else
917         {
918           string = strdup_convert (msg->str, charset);
919           g_string_append (gstring, string);
920           g_free (string);
921         }
922
923       g_string_free (msg, TRUE);
924     }
925   if (is_fatal)
926     g_string_append (gstring, "\naborting...\n");
927   else
928     g_string_append (gstring, "\n");
929
930   string = g_string_free (gstring, FALSE);
931
932   write_string (fd, string);
933   g_free (string);
934 }
935
936 GPrintFunc
937 g_set_print_handler (GPrintFunc func)
938 {
939   GPrintFunc old_print_func;
940   
941   g_mutex_lock (g_messages_lock);
942   old_print_func = glib_print_func;
943   glib_print_func = func;
944   g_mutex_unlock (g_messages_lock);
945   
946   return old_print_func;
947 }
948
949 void
950 g_print (const gchar *format,
951          ...)
952 {
953   va_list args;
954   gchar *string;
955   GPrintFunc local_glib_print_func;
956   
957   g_return_if_fail (format != NULL);
958   
959   va_start (args, format);
960   string = g_strdup_vprintf (format, args);
961   va_end (args);
962   
963   g_mutex_lock (g_messages_lock);
964   local_glib_print_func = glib_print_func;
965   g_mutex_unlock (g_messages_lock);
966   
967   if (local_glib_print_func)
968     local_glib_print_func (string);
969   else
970     {
971       const gchar *charset;
972
973       ensure_stdout_valid ();
974       if (g_get_charset (&charset))
975         fputs (string, stdout); /* charset is UTF-8 already */
976       else
977         {
978           gchar *lstring = strdup_convert (string, charset);
979
980           fputs (lstring, stdout);
981           g_free (lstring);
982         }
983       fflush (stdout);
984     }
985   g_free (string);
986 }
987
988 GPrintFunc
989 g_set_printerr_handler (GPrintFunc func)
990 {
991   GPrintFunc old_printerr_func;
992   
993   g_mutex_lock (g_messages_lock);
994   old_printerr_func = glib_printerr_func;
995   glib_printerr_func = func;
996   g_mutex_unlock (g_messages_lock);
997   
998   return old_printerr_func;
999 }
1000
1001 void
1002 g_printerr (const gchar *format,
1003             ...)
1004 {
1005   va_list args;
1006   gchar *string;
1007   GPrintFunc local_glib_printerr_func;
1008   
1009   g_return_if_fail (format != NULL);
1010   
1011   va_start (args, format);
1012   string = g_strdup_vprintf (format, args);
1013   va_end (args);
1014   
1015   g_mutex_lock (g_messages_lock);
1016   local_glib_printerr_func = glib_printerr_func;
1017   g_mutex_unlock (g_messages_lock);
1018   
1019   if (local_glib_printerr_func)
1020     local_glib_printerr_func (string);
1021   else
1022     {
1023       const gchar *charset;
1024
1025       ensure_stderr_valid ();
1026       if (g_get_charset (&charset))
1027         fputs (string, stderr); /* charset is UTF-8 already */
1028       else
1029         {
1030           gchar *lstring = strdup_convert (string, charset);
1031
1032           fputs (lstring, stderr);
1033           g_free (lstring);
1034         }
1035       fflush (stderr);
1036     }
1037   g_free (string);
1038 }
1039
1040 gsize
1041 g_printf_string_upper_bound (const gchar *format,
1042                              va_list      args)
1043 {
1044   gchar c;
1045   return _g_vsnprintf (&c, 1, format, args) + 1;
1046 }
1047
1048 void
1049 _g_messages_thread_init (void)
1050 {
1051   g_messages_lock = g_mutex_new ();
1052   g_messages_prefixed_init ();
1053   _g_debug_init ();
1054 }
1055
1056 void
1057 _g_messages_thread_private_init (void)
1058 {
1059   g_assert (g_log_depth == NULL);
1060   g_log_depth = g_private_new (NULL);
1061 }
1062
1063 gboolean _g_debug_initialized = FALSE;
1064 guint _g_debug_flags = 0;
1065
1066 void
1067 _g_debug_init (void) 
1068 {
1069   const gchar *val;
1070   
1071   _g_debug_initialized = TRUE;
1072   
1073   val = g_getenv ("G_DEBUG");
1074   if (val != NULL)
1075     {
1076       static const GDebugKey keys[] = {
1077         {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1078       };
1079       
1080       _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1081     }
1082   
1083   if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS) 
1084     {
1085       GLogLevelFlags fatal_mask;
1086       
1087       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1088       fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1089       g_log_set_always_fatal (fatal_mask);
1090     }
1091 }