Signal waiting threads, problem noticed by Christian Kellner.
[glib.git] / glib / gmessages.c
blob726db4e92331bb57fb8498c0dd0427632fb3dc21
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #include "config.h"
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>
44 #include "glib.h"
45 #include "gdebug.h"
46 #include "gprintfint.h"
47 #include "gthreadinit.h"
48 #include "galias.h"
50 #ifdef G_OS_WIN32
51 #include <process.h> /* For getpid() */
52 #include <io.h>
53 # define STRICT /* Strict typing, please */
54 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
55 # include <windows.h>
56 # undef STRICT
57 #endif
59 /* --- structures --- */
60 typedef struct _GLogDomain GLogDomain;
61 typedef struct _GLogHandler GLogHandler;
62 struct _GLogDomain
64 gchar *log_domain;
65 GLogLevelFlags fatal_mask;
66 GLogHandler *handlers;
67 GLogDomain *next;
69 struct _GLogHandler
71 guint id;
72 GLogLevelFlags log_level;
73 GLogFunc log_func;
74 gpointer data;
75 GLogHandler *next;
79 /* --- variables --- */
80 static GMutex *g_messages_lock = NULL;
81 static GLogDomain *g_log_domains = NULL;
82 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
83 static GPrintFunc glib_print_func = NULL;
84 static GPrintFunc glib_printerr_func = NULL;
85 static GPrivate *g_log_depth = NULL;
86 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
87 static GLogFunc default_log_func = g_log_default_handler;
88 static gpointer default_log_data = NULL;
90 /* --- functions --- */
91 #ifdef G_OS_WIN32
92 # define STRICT
93 # include <windows.h>
94 # undef STRICT
95 static gboolean win32_keep_fatal_message = FALSE;
97 /* This default message will usually be overwritten. */
98 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
99 * called with huge strings, is it?
101 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
102 static gchar *fatal_msg_ptr = fatal_msg_buf;
104 #undef write
105 static inline int
106 dowrite (int fd,
107 const void *buf,
108 unsigned int len)
110 if (win32_keep_fatal_message)
112 memcpy (fatal_msg_ptr, buf, len);
113 fatal_msg_ptr += len;
114 *fatal_msg_ptr = 0;
115 return len;
118 write (fd, buf, len);
120 return len;
122 #define write(fd, buf, len) dowrite(fd, buf, len)
124 #endif
126 static void
127 write_string (int fd,
128 const gchar *string)
130 write (fd, string, strlen (string));
133 static void
134 g_messages_prefixed_init (void)
136 static gboolean initialized = FALSE;
138 if (!initialized)
140 const gchar *val;
142 initialized = TRUE;
143 val = g_getenv ("G_MESSAGES_PREFIXED");
145 if (val)
147 static const GDebugKey keys[] = {
148 { "error", G_LOG_LEVEL_ERROR },
149 { "critical", G_LOG_LEVEL_CRITICAL },
150 { "warning", G_LOG_LEVEL_WARNING },
151 { "message", G_LOG_LEVEL_MESSAGE },
152 { "info", G_LOG_LEVEL_INFO },
153 { "debug", G_LOG_LEVEL_DEBUG }
156 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
161 static GLogDomain*
162 g_log_find_domain_L (const gchar *log_domain)
164 register GLogDomain *domain;
166 domain = g_log_domains;
167 while (domain)
169 if (strcmp (domain->log_domain, log_domain) == 0)
170 return domain;
171 domain = domain->next;
173 return NULL;
176 static GLogDomain*
177 g_log_domain_new_L (const gchar *log_domain)
179 register GLogDomain *domain;
181 domain = g_new (GLogDomain, 1);
182 domain->log_domain = g_strdup (log_domain);
183 domain->fatal_mask = G_LOG_FATAL_MASK;
184 domain->handlers = NULL;
186 domain->next = g_log_domains;
187 g_log_domains = domain;
189 return domain;
192 static void
193 g_log_domain_check_free_L (GLogDomain *domain)
195 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
196 domain->handlers == NULL)
198 register GLogDomain *last, *work;
200 last = NULL;
202 work = g_log_domains;
203 while (work)
205 if (work == domain)
207 if (last)
208 last->next = domain->next;
209 else
210 g_log_domains = domain->next;
211 g_free (domain->log_domain);
212 g_free (domain);
213 break;
215 last = work;
216 work = last->next;
221 static GLogFunc
222 g_log_domain_get_handler_L (GLogDomain *domain,
223 GLogLevelFlags log_level,
224 gpointer *data)
226 if (domain && log_level)
228 register GLogHandler *handler;
230 handler = domain->handlers;
231 while (handler)
233 if ((handler->log_level & log_level) == log_level)
235 *data = handler->data;
236 return handler->log_func;
238 handler = handler->next;
242 *data = default_log_data;
243 return default_log_func;
246 GLogLevelFlags
247 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
249 GLogLevelFlags old_mask;
251 /* restrict the global mask to levels that are known to glib
252 * since this setting applies to all domains
254 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
255 /* force errors to be fatal */
256 fatal_mask |= G_LOG_LEVEL_ERROR;
257 /* remove bogus flag */
258 fatal_mask &= ~G_LOG_FLAG_FATAL;
260 g_mutex_lock (g_messages_lock);
261 old_mask = g_log_always_fatal;
262 g_log_always_fatal = fatal_mask;
263 g_mutex_unlock (g_messages_lock);
265 return old_mask;
268 GLogLevelFlags
269 g_log_set_fatal_mask (const gchar *log_domain,
270 GLogLevelFlags fatal_mask)
272 GLogLevelFlags old_flags;
273 register GLogDomain *domain;
275 if (!log_domain)
276 log_domain = "";
278 /* force errors to be fatal */
279 fatal_mask |= G_LOG_LEVEL_ERROR;
280 /* remove bogus flag */
281 fatal_mask &= ~G_LOG_FLAG_FATAL;
283 g_mutex_lock (g_messages_lock);
285 domain = g_log_find_domain_L (log_domain);
286 if (!domain)
287 domain = g_log_domain_new_L (log_domain);
288 old_flags = domain->fatal_mask;
290 domain->fatal_mask = fatal_mask;
291 g_log_domain_check_free_L (domain);
293 g_mutex_unlock (g_messages_lock);
295 return old_flags;
298 guint
299 g_log_set_handler (const gchar *log_domain,
300 GLogLevelFlags log_levels,
301 GLogFunc log_func,
302 gpointer user_data)
304 static guint handler_id = 0;
305 GLogDomain *domain;
306 GLogHandler *handler;
308 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
309 g_return_val_if_fail (log_func != NULL, 0);
311 if (!log_domain)
312 log_domain = "";
314 handler = g_new (GLogHandler, 1);
316 g_mutex_lock (g_messages_lock);
318 domain = g_log_find_domain_L (log_domain);
319 if (!domain)
320 domain = g_log_domain_new_L (log_domain);
322 handler->id = ++handler_id;
323 handler->log_level = log_levels;
324 handler->log_func = log_func;
325 handler->data = user_data;
326 handler->next = domain->handlers;
327 domain->handlers = handler;
329 g_mutex_unlock (g_messages_lock);
331 return handler_id;
334 GLogFunc
335 g_log_set_default_handler (GLogFunc log_func,
336 gpointer user_data)
338 GLogFunc old_log_func;
340 g_mutex_lock (g_messages_lock);
341 old_log_func = default_log_func;
342 default_log_func = log_func;
343 default_log_data = user_data;
344 g_mutex_unlock (g_messages_lock);
346 return old_log_func;
349 void
350 g_log_remove_handler (const gchar *log_domain,
351 guint handler_id)
353 register GLogDomain *domain;
355 g_return_if_fail (handler_id > 0);
357 if (!log_domain)
358 log_domain = "";
360 g_mutex_lock (g_messages_lock);
361 domain = g_log_find_domain_L (log_domain);
362 if (domain)
364 GLogHandler *work, *last;
366 last = NULL;
367 work = domain->handlers;
368 while (work)
370 if (work->id == handler_id)
372 if (last)
373 last->next = work->next;
374 else
375 domain->handlers = work->next;
376 g_log_domain_check_free_L (domain);
377 g_mutex_unlock (g_messages_lock);
378 g_free (work);
379 return;
381 last = work;
382 work = last->next;
385 g_mutex_unlock (g_messages_lock);
386 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
387 G_STRLOC, handler_id, log_domain);
390 void
391 g_logv (const gchar *log_domain,
392 GLogLevelFlags log_level,
393 const gchar *format,
394 va_list args1)
396 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
397 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
398 gint i;
400 log_level &= G_LOG_LEVEL_MASK;
401 if (!log_level)
402 return;
404 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
406 register GLogLevelFlags test_level;
408 test_level = 1 << i;
409 if (log_level & test_level)
411 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
412 GLogDomain *domain;
413 GLogFunc log_func;
414 GLogLevelFlags domain_fatal_mask;
415 gpointer data = NULL;
417 if (was_fatal)
418 test_level |= G_LOG_FLAG_FATAL;
419 if (was_recursion)
420 test_level |= G_LOG_FLAG_RECURSION;
422 /* check recursion and lookup handler */
423 g_mutex_lock (g_messages_lock);
424 domain = g_log_find_domain_L (log_domain ? log_domain : "");
425 if (depth)
426 test_level |= G_LOG_FLAG_RECURSION;
427 depth++;
428 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
429 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
430 test_level |= G_LOG_FLAG_FATAL;
431 if (test_level & G_LOG_FLAG_RECURSION)
432 log_func = _g_log_fallback_handler;
433 else
434 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
435 domain = NULL;
436 g_mutex_unlock (g_messages_lock);
438 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
440 /* had to defer debug initialization until we can keep track of recursion */
441 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
443 GLogLevelFlags orig_test_level = test_level;
445 _g_debug_init ();
446 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
447 test_level |= G_LOG_FLAG_FATAL;
448 if (test_level != orig_test_level)
450 /* need a relookup, not nice, but not too bad either */
451 g_mutex_lock (g_messages_lock);
452 domain = g_log_find_domain_L (log_domain ? log_domain : "");
453 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
454 domain = NULL;
455 g_mutex_unlock (g_messages_lock);
459 if (test_level & G_LOG_FLAG_RECURSION)
461 /* we use a stack buffer of fixed size, since we're likely
462 * in an out-of-memory situation
464 gchar buffer[1025];
465 gint size;
466 size = _g_vsnprintf (buffer, 1024, format, args1);
468 log_func (log_domain, test_level, buffer, data);
470 else
472 gchar *msg = g_strdup_vprintf (format, args1);
474 log_func (log_domain, test_level, msg, data);
476 g_free (msg);
479 if (test_level & G_LOG_FLAG_FATAL)
481 #ifdef G_OS_WIN32
482 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
484 MessageBox (NULL, locale_msg, NULL,
485 MB_ICONERROR|MB_SETFOREGROUND);
486 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
487 G_BREAKPOINT ();
489 abort ();
490 #else
491 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
492 if (!(test_level & G_LOG_FLAG_RECURSION))
493 G_BREAKPOINT ();
494 else
495 abort ();
496 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
497 abort ();
498 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
499 #endif /* !G_OS_WIN32 */
502 depth--;
503 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
508 void
509 g_log (const gchar *log_domain,
510 GLogLevelFlags log_level,
511 const gchar *format,
512 ...)
514 va_list args;
516 va_start (args, format);
517 g_logv (log_domain, log_level, format, args);
518 va_end (args);
521 void
522 g_return_if_fail_warning (const char *log_domain,
523 const char *pretty_function,
524 const char *expression)
527 * Omit the prefix used by the PLT-reduction
528 * technique used in GTK+.
530 if (g_str_has_prefix (pretty_function, "IA__"))
531 pretty_function += 4;
532 g_log (log_domain,
533 G_LOG_LEVEL_CRITICAL,
534 "%s: assertion `%s' failed",
535 pretty_function,
536 expression);
539 void
540 g_assert_warning (const char *log_domain,
541 const char *file,
542 const int line,
543 const char *pretty_function,
544 const char *expression)
547 * Omit the prefix used by the PLT-reduction
548 * technique used in GTK+.
550 if (g_str_has_prefix (pretty_function, "IA__"))
551 pretty_function += 4;
552 g_log (log_domain,
553 G_LOG_LEVEL_ERROR,
554 expression
555 ? "file %s: line %d (%s): assertion failed: (%s)"
556 : "file %s: line %d (%s): should not be reached",
557 file,
558 line,
559 pretty_function,
560 expression);
561 abort ();
564 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
565 (wc == 0x7f) || \
566 (wc >= 0x80 && wc < 0xa0)))
568 static gchar*
569 strdup_convert (const gchar *string,
570 const gchar *charset)
572 if (!g_utf8_validate (string, -1, NULL))
574 GString *gstring = g_string_new ("[Invalid UTF-8] ");
575 guchar *p;
577 for (p = (guchar *)string; *p; p++)
579 if (CHAR_IS_SAFE(*p) &&
580 !(*p == '\r' && *(p + 1) != '\n') &&
581 *p < 0x80)
582 g_string_append_c (gstring, *p);
583 else
584 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
587 return g_string_free (gstring, FALSE);
589 else
591 GError *err = NULL;
593 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
594 if (result)
595 return result;
596 else
598 /* Not thread-safe, but doesn't matter if we print the warning twice
600 static gboolean warned = FALSE;
601 if (!warned)
603 warned = TRUE;
604 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
606 g_error_free (err);
608 return g_strdup (string);
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.
617 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
619 static void
620 format_unsigned (gchar *buf,
621 gulong num,
622 guint radix)
624 gulong tmp;
625 gchar c;
626 gint i, n;
628 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
630 if (radix != 8 && radix != 10 && radix != 16)
632 *buf = '\000';
633 return;
636 if (!num)
638 *buf++ = '0';
639 *buf = '\000';
640 return;
643 if (radix == 16)
645 *buf++ = '0';
646 *buf++ = 'x';
648 else if (radix == 8)
650 *buf++ = '0';
653 n = 0;
654 tmp = num;
655 while (tmp)
657 tmp /= radix;
658 n++;
661 i = n;
663 /* Again we can't use g_assert; actually this check should _never_ fail. */
664 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
666 *buf = '\000';
667 return;
670 while (num)
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;
681 buf[n] = '\000';
684 /* string size big enough to hold level prefix */
685 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
687 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
689 static int
690 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
691 GLogLevelFlags log_level)
693 gboolean to_stdout = TRUE;
695 /* we may not call _any_ GLib functions here */
697 switch (log_level & G_LOG_LEVEL_MASK)
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)
724 strcpy (level_prefix, "LOG-");
725 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
727 else
728 strcpy (level_prefix, "LOG");
729 break;
731 if (log_level & G_LOG_FLAG_RECURSION)
732 strcat (level_prefix, " (recursed)");
733 if (log_level & ALERT_LEVELS)
734 strcat (level_prefix, " **");
736 #ifdef G_OS_WIN32
737 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
738 #endif
739 return to_stdout ? 1 : 2;
742 void
743 _g_log_fallback_handler (const gchar *log_domain,
744 GLogLevelFlags log_level,
745 const gchar *message,
746 gpointer unused_data)
748 gchar level_prefix[STRING_BUFFER_SIZE];
749 #ifndef G_OS_WIN32
750 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
751 #endif
752 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
753 int fd;
755 /* we can not call _any_ GLib functions in this fallback handler,
756 * which is why we skip UTF-8 conversion, etc.
757 * since we either recursed or ran out of memory, we're in a pretty
758 * pathologic situation anyways, what we can do is giving the
759 * the process ID unconditionally however.
762 fd = mklevel_prefix (level_prefix, log_level);
763 if (!message)
764 message = "(NULL) message";
766 #ifndef G_OS_WIN32
767 format_unsigned (pid_string, getpid (), 10);
768 #endif
770 if (log_domain)
771 write_string (fd, "\n");
772 else
773 write_string (fd, "\n** ");
775 #ifndef G_OS_WIN32
776 write_string (fd, "(process:");
777 write_string (fd, pid_string);
778 write_string (fd, "): ");
779 #endif
781 if (log_domain)
783 write_string (fd, log_domain);
784 write_string (fd, "-");
786 write_string (fd, level_prefix);
787 write_string (fd, ": ");
788 write_string (fd, message);
789 if (is_fatal)
790 write_string (fd, "\naborting...\n");
791 else
792 write_string (fd, "\n");
795 static void
796 escape_string (GString *string)
798 const char *p = string->str;
799 gunichar wc;
801 while (p < string->str + string->len)
803 gboolean safe;
805 wc = g_utf8_get_char_validated (p, -1);
806 if (wc == (gunichar)-1 || wc == (gunichar)-2)
808 gchar *tmp;
809 guint pos;
811 pos = p - string->str;
813 /* Emit invalid UTF-8 as hex escapes
815 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
816 g_string_erase (string, pos, 1);
817 g_string_insert (string, pos, tmp);
819 p = string->str + (pos + 4); /* Skip over escape sequence */
821 g_free (tmp);
822 continue;
824 if (wc == '\r')
826 safe = *(p + 1) == '\n';
828 else
830 safe = CHAR_IS_SAFE (wc);
833 if (!safe)
835 gchar *tmp;
836 guint pos;
838 pos = p - string->str;
840 /* Largest char we escape is 0x0a, so we don't have to worry
841 * about 8-digit \Uxxxxyyyy
843 tmp = g_strdup_printf ("\\u%04x", wc);
844 g_string_erase (string, pos, g_utf8_next_char (p) - p);
845 g_string_insert (string, pos, tmp);
846 g_free (tmp);
848 p = string->str + (pos + 6); /* Skip over escape sequence */
850 else
851 p = g_utf8_next_char (p);
855 void
856 g_log_default_handler (const gchar *log_domain,
857 GLogLevelFlags log_level,
858 const gchar *message,
859 gpointer unused_data)
861 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
862 gchar level_prefix[STRING_BUFFER_SIZE], *string;
863 GString *gstring;
864 int fd;
866 /* we can be called externally with recursion for whatever reason */
867 if (log_level & G_LOG_FLAG_RECURSION)
869 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
870 return;
873 g_messages_prefixed_init ();
875 fd = mklevel_prefix (level_prefix, log_level);
877 gstring = g_string_new (NULL);
878 if (log_level & ALERT_LEVELS)
879 g_string_append (gstring, "\n");
880 if (!log_domain)
881 g_string_append (gstring, "** ");
883 if ((g_log_msg_prefix & log_level) == log_level)
885 const gchar *prg_name = g_get_prgname ();
887 if (!prg_name)
888 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
889 else
890 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
893 if (log_domain)
895 g_string_append (gstring, log_domain);
896 g_string_append_c (gstring, '-');
898 g_string_append (gstring, level_prefix);
900 g_string_append (gstring, ": ");
901 if (!message)
902 g_string_append (gstring, "(NULL) message");
903 else
905 GString *msg;
906 const gchar *charset;
908 msg = g_string_new (message);
909 escape_string (msg);
911 if (g_get_charset (&charset))
912 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
913 else
915 string = strdup_convert (msg->str, charset);
916 g_string_append (gstring, string);
917 g_free (string);
920 g_string_free (msg, TRUE);
922 if (is_fatal)
923 g_string_append (gstring, "\naborting...\n");
924 else
925 g_string_append (gstring, "\n");
927 string = g_string_free (gstring, FALSE);
929 write_string (fd, string);
930 g_free (string);
933 GPrintFunc
934 g_set_print_handler (GPrintFunc func)
936 GPrintFunc old_print_func;
938 g_mutex_lock (g_messages_lock);
939 old_print_func = glib_print_func;
940 glib_print_func = func;
941 g_mutex_unlock (g_messages_lock);
943 return old_print_func;
946 void
947 g_print (const gchar *format,
948 ...)
950 va_list args;
951 gchar *string;
952 GPrintFunc local_glib_print_func;
954 g_return_if_fail (format != NULL);
956 va_start (args, format);
957 string = g_strdup_vprintf (format, args);
958 va_end (args);
960 g_mutex_lock (g_messages_lock);
961 local_glib_print_func = glib_print_func;
962 g_mutex_unlock (g_messages_lock);
964 if (local_glib_print_func)
965 local_glib_print_func (string);
966 else
968 const gchar *charset;
970 if (g_get_charset (&charset))
971 fputs (string, stdout); /* charset is UTF-8 already */
972 else
974 gchar *lstring = strdup_convert (string, charset);
976 fputs (lstring, stdout);
977 g_free (lstring);
979 fflush (stdout);
981 g_free (string);
984 GPrintFunc
985 g_set_printerr_handler (GPrintFunc func)
987 GPrintFunc old_printerr_func;
989 g_mutex_lock (g_messages_lock);
990 old_printerr_func = glib_printerr_func;
991 glib_printerr_func = func;
992 g_mutex_unlock (g_messages_lock);
994 return old_printerr_func;
997 void
998 g_printerr (const gchar *format,
999 ...)
1001 va_list args;
1002 gchar *string;
1003 GPrintFunc local_glib_printerr_func;
1005 g_return_if_fail (format != NULL);
1007 va_start (args, format);
1008 string = g_strdup_vprintf (format, args);
1009 va_end (args);
1011 g_mutex_lock (g_messages_lock);
1012 local_glib_printerr_func = glib_printerr_func;
1013 g_mutex_unlock (g_messages_lock);
1015 if (local_glib_printerr_func)
1016 local_glib_printerr_func (string);
1017 else
1019 const gchar *charset;
1021 if (g_get_charset (&charset))
1022 fputs (string, stderr); /* charset is UTF-8 already */
1023 else
1025 gchar *lstring = strdup_convert (string, charset);
1027 fputs (lstring, stderr);
1028 g_free (lstring);
1030 fflush (stderr);
1032 g_free (string);
1035 gsize
1036 g_printf_string_upper_bound (const gchar *format,
1037 va_list args)
1039 gchar c;
1040 return _g_vsnprintf (&c, 1, format, args) + 1;
1043 void
1044 _g_messages_thread_init_nomessage (void)
1046 g_messages_lock = g_mutex_new ();
1047 g_log_depth = g_private_new (NULL);
1048 g_messages_prefixed_init ();
1049 _g_debug_init ();
1052 gboolean _g_debug_initialized = FALSE;
1053 guint _g_debug_flags = 0;
1055 void
1056 _g_debug_init (void)
1058 const gchar *val;
1060 _g_debug_initialized = TRUE;
1062 val = g_getenv ("G_DEBUG");
1063 if (val != NULL)
1065 static const GDebugKey keys[] = {
1066 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
1067 {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
1070 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1073 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1075 GLogLevelFlags fatal_mask;
1077 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1078 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1079 g_log_set_always_fatal (fatal_mask);
1082 if (_g_debug_flags & G_DEBUG_FATAL_CRITICALS)
1084 GLogLevelFlags fatal_mask;
1086 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1087 fatal_mask |= G_LOG_LEVEL_CRITICAL;
1088 g_log_set_always_fatal (fatal_mask);
1092 #define __G_MESSAGES_C__
1093 #include "galiasdef.c"