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/.
46 #include "gprintfint.h"
47 #include "gthreadinit.h"
52 # define STRICT /* Strict typing, please */
53 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
58 /* --- structures --- */
59 typedef struct _GLogDomain GLogDomain;
60 typedef struct _GLogHandler GLogHandler;
64 GLogLevelFlags fatal_mask;
65 GLogHandler *handlers;
71 GLogLevelFlags log_level;
78 /* --- variables --- */
79 static GMutex *g_messages_lock = NULL;
80 static GLogDomain *g_log_domains = NULL;
81 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
82 static GPrintFunc glib_print_func = NULL;
83 static GPrintFunc glib_printerr_func = NULL;
84 static GPrivate *g_log_depth = NULL;
85 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
86 static GLogFunc default_log_func = g_log_default_handler;
87 static gpointer default_log_data = NULL;
89 /* --- functions --- */
94 static gboolean win32_keep_fatal_message = FALSE;
96 /* This default message will usually be overwritten. */
97 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
98 * called with huge strings, is it?
100 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
101 static gchar *fatal_msg_ptr = fatal_msg_buf;
109 if (win32_keep_fatal_message)
111 memcpy (fatal_msg_ptr, buf, len);
112 fatal_msg_ptr += len;
117 write (fd, buf, len);
121 #define write(fd, buf, len) dowrite(fd, buf, len)
126 write_string (int fd,
129 write (fd, string, strlen (string));
133 g_messages_prefixed_init (void)
135 static gboolean initialized = FALSE;
142 val = g_getenv ("G_MESSAGES_PREFIXED");
146 static const GDebugKey keys[] = {
147 { "error", G_LOG_LEVEL_ERROR },
148 { "critical", G_LOG_LEVEL_CRITICAL },
149 { "warning", G_LOG_LEVEL_WARNING },
150 { "message", G_LOG_LEVEL_MESSAGE },
151 { "info", G_LOG_LEVEL_INFO },
152 { "debug", G_LOG_LEVEL_DEBUG }
155 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
161 g_log_find_domain_L (const gchar *log_domain)
163 register GLogDomain *domain;
165 domain = g_log_domains;
168 if (strcmp (domain->log_domain, log_domain) == 0)
170 domain = domain->next;
176 g_log_domain_new_L (const gchar *log_domain)
178 register GLogDomain *domain;
180 domain = g_new (GLogDomain, 1);
181 domain->log_domain = g_strdup (log_domain);
182 domain->fatal_mask = G_LOG_FATAL_MASK;
183 domain->handlers = NULL;
185 domain->next = g_log_domains;
186 g_log_domains = domain;
192 g_log_domain_check_free_L (GLogDomain *domain)
194 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
195 domain->handlers == NULL)
197 register GLogDomain *last, *work;
201 work = g_log_domains;
207 last->next = domain->next;
209 g_log_domains = domain->next;
210 g_free (domain->log_domain);
221 g_log_domain_get_handler_L (GLogDomain *domain,
222 GLogLevelFlags log_level,
225 if (domain && log_level)
227 register GLogHandler *handler;
229 handler = domain->handlers;
232 if ((handler->log_level & log_level) == log_level)
234 *data = handler->data;
235 return handler->log_func;
237 handler = handler->next;
241 *data = default_log_data;
242 return default_log_func;
246 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
248 GLogLevelFlags old_mask;
250 /* restrict the global mask to levels that are known to glib
251 * since this setting applies to all domains
253 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
254 /* force errors to be fatal */
255 fatal_mask |= G_LOG_LEVEL_ERROR;
256 /* remove bogus flag */
257 fatal_mask &= ~G_LOG_FLAG_FATAL;
259 g_mutex_lock (g_messages_lock);
260 old_mask = g_log_always_fatal;
261 g_log_always_fatal = fatal_mask;
262 g_mutex_unlock (g_messages_lock);
268 g_log_set_fatal_mask (const gchar *log_domain,
269 GLogLevelFlags fatal_mask)
271 GLogLevelFlags old_flags;
272 register GLogDomain *domain;
277 /* force errors to be fatal */
278 fatal_mask |= G_LOG_LEVEL_ERROR;
279 /* remove bogus flag */
280 fatal_mask &= ~G_LOG_FLAG_FATAL;
282 g_mutex_lock (g_messages_lock);
284 domain = g_log_find_domain_L (log_domain);
286 domain = g_log_domain_new_L (log_domain);
287 old_flags = domain->fatal_mask;
289 domain->fatal_mask = fatal_mask;
290 g_log_domain_check_free_L (domain);
292 g_mutex_unlock (g_messages_lock);
298 g_log_set_handler (const gchar *log_domain,
299 GLogLevelFlags log_levels,
303 static guint handler_id = 0;
305 GLogHandler *handler;
307 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
308 g_return_val_if_fail (log_func != NULL, 0);
313 handler = g_new (GLogHandler, 1);
315 g_mutex_lock (g_messages_lock);
317 domain = g_log_find_domain_L (log_domain);
319 domain = g_log_domain_new_L (log_domain);
321 handler->id = ++handler_id;
322 handler->log_level = log_levels;
323 handler->log_func = log_func;
324 handler->data = user_data;
325 handler->next = domain->handlers;
326 domain->handlers = handler;
328 g_mutex_unlock (g_messages_lock);
334 g_log_set_default_handler (GLogFunc log_func,
337 GLogFunc old_log_func;
339 g_mutex_lock (g_messages_lock);
340 old_log_func = default_log_func;
341 default_log_func = log_func;
342 default_log_data = user_data;
343 g_mutex_unlock (g_messages_lock);
349 g_log_remove_handler (const gchar *log_domain,
352 register GLogDomain *domain;
354 g_return_if_fail (handler_id > 0);
359 g_mutex_lock (g_messages_lock);
360 domain = g_log_find_domain_L (log_domain);
363 GLogHandler *work, *last;
366 work = domain->handlers;
369 if (work->id == handler_id)
372 last->next = work->next;
374 domain->handlers = work->next;
375 g_log_domain_check_free_L (domain);
376 g_mutex_unlock (g_messages_lock);
384 g_mutex_unlock (g_messages_lock);
385 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
386 G_STRLOC, handler_id, log_domain);
390 g_logv (const gchar *log_domain,
391 GLogLevelFlags log_level,
395 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
396 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
399 log_level &= G_LOG_LEVEL_MASK;
403 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
405 register GLogLevelFlags test_level;
408 if (log_level & test_level)
410 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
413 GLogLevelFlags domain_fatal_mask;
414 gpointer data = NULL;
417 test_level |= G_LOG_FLAG_FATAL;
419 test_level |= G_LOG_FLAG_RECURSION;
421 /* check recursion and lookup handler */
422 g_mutex_lock (g_messages_lock);
423 domain = g_log_find_domain_L (log_domain ? log_domain : "");
425 test_level |= G_LOG_FLAG_RECURSION;
427 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
428 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
429 test_level |= G_LOG_FLAG_FATAL;
430 if (test_level & G_LOG_FLAG_RECURSION)
431 log_func = _g_log_fallback_handler;
433 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
435 g_mutex_unlock (g_messages_lock);
437 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
439 /* had to defer debug initialization until we can keep track of recursion */
440 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
442 GLogLevelFlags orig_test_level = test_level;
445 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
446 test_level |= G_LOG_FLAG_FATAL;
447 if (test_level != orig_test_level)
449 /* need a relookup, not nice, but not too bad either */
450 g_mutex_lock (g_messages_lock);
451 domain = g_log_find_domain_L (log_domain ? log_domain : "");
452 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
454 g_mutex_unlock (g_messages_lock);
458 if (test_level & G_LOG_FLAG_RECURSION)
460 /* we use a stack buffer of fixed size, since we're likely
461 * in an out-of-memory situation
465 size = _g_vsnprintf (buffer, 1024, format, args1);
467 log_func (log_domain, test_level, buffer, data);
471 gchar *msg = g_strdup_vprintf (format, args1);
473 log_func (log_domain, test_level, msg, data);
478 if (test_level & G_LOG_FLAG_FATAL)
481 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
483 MessageBox (NULL, locale_msg, NULL,
484 MB_ICONERROR|MB_SETFOREGROUND);
485 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
490 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
491 if (!(test_level & G_LOG_FLAG_RECURSION))
495 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
497 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
498 #endif /* !G_OS_WIN32 */
502 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
508 g_log (const gchar *log_domain,
509 GLogLevelFlags log_level,
515 va_start (args, format);
516 g_logv (log_domain, log_level, format, args);
521 g_return_if_fail_warning (const char *log_domain,
522 const char *pretty_function,
523 const char *expression)
526 * Omit the prefix used by the PLT-reduction
527 * technique used in GTK+.
529 if (g_str_has_prefix (pretty_function, "IA__"))
530 pretty_function += 4;
532 G_LOG_LEVEL_CRITICAL,
533 "%s: assertion `%s' failed",
539 g_assert_warning (const char *log_domain,
542 const char *pretty_function,
543 const char *expression)
546 * Omit the prefix used by the PLT-reduction
547 * technique used in GTK+.
549 if (g_str_has_prefix (pretty_function, "IA__"))
550 pretty_function += 4;
554 ? "file %s: line %d (%s): assertion failed: (%s)"
555 : "file %s: line %d (%s): should not be reached",
563 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
565 (wc >= 0x80 && wc < 0xa0)))
568 strdup_convert (const gchar *string,
569 const gchar *charset)
571 if (!g_utf8_validate (string, -1, NULL))
573 GString *gstring = g_string_new ("[Invalid UTF-8] ");
576 for (p = (guchar *)string; *p; p++)
578 if (CHAR_IS_SAFE(*p) &&
579 !(*p == '\r' && *(p + 1) != '\n') &&
581 g_string_append_c (gstring, *p);
583 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
586 return g_string_free (gstring, FALSE);
592 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
597 /* Not thread-safe, but doesn't matter if we print the warning twice
599 static gboolean warned = FALSE;
603 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
607 return g_strdup (string);
612 /* For a radix of 8 we need at most 3 output bytes for 1 input
613 * byte. Additionally we might need up to 2 output bytes for the
614 * readix prefix and 1 byte for the trailing NULL.
616 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
619 format_unsigned (gchar *buf,
627 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
629 if (radix != 8 && radix != 10 && radix != 16)
662 /* Again we can't use g_assert; actually this check should _never_ fail. */
663 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
676 buf[i] = c + 'a' - 10;
683 /* string size big enough to hold level prefix */
684 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
686 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
689 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
690 GLogLevelFlags log_level)
692 gboolean to_stdout = TRUE;
694 /* we may not call _any_ GLib functions here */
696 switch (log_level & G_LOG_LEVEL_MASK)
698 case G_LOG_LEVEL_ERROR:
699 strcpy (level_prefix, "ERROR");
702 case G_LOG_LEVEL_CRITICAL:
703 strcpy (level_prefix, "CRITICAL");
706 case G_LOG_LEVEL_WARNING:
707 strcpy (level_prefix, "WARNING");
710 case G_LOG_LEVEL_MESSAGE:
711 strcpy (level_prefix, "Message");
714 case G_LOG_LEVEL_INFO:
715 strcpy (level_prefix, "INFO");
717 case G_LOG_LEVEL_DEBUG:
718 strcpy (level_prefix, "DEBUG");
723 strcpy (level_prefix, "LOG-");
724 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
727 strcpy (level_prefix, "LOG");
730 if (log_level & G_LOG_FLAG_RECURSION)
731 strcat (level_prefix, " (recursed)");
732 if (log_level & ALERT_LEVELS)
733 strcat (level_prefix, " **");
736 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
738 return to_stdout ? 1 : 2;
742 _g_log_fallback_handler (const gchar *log_domain,
743 GLogLevelFlags log_level,
744 const gchar *message,
745 gpointer unused_data)
747 gchar level_prefix[STRING_BUFFER_SIZE];
749 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
751 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
754 /* we can not call _any_ GLib functions in this fallback handler,
755 * which is why we skip UTF-8 conversion, etc.
756 * since we either recursed or ran out of memory, we're in a pretty
757 * pathologic situation anyways, what we can do is giving the
758 * the process ID unconditionally however.
761 fd = mklevel_prefix (level_prefix, log_level);
763 message = "(NULL) message";
766 format_unsigned (pid_string, getpid (), 10);
770 write_string (fd, "\n");
772 write_string (fd, "\n** ");
775 write_string (fd, "(process:");
776 write_string (fd, pid_string);
777 write_string (fd, "): ");
782 write_string (fd, log_domain);
783 write_string (fd, "-");
785 write_string (fd, level_prefix);
786 write_string (fd, ": ");
787 write_string (fd, message);
789 write_string (fd, "\naborting...\n");
791 write_string (fd, "\n");
795 escape_string (GString *string)
797 const char *p = string->str;
800 while (p < string->str + string->len)
804 wc = g_utf8_get_char_validated (p, -1);
805 if (wc == (gunichar)-1 || wc == (gunichar)-2)
810 pos = p - string->str;
812 /* Emit invalid UTF-8 as hex escapes
814 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
815 g_string_erase (string, pos, 1);
816 g_string_insert (string, pos, tmp);
818 p = string->str + (pos + 4); /* Skip over escape sequence */
825 safe = *(p + 1) == '\n';
829 safe = CHAR_IS_SAFE (wc);
837 pos = p - string->str;
839 /* Largest char we escape is 0x0a, so we don't have to worry
840 * about 8-digit \Uxxxxyyyy
842 tmp = g_strdup_printf ("\\u%04x", wc);
843 g_string_erase (string, pos, g_utf8_next_char (p) - p);
844 g_string_insert (string, pos, tmp);
847 p = string->str + (pos + 6); /* Skip over escape sequence */
850 p = g_utf8_next_char (p);
855 g_log_default_handler (const gchar *log_domain,
856 GLogLevelFlags log_level,
857 const gchar *message,
858 gpointer unused_data)
860 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
861 gchar level_prefix[STRING_BUFFER_SIZE], *string;
865 /* we can be called externally with recursion for whatever reason */
866 if (log_level & G_LOG_FLAG_RECURSION)
868 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
872 g_messages_prefixed_init ();
874 fd = mklevel_prefix (level_prefix, log_level);
876 gstring = g_string_new (NULL);
877 if (log_level & ALERT_LEVELS)
878 g_string_append (gstring, "\n");
880 g_string_append (gstring, "** ");
882 if ((g_log_msg_prefix & log_level) == log_level)
884 const gchar *prg_name = g_get_prgname ();
887 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
889 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
894 g_string_append (gstring, log_domain);
895 g_string_append_c (gstring, '-');
897 g_string_append (gstring, level_prefix);
899 g_string_append (gstring, ": ");
901 g_string_append (gstring, "(NULL) message");
905 const gchar *charset;
907 msg = g_string_new (message);
910 if (g_get_charset (&charset))
911 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
914 string = strdup_convert (msg->str, charset);
915 g_string_append (gstring, string);
919 g_string_free (msg, TRUE);
922 g_string_append (gstring, "\naborting...\n");
924 g_string_append (gstring, "\n");
926 string = g_string_free (gstring, FALSE);
928 write_string (fd, string);
933 g_set_print_handler (GPrintFunc func)
935 GPrintFunc old_print_func;
937 g_mutex_lock (g_messages_lock);
938 old_print_func = glib_print_func;
939 glib_print_func = func;
940 g_mutex_unlock (g_messages_lock);
942 return old_print_func;
946 g_print (const gchar *format,
951 GPrintFunc local_glib_print_func;
953 g_return_if_fail (format != NULL);
955 va_start (args, format);
956 string = g_strdup_vprintf (format, args);
959 g_mutex_lock (g_messages_lock);
960 local_glib_print_func = glib_print_func;
961 g_mutex_unlock (g_messages_lock);
963 if (local_glib_print_func)
964 local_glib_print_func (string);
967 const gchar *charset;
969 if (g_get_charset (&charset))
970 fputs (string, stdout); /* charset is UTF-8 already */
973 gchar *lstring = strdup_convert (string, charset);
975 fputs (lstring, stdout);
984 g_set_printerr_handler (GPrintFunc func)
986 GPrintFunc old_printerr_func;
988 g_mutex_lock (g_messages_lock);
989 old_printerr_func = glib_printerr_func;
990 glib_printerr_func = func;
991 g_mutex_unlock (g_messages_lock);
993 return old_printerr_func;
997 g_printerr (const gchar *format,
1002 GPrintFunc local_glib_printerr_func;
1004 g_return_if_fail (format != NULL);
1006 va_start (args, format);
1007 string = g_strdup_vprintf (format, args);
1010 g_mutex_lock (g_messages_lock);
1011 local_glib_printerr_func = glib_printerr_func;
1012 g_mutex_unlock (g_messages_lock);
1014 if (local_glib_printerr_func)
1015 local_glib_printerr_func (string);
1018 const gchar *charset;
1020 if (g_get_charset (&charset))
1021 fputs (string, stderr); /* charset is UTF-8 already */
1024 gchar *lstring = strdup_convert (string, charset);
1026 fputs (lstring, stderr);
1035 g_printf_string_upper_bound (const gchar *format,
1039 return _g_vsnprintf (&c, 1, format, args) + 1;
1043 _g_messages_thread_init (void)
1045 g_messages_lock = g_mutex_new ();
1046 g_messages_prefixed_init ();
1051 _g_messages_thread_private_init (void)
1053 g_assert (g_log_depth == NULL);
1054 g_log_depth = g_private_new (NULL);
1057 gboolean _g_debug_initialized = FALSE;
1058 guint _g_debug_flags = 0;
1061 _g_debug_init (void)
1065 _g_debug_initialized = TRUE;
1067 val = g_getenv ("G_DEBUG");
1070 static const GDebugKey keys[] = {
1071 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1074 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1077 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1079 GLogLevelFlags fatal_mask;
1081 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1082 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1083 g_log_set_always_fatal (fatal_mask);
1087 #define __G_MESSAGES_C__
1088 #include "galiasdef.c"