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 "gthreadprivate.h"
51 #include <process.h> /* For getpid() */
53 # define STRICT /* Strict typing, please */
54 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
59 /* --- structures --- */
60 typedef struct _GLogDomain GLogDomain;
61 typedef struct _GLogHandler GLogHandler;
65 GLogLevelFlags fatal_mask;
66 GLogHandler *handlers;
72 GLogLevelFlags log_level;
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 --- */
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;
110 if (win32_keep_fatal_message)
112 memcpy (fatal_msg_ptr, buf, len);
113 fatal_msg_ptr += len;
118 write (fd, buf, len);
122 #define write(fd, buf, len) dowrite(fd, buf, len)
127 write_string (int fd,
130 write (fd, string, strlen (string));
134 g_messages_prefixed_init (void)
136 static gboolean initialized = FALSE;
143 val = g_getenv ("G_MESSAGES_PREFIXED");
147 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));
162 g_log_find_domain_L (const gchar *log_domain)
164 register GLogDomain *domain;
166 domain = g_log_domains;
169 if (strcmp (domain->log_domain, log_domain) == 0)
171 domain = domain->next;
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;
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;
202 work = g_log_domains;
208 last->next = domain->next;
210 g_log_domains = domain->next;
211 g_free (domain->log_domain);
222 g_log_domain_get_handler_L (GLogDomain *domain,
223 GLogLevelFlags log_level,
226 if (domain && log_level)
228 register GLogHandler *handler;
230 handler = domain->handlers;
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;
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);
269 g_log_set_fatal_mask (const gchar *log_domain,
270 GLogLevelFlags fatal_mask)
272 GLogLevelFlags old_flags;
273 register GLogDomain *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);
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);
299 g_log_set_handler (const gchar *log_domain,
300 GLogLevelFlags log_levels,
304 static guint handler_id = 0;
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);
314 handler = g_new (GLogHandler, 1);
316 g_mutex_lock (g_messages_lock);
318 domain = g_log_find_domain_L (log_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);
335 g_log_set_default_handler (GLogFunc log_func,
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);
350 g_log_remove_handler (const gchar *log_domain,
353 register GLogDomain *domain;
355 g_return_if_fail (handler_id > 0);
360 g_mutex_lock (g_messages_lock);
361 domain = g_log_find_domain_L (log_domain);
364 GLogHandler *work, *last;
367 work = domain->handlers;
370 if (work->id == handler_id)
373 last->next = work->next;
375 domain->handlers = work->next;
376 g_log_domain_check_free_L (domain);
377 g_mutex_unlock (g_messages_lock);
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);
391 g_logv (const gchar *log_domain,
392 GLogLevelFlags log_level,
396 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
397 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
400 log_level &= G_LOG_LEVEL_MASK;
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;
409 if (log_level & test_level)
411 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
414 GLogLevelFlags domain_fatal_mask;
415 gpointer data = NULL;
418 test_level |= G_LOG_FLAG_FATAL;
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 : "");
426 test_level |= G_LOG_FLAG_RECURSION;
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;
434 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
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;
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);
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
468 G_VA_COPY (args2, args1);
469 size = _g_vsnprintf (buffer, 1024, format, args2);
472 log_func (log_domain, test_level, buffer, data);
479 G_VA_COPY (args2, args1);
480 msg = g_strdup_vprintf (format, args2);
483 log_func (log_domain, test_level, msg, data);
488 if (test_level & G_LOG_FLAG_FATAL)
491 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
493 MessageBox (NULL, locale_msg, NULL,
494 MB_ICONERROR|MB_SETFOREGROUND);
495 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
500 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
501 if (!(test_level & G_LOG_FLAG_RECURSION))
505 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
507 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
508 #endif /* !G_OS_WIN32 */
512 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
518 g_log (const gchar *log_domain,
519 GLogLevelFlags log_level,
525 va_start (args, format);
526 g_logv (log_domain, log_level, format, args);
531 g_return_if_fail_warning (const char *log_domain,
532 const char *pretty_function,
533 const char *expression)
536 * Omit the prefix used by the PLT-reduction
537 * technique used in GTK+.
539 if (g_str_has_prefix (pretty_function, "IA__"))
540 pretty_function += 4;
542 G_LOG_LEVEL_CRITICAL,
543 "%s: assertion `%s' failed",
549 g_warn_message (const char *domain,
553 const char *warnexpr)
556 g_snprintf (lstr, 32, "%d", line);
558 s = g_strconcat ("(", file, ":", lstr, "):",
559 func, func[0] ? ":" : "",
560 " runtime check failed: (", warnexpr, ")", NULL);
562 s = g_strconcat ("(", file, ":", lstr, "):",
563 func, func[0] ? ":" : "",
564 " ", "code should not be reached", NULL);
565 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
570 g_assert_warning (const char *log_domain,
573 const char *pretty_function,
574 const char *expression)
577 * Omit the prefix used by the PLT-reduction
578 * technique used in GTK+.
580 if (g_str_has_prefix (pretty_function, "IA__"))
581 pretty_function += 4;
585 ? "file %s: line %d (%s): assertion failed: (%s)"
586 : "file %s: line %d (%s): should not be reached",
594 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
596 (wc >= 0x80 && wc < 0xa0)))
599 strdup_convert (const gchar *string,
600 const gchar *charset)
602 if (!g_utf8_validate (string, -1, NULL))
604 GString *gstring = g_string_new ("[Invalid UTF-8] ");
607 for (p = (guchar *)string; *p; p++)
609 if (CHAR_IS_SAFE(*p) &&
610 !(*p == '\r' && *(p + 1) != '\n') &&
612 g_string_append_c (gstring, *p);
614 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
617 return g_string_free (gstring, FALSE);
623 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
628 /* Not thread-safe, but doesn't matter if we print the warning twice
630 static gboolean warned = FALSE;
634 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
638 return g_strdup (string);
643 /* For a radix of 8 we need at most 3 output bytes for 1 input
644 * byte. Additionally we might need up to 2 output bytes for the
645 * readix prefix and 1 byte for the trailing NULL.
647 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
650 format_unsigned (gchar *buf,
658 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
660 if (radix != 8 && radix != 10 && radix != 16)
693 /* Again we can't use g_assert; actually this check should _never_ fail. */
694 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
707 buf[i] = c + 'a' - 10;
714 /* string size big enough to hold level prefix */
715 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
717 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
720 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
721 GLogLevelFlags log_level)
723 gboolean to_stdout = TRUE;
725 /* we may not call _any_ GLib functions here */
727 switch (log_level & G_LOG_LEVEL_MASK)
729 case G_LOG_LEVEL_ERROR:
730 strcpy (level_prefix, "ERROR");
733 case G_LOG_LEVEL_CRITICAL:
734 strcpy (level_prefix, "CRITICAL");
737 case G_LOG_LEVEL_WARNING:
738 strcpy (level_prefix, "WARNING");
741 case G_LOG_LEVEL_MESSAGE:
742 strcpy (level_prefix, "Message");
745 case G_LOG_LEVEL_INFO:
746 strcpy (level_prefix, "INFO");
748 case G_LOG_LEVEL_DEBUG:
749 strcpy (level_prefix, "DEBUG");
754 strcpy (level_prefix, "LOG-");
755 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
758 strcpy (level_prefix, "LOG");
761 if (log_level & G_LOG_FLAG_RECURSION)
762 strcat (level_prefix, " (recursed)");
763 if (log_level & ALERT_LEVELS)
764 strcat (level_prefix, " **");
767 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
769 return to_stdout ? 1 : 2;
773 _g_log_fallback_handler (const gchar *log_domain,
774 GLogLevelFlags log_level,
775 const gchar *message,
776 gpointer unused_data)
778 gchar level_prefix[STRING_BUFFER_SIZE];
780 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
782 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
785 /* we can not call _any_ GLib functions in this fallback handler,
786 * which is why we skip UTF-8 conversion, etc.
787 * since we either recursed or ran out of memory, we're in a pretty
788 * pathologic situation anyways, what we can do is giving the
789 * the process ID unconditionally however.
792 fd = mklevel_prefix (level_prefix, log_level);
794 message = "(NULL) message";
797 format_unsigned (pid_string, getpid (), 10);
801 write_string (fd, "\n");
803 write_string (fd, "\n** ");
806 write_string (fd, "(process:");
807 write_string (fd, pid_string);
808 write_string (fd, "): ");
813 write_string (fd, log_domain);
814 write_string (fd, "-");
816 write_string (fd, level_prefix);
817 write_string (fd, ": ");
818 write_string (fd, message);
820 write_string (fd, "\naborting...\n");
822 write_string (fd, "\n");
826 escape_string (GString *string)
828 const char *p = string->str;
831 while (p < string->str + string->len)
835 wc = g_utf8_get_char_validated (p, -1);
836 if (wc == (gunichar)-1 || wc == (gunichar)-2)
841 pos = p - string->str;
843 /* Emit invalid UTF-8 as hex escapes
845 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
846 g_string_erase (string, pos, 1);
847 g_string_insert (string, pos, tmp);
849 p = string->str + (pos + 4); /* Skip over escape sequence */
856 safe = *(p + 1) == '\n';
860 safe = CHAR_IS_SAFE (wc);
868 pos = p - string->str;
870 /* Largest char we escape is 0x0a, so we don't have to worry
871 * about 8-digit \Uxxxxyyyy
873 tmp = g_strdup_printf ("\\u%04x", wc);
874 g_string_erase (string, pos, g_utf8_next_char (p) - p);
875 g_string_insert (string, pos, tmp);
878 p = string->str + (pos + 6); /* Skip over escape sequence */
881 p = g_utf8_next_char (p);
886 g_log_default_handler (const gchar *log_domain,
887 GLogLevelFlags log_level,
888 const gchar *message,
889 gpointer unused_data)
891 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
892 gchar level_prefix[STRING_BUFFER_SIZE], *string;
896 /* we can be called externally with recursion for whatever reason */
897 if (log_level & G_LOG_FLAG_RECURSION)
899 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
903 g_messages_prefixed_init ();
905 fd = mklevel_prefix (level_prefix, log_level);
907 gstring = g_string_new (NULL);
908 if (log_level & ALERT_LEVELS)
909 g_string_append (gstring, "\n");
911 g_string_append (gstring, "** ");
913 if ((g_log_msg_prefix & log_level) == log_level)
915 const gchar *prg_name = g_get_prgname ();
918 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
920 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
925 g_string_append (gstring, log_domain);
926 g_string_append_c (gstring, '-');
928 g_string_append (gstring, level_prefix);
930 g_string_append (gstring, ": ");
932 g_string_append (gstring, "(NULL) message");
936 const gchar *charset;
938 msg = g_string_new (message);
941 if (g_get_charset (&charset))
942 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
945 string = strdup_convert (msg->str, charset);
946 g_string_append (gstring, string);
950 g_string_free (msg, TRUE);
953 g_string_append (gstring, "\naborting...\n");
955 g_string_append (gstring, "\n");
957 string = g_string_free (gstring, FALSE);
959 write_string (fd, string);
964 g_set_print_handler (GPrintFunc func)
966 GPrintFunc old_print_func;
968 g_mutex_lock (g_messages_lock);
969 old_print_func = glib_print_func;
970 glib_print_func = func;
971 g_mutex_unlock (g_messages_lock);
973 return old_print_func;
977 g_print (const gchar *format,
982 GPrintFunc local_glib_print_func;
984 g_return_if_fail (format != NULL);
986 va_start (args, format);
987 string = g_strdup_vprintf (format, args);
990 g_mutex_lock (g_messages_lock);
991 local_glib_print_func = glib_print_func;
992 g_mutex_unlock (g_messages_lock);
994 if (local_glib_print_func)
995 local_glib_print_func (string);
998 const gchar *charset;
1000 if (g_get_charset (&charset))
1001 fputs (string, stdout); /* charset is UTF-8 already */
1004 gchar *lstring = strdup_convert (string, charset);
1006 fputs (lstring, stdout);
1015 g_set_printerr_handler (GPrintFunc func)
1017 GPrintFunc old_printerr_func;
1019 g_mutex_lock (g_messages_lock);
1020 old_printerr_func = glib_printerr_func;
1021 glib_printerr_func = func;
1022 g_mutex_unlock (g_messages_lock);
1024 return old_printerr_func;
1028 g_printerr (const gchar *format,
1033 GPrintFunc local_glib_printerr_func;
1035 g_return_if_fail (format != NULL);
1037 va_start (args, format);
1038 string = g_strdup_vprintf (format, args);
1041 g_mutex_lock (g_messages_lock);
1042 local_glib_printerr_func = glib_printerr_func;
1043 g_mutex_unlock (g_messages_lock);
1045 if (local_glib_printerr_func)
1046 local_glib_printerr_func (string);
1049 const gchar *charset;
1051 if (g_get_charset (&charset))
1052 fputs (string, stderr); /* charset is UTF-8 already */
1055 gchar *lstring = strdup_convert (string, charset);
1057 fputs (lstring, stderr);
1066 g_printf_string_upper_bound (const gchar *format,
1070 return _g_vsnprintf (&c, 1, format, args) + 1;
1074 _g_messages_thread_init_nomessage (void)
1076 g_messages_lock = g_mutex_new ();
1077 g_log_depth = g_private_new (NULL);
1078 g_messages_prefixed_init ();
1082 gboolean _g_debug_initialized = FALSE;
1083 guint _g_debug_flags = 0;
1086 _g_debug_init (void)
1090 _g_debug_initialized = TRUE;
1092 val = g_getenv ("G_DEBUG");
1095 const GDebugKey keys[] = {
1096 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
1097 {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
1100 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1103 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1105 GLogLevelFlags fatal_mask;
1107 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1108 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1109 g_log_set_always_fatal (fatal_mask);
1112 if (_g_debug_flags & G_DEBUG_FATAL_CRITICALS)
1114 GLogLevelFlags fatal_mask;
1116 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1117 fatal_mask |= G_LOG_LEVEL_CRITICAL;
1118 g_log_set_always_fatal (fatal_mask);
1122 #define __G_MESSAGES_C__
1123 #include "galiasdef.c"