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/.
47 #include "gprintfint.h"
48 #include "gthreadinit.h"
54 /* --- structures --- */
55 typedef struct _GLogDomain GLogDomain;
56 typedef struct _GLogHandler GLogHandler;
60 GLogLevelFlags fatal_mask;
61 GLogHandler *handlers;
67 GLogLevelFlags log_level;
74 /* --- variables --- */
75 static GMutex *g_messages_lock = NULL;
76 static GLogDomain *g_log_domains = NULL;
77 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
78 static GPrintFunc glib_print_func = NULL;
79 static GPrintFunc glib_printerr_func = NULL;
80 static GPrivate *g_log_depth = NULL;
81 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
82 static GLogFunc default_log_func = g_log_default_handler;
83 static gpointer default_log_data = NULL;
85 /* --- functions --- */
90 static gboolean win32_keep_fatal_message = FALSE;
92 /* This default message will usually be overwritten. */
93 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
94 * called with huge strings, is it?
96 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
97 static gchar *fatal_msg_ptr = fatal_msg_buf;
105 if (win32_keep_fatal_message)
107 memcpy (fatal_msg_ptr, buf, len);
108 fatal_msg_ptr += len;
113 write (fd, buf, len);
117 #define write(fd, buf, len) dowrite(fd, buf, len)
122 write_string (int fd,
125 write (fd, string, strlen (string));
129 g_messages_prefixed_init (void)
131 static gboolean initialized = FALSE;
138 val = g_getenv ("G_MESSAGES_PREFIXED");
142 static const GDebugKey keys[] = {
143 { "error", G_LOG_LEVEL_ERROR },
144 { "critical", G_LOG_LEVEL_CRITICAL },
145 { "warning", G_LOG_LEVEL_WARNING },
146 { "message", G_LOG_LEVEL_MESSAGE },
147 { "info", G_LOG_LEVEL_INFO },
148 { "debug", G_LOG_LEVEL_DEBUG }
151 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
157 g_log_find_domain_L (const gchar *log_domain)
159 register GLogDomain *domain;
161 domain = g_log_domains;
164 if (strcmp (domain->log_domain, log_domain) == 0)
166 domain = domain->next;
172 g_log_domain_new_L (const gchar *log_domain)
174 register GLogDomain *domain;
176 domain = g_new (GLogDomain, 1);
177 domain->log_domain = g_strdup (log_domain);
178 domain->fatal_mask = G_LOG_FATAL_MASK;
179 domain->handlers = NULL;
181 domain->next = g_log_domains;
182 g_log_domains = domain;
188 g_log_domain_check_free_L (GLogDomain *domain)
190 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
191 domain->handlers == NULL)
193 register GLogDomain *last, *work;
197 work = g_log_domains;
203 last->next = domain->next;
205 g_log_domains = domain->next;
206 g_free (domain->log_domain);
217 g_log_domain_get_handler_L (GLogDomain *domain,
218 GLogLevelFlags log_level,
221 if (domain && log_level)
223 register GLogHandler *handler;
225 handler = domain->handlers;
228 if ((handler->log_level & log_level) == log_level)
230 *data = handler->data;
231 return handler->log_func;
233 handler = handler->next;
237 *data = default_log_data;
238 return default_log_func;
242 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
244 GLogLevelFlags old_mask;
246 /* restrict the global mask to levels that are known to glib
247 * since this setting applies to all domains
249 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
250 /* force errors to be fatal */
251 fatal_mask |= G_LOG_LEVEL_ERROR;
252 /* remove bogus flag */
253 fatal_mask &= ~G_LOG_FLAG_FATAL;
255 g_mutex_lock (g_messages_lock);
256 old_mask = g_log_always_fatal;
257 g_log_always_fatal = fatal_mask;
258 g_mutex_unlock (g_messages_lock);
264 g_log_set_fatal_mask (const gchar *log_domain,
265 GLogLevelFlags fatal_mask)
267 GLogLevelFlags old_flags;
268 register GLogDomain *domain;
273 /* force errors to be fatal */
274 fatal_mask |= G_LOG_LEVEL_ERROR;
275 /* remove bogus flag */
276 fatal_mask &= ~G_LOG_FLAG_FATAL;
278 g_mutex_lock (g_messages_lock);
280 domain = g_log_find_domain_L (log_domain);
282 domain = g_log_domain_new_L (log_domain);
283 old_flags = domain->fatal_mask;
285 domain->fatal_mask = fatal_mask;
286 g_log_domain_check_free_L (domain);
288 g_mutex_unlock (g_messages_lock);
294 g_log_set_handler (const gchar *log_domain,
295 GLogLevelFlags log_levels,
299 static guint handler_id = 0;
301 GLogHandler *handler;
303 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
304 g_return_val_if_fail (log_func != NULL, 0);
309 handler = g_new (GLogHandler, 1);
311 g_mutex_lock (g_messages_lock);
313 domain = g_log_find_domain_L (log_domain);
315 domain = g_log_domain_new_L (log_domain);
317 handler->id = ++handler_id;
318 handler->log_level = log_levels;
319 handler->log_func = log_func;
320 handler->data = user_data;
321 handler->next = domain->handlers;
322 domain->handlers = handler;
324 g_mutex_unlock (g_messages_lock);
330 g_log_set_default_handler (GLogFunc log_func,
333 GLogFunc old_log_func;
335 g_mutex_lock (g_messages_lock);
336 old_log_func = default_log_func;
337 default_log_func = log_func;
338 default_log_data = user_data;
339 g_mutex_unlock (g_messages_lock);
345 g_log_remove_handler (const gchar *log_domain,
348 register GLogDomain *domain;
350 g_return_if_fail (handler_id > 0);
355 g_mutex_lock (g_messages_lock);
356 domain = g_log_find_domain_L (log_domain);
359 GLogHandler *work, *last;
362 work = domain->handlers;
365 if (work->id == handler_id)
368 last->next = work->next;
370 domain->handlers = work->next;
371 g_log_domain_check_free_L (domain);
372 g_mutex_unlock (g_messages_lock);
380 g_mutex_unlock (g_messages_lock);
381 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
382 G_STRLOC, handler_id, log_domain);
386 g_logv (const gchar *log_domain,
387 GLogLevelFlags log_level,
391 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
392 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
395 log_level &= G_LOG_LEVEL_MASK;
399 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
401 register GLogLevelFlags test_level;
404 if (log_level & test_level)
406 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
409 GLogLevelFlags domain_fatal_mask;
410 gpointer data = NULL;
413 test_level |= G_LOG_FLAG_FATAL;
415 test_level |= G_LOG_FLAG_RECURSION;
417 /* check recursion and lookup handler */
418 g_mutex_lock (g_messages_lock);
419 domain = g_log_find_domain_L (log_domain ? log_domain : "");
421 test_level |= G_LOG_FLAG_RECURSION;
423 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
424 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
425 test_level |= G_LOG_FLAG_FATAL;
426 if (test_level & G_LOG_FLAG_RECURSION)
427 log_func = _g_log_fallback_handler;
429 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
431 g_mutex_unlock (g_messages_lock);
433 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
435 /* had to defer debug initialization until we can keep track of recursion */
436 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
438 GLogLevelFlags orig_test_level = test_level;
441 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
442 test_level |= G_LOG_FLAG_FATAL;
443 if (test_level != orig_test_level)
445 /* need a relookup, not nice, but not too bad either */
446 g_mutex_lock (g_messages_lock);
447 domain = g_log_find_domain_L (log_domain ? log_domain : "");
448 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
450 g_mutex_unlock (g_messages_lock);
454 if (test_level & G_LOG_FLAG_RECURSION)
456 /* we use a stack buffer of fixed size, since we're likely
457 * in an out-of-memory situation
461 size = _g_vsnprintf (buffer, 1024, format, args1);
463 log_func (log_domain, test_level, buffer, data);
467 gchar *msg = g_strdup_vprintf (format, args1);
469 log_func (log_domain, test_level, msg, data);
474 if (test_level & G_LOG_FLAG_FATAL)
477 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
479 MessageBox (NULL, locale_msg, NULL,
480 MB_ICONERROR|MB_SETFOREGROUND);
482 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
483 if (!(test_level & G_LOG_FLAG_RECURSION))
487 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
489 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
493 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
499 g_log (const gchar *log_domain,
500 GLogLevelFlags log_level,
506 va_start (args, format);
507 g_logv (log_domain, log_level, format, args);
512 g_return_if_fail_warning (const char *log_domain,
513 const char *pretty_function,
514 const char *expression)
517 * Omit the prefix used by the PLT-reduction
518 * technique used in GTK+.
520 if (g_str_has_prefix (pretty_function, "IA__"))
521 pretty_function += 4;
523 G_LOG_LEVEL_CRITICAL,
524 "%s: assertion `%s' failed",
530 g_assert_warning (const char *log_domain,
533 const char *pretty_function,
534 const char *expression)
537 * Omit the prefix used by the PLT-reduction
538 * technique used in GTK+.
540 if (g_str_has_prefix (pretty_function, "IA__"))
541 pretty_function += 4;
545 ? "file %s: line %d (%s): assertion failed: (%s)"
546 : "file %s: line %d (%s): should not be reached",
553 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
555 (wc >= 0x80 && wc < 0xa0)))
558 strdup_convert (const gchar *string,
559 const gchar *charset)
561 if (!g_utf8_validate (string, -1, NULL))
563 GString *gstring = g_string_new ("[Invalid UTF-8] ");
566 for (p = (guchar *)string; *p; p++)
568 if (CHAR_IS_SAFE(*p) &&
569 !(*p == '\r' && *(p + 1) != '\n') &&
571 g_string_append_c (gstring, *p);
573 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
576 return g_string_free (gstring, FALSE);
582 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
587 /* Not thread-safe, but doesn't matter if we print the warning twice
589 static gboolean warned = FALSE;
593 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
597 return g_strdup (string);
602 /* For a radix of 8 we need at most 3 output bytes for 1 input
603 * byte. Additionally we might need up to 2 output bytes for the
604 * readix prefix and 1 byte for the trailing NULL.
606 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
609 format_unsigned (gchar *buf,
617 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
619 if (radix != 8 && radix != 10 && radix != 16)
652 /* Again we can't use g_assert; actually this check should _never_ fail. */
653 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
666 buf[i] = c + 'a' - 10;
673 /* string size big enough to hold level prefix */
674 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
676 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
679 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
680 GLogLevelFlags log_level)
682 gboolean to_stdout = TRUE;
684 /* we may not call _any_ GLib functions here */
686 switch (log_level & G_LOG_LEVEL_MASK)
688 case G_LOG_LEVEL_ERROR:
689 strcpy (level_prefix, "ERROR");
692 case G_LOG_LEVEL_CRITICAL:
693 strcpy (level_prefix, "CRITICAL");
696 case G_LOG_LEVEL_WARNING:
697 strcpy (level_prefix, "WARNING");
700 case G_LOG_LEVEL_MESSAGE:
701 strcpy (level_prefix, "Message");
704 case G_LOG_LEVEL_INFO:
705 strcpy (level_prefix, "INFO");
707 case G_LOG_LEVEL_DEBUG:
708 strcpy (level_prefix, "DEBUG");
713 strcpy (level_prefix, "LOG-");
714 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
717 strcpy (level_prefix, "LOG");
720 if (log_level & G_LOG_FLAG_RECURSION)
721 strcat (level_prefix, " (recursed)");
722 if (log_level & ALERT_LEVELS)
723 strcat (level_prefix, " **");
726 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
728 return to_stdout ? 1 : 2;
732 _g_log_fallback_handler (const gchar *log_domain,
733 GLogLevelFlags log_level,
734 const gchar *message,
735 gpointer unused_data)
737 gchar level_prefix[STRING_BUFFER_SIZE];
739 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
741 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
744 /* we can not call _any_ GLib functions in this fallback handler,
745 * which is why we skip UTF-8 conversion, etc.
746 * since we either recursed or ran out of memory, we're in a pretty
747 * pathologic situation anyways, what we can do is giving the
748 * the process ID unconditionally however.
751 fd = mklevel_prefix (level_prefix, log_level);
753 message = "(NULL) message";
756 format_unsigned (pid_string, getpid (), 10);
760 write_string (fd, "\n");
762 write_string (fd, "\n** ");
765 write_string (fd, "(process:");
766 write_string (fd, pid_string);
767 write_string (fd, "): ");
772 write_string (fd, log_domain);
773 write_string (fd, "-");
775 write_string (fd, level_prefix);
776 write_string (fd, ": ");
777 write_string (fd, message);
779 write_string (fd, "\naborting...\n");
781 write_string (fd, "\n");
785 escape_string (GString *string)
787 const char *p = string->str;
790 while (p < string->str + string->len)
794 wc = g_utf8_get_char_validated (p, -1);
795 if (wc == (gunichar)-1 || wc == (gunichar)-2)
800 pos = p - string->str;
802 /* Emit invalid UTF-8 as hex escapes
804 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
805 g_string_erase (string, pos, 1);
806 g_string_insert (string, pos, tmp);
808 p = string->str + (pos + 4); /* Skip over escape sequence */
815 safe = *(p + 1) == '\n';
819 safe = CHAR_IS_SAFE (wc);
827 pos = p - string->str;
829 /* Largest char we escape is 0x0a, so we don't have to worry
830 * about 8-digit \Uxxxxyyyy
832 tmp = g_strdup_printf ("\\u%04x", wc);
833 g_string_erase (string, pos, g_utf8_next_char (p) - p);
834 g_string_insert (string, pos, tmp);
837 p = string->str + (pos + 6); /* Skip over escape sequence */
840 p = g_utf8_next_char (p);
845 g_log_default_handler (const gchar *log_domain,
846 GLogLevelFlags log_level,
847 const gchar *message,
848 gpointer unused_data)
850 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
851 gchar level_prefix[STRING_BUFFER_SIZE], *string;
855 /* we can be called externally with recursion for whatever reason */
856 if (log_level & G_LOG_FLAG_RECURSION)
858 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
862 g_messages_prefixed_init ();
864 fd = mklevel_prefix (level_prefix, log_level);
866 gstring = g_string_new (NULL);
867 if (log_level & ALERT_LEVELS)
868 g_string_append (gstring, "\n");
870 g_string_append (gstring, "** ");
872 if ((g_log_msg_prefix & log_level) == log_level)
874 const gchar *prg_name = g_get_prgname ();
878 prg_name = g_strdup (prg_name);
881 if (G_WIN32_HAVE_WIDECHAR_API ())
883 wchar_t buf[MAX_PATH+1];
884 if (GetModuleFileNameW (GetModuleHandle (NULL),
885 buf, G_N_ELEMENTS (buf)) > 0)
887 gchar *utf8_buf = g_utf16_to_utf8 (buf, -1,
891 prg_name = g_path_get_basename (utf8_buf);
898 gchar buf[MAX_PATH+1];
899 if (GetModuleFileNameA (GetModuleHandle (NULL),
900 buf, G_N_ELEMENTS (buf)) > 0)
902 gchar *locale_buf = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
905 prg_name = g_path_get_basename (locale_buf);
914 g_string_append_printf (gstring, "(%s): ", prg_name);
915 g_free ((gchar *) prg_name);
919 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
921 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
927 g_string_append (gstring, log_domain);
928 g_string_append_c (gstring, '-');
930 g_string_append (gstring, level_prefix);
932 g_string_append (gstring, ": ");
934 g_string_append (gstring, "(NULL) message");
938 const gchar *charset;
940 msg = g_string_new (message);
943 if (g_get_charset (&charset))
944 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
947 string = strdup_convert (msg->str, charset);
948 g_string_append (gstring, string);
952 g_string_free (msg, TRUE);
955 g_string_append (gstring, "\naborting...\n");
957 g_string_append (gstring, "\n");
959 string = g_string_free (gstring, FALSE);
961 write_string (fd, string);
966 g_set_print_handler (GPrintFunc func)
968 GPrintFunc old_print_func;
970 g_mutex_lock (g_messages_lock);
971 old_print_func = glib_print_func;
972 glib_print_func = func;
973 g_mutex_unlock (g_messages_lock);
975 return old_print_func;
979 g_print (const gchar *format,
984 GPrintFunc local_glib_print_func;
986 g_return_if_fail (format != NULL);
988 va_start (args, format);
989 string = g_strdup_vprintf (format, args);
992 g_mutex_lock (g_messages_lock);
993 local_glib_print_func = glib_print_func;
994 g_mutex_unlock (g_messages_lock);
996 if (local_glib_print_func)
997 local_glib_print_func (string);
1000 const gchar *charset;
1002 if (g_get_charset (&charset))
1003 fputs (string, stdout); /* charset is UTF-8 already */
1006 gchar *lstring = strdup_convert (string, charset);
1008 fputs (lstring, stdout);
1017 g_set_printerr_handler (GPrintFunc func)
1019 GPrintFunc old_printerr_func;
1021 g_mutex_lock (g_messages_lock);
1022 old_printerr_func = glib_printerr_func;
1023 glib_printerr_func = func;
1024 g_mutex_unlock (g_messages_lock);
1026 return old_printerr_func;
1030 g_printerr (const gchar *format,
1035 GPrintFunc local_glib_printerr_func;
1037 g_return_if_fail (format != NULL);
1039 va_start (args, format);
1040 string = g_strdup_vprintf (format, args);
1043 g_mutex_lock (g_messages_lock);
1044 local_glib_printerr_func = glib_printerr_func;
1045 g_mutex_unlock (g_messages_lock);
1047 if (local_glib_printerr_func)
1048 local_glib_printerr_func (string);
1051 const gchar *charset;
1053 if (g_get_charset (&charset))
1054 fputs (string, stderr); /* charset is UTF-8 already */
1057 gchar *lstring = strdup_convert (string, charset);
1059 fputs (lstring, stderr);
1068 g_printf_string_upper_bound (const gchar *format,
1072 return _g_vsnprintf (&c, 1, format, args) + 1;
1076 _g_messages_thread_init (void)
1078 g_messages_lock = g_mutex_new ();
1079 g_messages_prefixed_init ();
1084 _g_messages_thread_private_init (void)
1086 g_assert (g_log_depth == NULL);
1087 g_log_depth = g_private_new (NULL);
1090 gboolean _g_debug_initialized = FALSE;
1091 guint _g_debug_flags = 0;
1094 _g_debug_init (void)
1098 _g_debug_initialized = TRUE;
1100 val = g_getenv ("G_DEBUG");
1103 static const GDebugKey keys[] = {
1104 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1107 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1110 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1112 GLogLevelFlags fatal_mask;
1114 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1115 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1116 g_log_set_always_fatal (fatal_mask);