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"
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);
481 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
486 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
487 if (!(test_level & G_LOG_FLAG_RECURSION))
491 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
493 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
494 #endif /* !G_OS_WIN32 */
498 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
504 g_log (const gchar *log_domain,
505 GLogLevelFlags log_level,
511 va_start (args, format);
512 g_logv (log_domain, log_level, format, args);
517 g_return_if_fail_warning (const char *log_domain,
518 const char *pretty_function,
519 const char *expression)
522 * Omit the prefix used by the PLT-reduction
523 * technique used in GTK+.
525 if (g_str_has_prefix (pretty_function, "IA__"))
526 pretty_function += 4;
528 G_LOG_LEVEL_CRITICAL,
529 "%s: assertion `%s' failed",
535 g_assert_warning (const char *log_domain,
538 const char *pretty_function,
539 const char *expression)
542 * Omit the prefix used by the PLT-reduction
543 * technique used in GTK+.
545 if (g_str_has_prefix (pretty_function, "IA__"))
546 pretty_function += 4;
550 ? "file %s: line %d (%s): assertion failed: (%s)"
551 : "file %s: line %d (%s): should not be reached",
559 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
561 (wc >= 0x80 && wc < 0xa0)))
564 strdup_convert (const gchar *string,
565 const gchar *charset)
567 if (!g_utf8_validate (string, -1, NULL))
569 GString *gstring = g_string_new ("[Invalid UTF-8] ");
572 for (p = (guchar *)string; *p; p++)
574 if (CHAR_IS_SAFE(*p) &&
575 !(*p == '\r' && *(p + 1) != '\n') &&
577 g_string_append_c (gstring, *p);
579 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
582 return g_string_free (gstring, FALSE);
588 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
593 /* Not thread-safe, but doesn't matter if we print the warning twice
595 static gboolean warned = FALSE;
599 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
603 return g_strdup (string);
608 /* For a radix of 8 we need at most 3 output bytes for 1 input
609 * byte. Additionally we might need up to 2 output bytes for the
610 * readix prefix and 1 byte for the trailing NULL.
612 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
615 format_unsigned (gchar *buf,
623 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
625 if (radix != 8 && radix != 10 && radix != 16)
658 /* Again we can't use g_assert; actually this check should _never_ fail. */
659 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
672 buf[i] = c + 'a' - 10;
679 /* string size big enough to hold level prefix */
680 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
682 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
685 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
686 GLogLevelFlags log_level)
688 gboolean to_stdout = TRUE;
690 /* we may not call _any_ GLib functions here */
692 switch (log_level & G_LOG_LEVEL_MASK)
694 case G_LOG_LEVEL_ERROR:
695 strcpy (level_prefix, "ERROR");
698 case G_LOG_LEVEL_CRITICAL:
699 strcpy (level_prefix, "CRITICAL");
702 case G_LOG_LEVEL_WARNING:
703 strcpy (level_prefix, "WARNING");
706 case G_LOG_LEVEL_MESSAGE:
707 strcpy (level_prefix, "Message");
710 case G_LOG_LEVEL_INFO:
711 strcpy (level_prefix, "INFO");
713 case G_LOG_LEVEL_DEBUG:
714 strcpy (level_prefix, "DEBUG");
719 strcpy (level_prefix, "LOG-");
720 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
723 strcpy (level_prefix, "LOG");
726 if (log_level & G_LOG_FLAG_RECURSION)
727 strcat (level_prefix, " (recursed)");
728 if (log_level & ALERT_LEVELS)
729 strcat (level_prefix, " **");
732 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
734 return to_stdout ? 1 : 2;
738 _g_log_fallback_handler (const gchar *log_domain,
739 GLogLevelFlags log_level,
740 const gchar *message,
741 gpointer unused_data)
743 gchar level_prefix[STRING_BUFFER_SIZE];
745 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
747 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
750 /* we can not call _any_ GLib functions in this fallback handler,
751 * which is why we skip UTF-8 conversion, etc.
752 * since we either recursed or ran out of memory, we're in a pretty
753 * pathologic situation anyways, what we can do is giving the
754 * the process ID unconditionally however.
757 fd = mklevel_prefix (level_prefix, log_level);
759 message = "(NULL) message";
762 format_unsigned (pid_string, getpid (), 10);
766 write_string (fd, "\n");
768 write_string (fd, "\n** ");
771 write_string (fd, "(process:");
772 write_string (fd, pid_string);
773 write_string (fd, "): ");
778 write_string (fd, log_domain);
779 write_string (fd, "-");
781 write_string (fd, level_prefix);
782 write_string (fd, ": ");
783 write_string (fd, message);
785 write_string (fd, "\naborting...\n");
787 write_string (fd, "\n");
791 escape_string (GString *string)
793 const char *p = string->str;
796 while (p < string->str + string->len)
800 wc = g_utf8_get_char_validated (p, -1);
801 if (wc == (gunichar)-1 || wc == (gunichar)-2)
806 pos = p - string->str;
808 /* Emit invalid UTF-8 as hex escapes
810 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
811 g_string_erase (string, pos, 1);
812 g_string_insert (string, pos, tmp);
814 p = string->str + (pos + 4); /* Skip over escape sequence */
821 safe = *(p + 1) == '\n';
825 safe = CHAR_IS_SAFE (wc);
833 pos = p - string->str;
835 /* Largest char we escape is 0x0a, so we don't have to worry
836 * about 8-digit \Uxxxxyyyy
838 tmp = g_strdup_printf ("\\u%04x", wc);
839 g_string_erase (string, pos, g_utf8_next_char (p) - p);
840 g_string_insert (string, pos, tmp);
843 p = string->str + (pos + 6); /* Skip over escape sequence */
846 p = g_utf8_next_char (p);
851 g_log_default_handler (const gchar *log_domain,
852 GLogLevelFlags log_level,
853 const gchar *message,
854 gpointer unused_data)
856 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
857 gchar level_prefix[STRING_BUFFER_SIZE], *string;
861 /* we can be called externally with recursion for whatever reason */
862 if (log_level & G_LOG_FLAG_RECURSION)
864 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
868 g_messages_prefixed_init ();
870 fd = mklevel_prefix (level_prefix, log_level);
872 gstring = g_string_new (NULL);
873 if (log_level & ALERT_LEVELS)
874 g_string_append (gstring, "\n");
876 g_string_append (gstring, "** ");
878 if ((g_log_msg_prefix & log_level) == log_level)
880 const gchar *prg_name = g_get_prgname ();
884 prg_name = g_strdup (prg_name);
887 if (G_WIN32_HAVE_WIDECHAR_API ())
889 wchar_t buf[MAX_PATH+1];
890 if (GetModuleFileNameW (GetModuleHandle (NULL),
891 buf, G_N_ELEMENTS (buf)) > 0)
893 gchar *utf8_buf = g_utf16_to_utf8 (buf, -1,
897 prg_name = g_path_get_basename (utf8_buf);
904 gchar buf[MAX_PATH+1];
905 if (GetModuleFileNameA (GetModuleHandle (NULL),
906 buf, G_N_ELEMENTS (buf)) > 0)
908 gchar *locale_buf = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
911 prg_name = g_path_get_basename (locale_buf);
920 g_string_append_printf (gstring, "(%s): ", prg_name);
921 g_free ((gchar *) prg_name);
925 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
927 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
933 g_string_append (gstring, log_domain);
934 g_string_append_c (gstring, '-');
936 g_string_append (gstring, level_prefix);
938 g_string_append (gstring, ": ");
940 g_string_append (gstring, "(NULL) message");
944 const gchar *charset;
946 msg = g_string_new (message);
949 if (g_get_charset (&charset))
950 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
953 string = strdup_convert (msg->str, charset);
954 g_string_append (gstring, string);
958 g_string_free (msg, TRUE);
961 g_string_append (gstring, "\naborting...\n");
963 g_string_append (gstring, "\n");
965 string = g_string_free (gstring, FALSE);
967 write_string (fd, string);
972 g_set_print_handler (GPrintFunc func)
974 GPrintFunc old_print_func;
976 g_mutex_lock (g_messages_lock);
977 old_print_func = glib_print_func;
978 glib_print_func = func;
979 g_mutex_unlock (g_messages_lock);
981 return old_print_func;
985 g_print (const gchar *format,
990 GPrintFunc local_glib_print_func;
992 g_return_if_fail (format != NULL);
994 va_start (args, format);
995 string = g_strdup_vprintf (format, args);
998 g_mutex_lock (g_messages_lock);
999 local_glib_print_func = glib_print_func;
1000 g_mutex_unlock (g_messages_lock);
1002 if (local_glib_print_func)
1003 local_glib_print_func (string);
1006 const gchar *charset;
1008 if (g_get_charset (&charset))
1009 fputs (string, stdout); /* charset is UTF-8 already */
1012 gchar *lstring = strdup_convert (string, charset);
1014 fputs (lstring, stdout);
1023 g_set_printerr_handler (GPrintFunc func)
1025 GPrintFunc old_printerr_func;
1027 g_mutex_lock (g_messages_lock);
1028 old_printerr_func = glib_printerr_func;
1029 glib_printerr_func = func;
1030 g_mutex_unlock (g_messages_lock);
1032 return old_printerr_func;
1036 g_printerr (const gchar *format,
1041 GPrintFunc local_glib_printerr_func;
1043 g_return_if_fail (format != NULL);
1045 va_start (args, format);
1046 string = g_strdup_vprintf (format, args);
1049 g_mutex_lock (g_messages_lock);
1050 local_glib_printerr_func = glib_printerr_func;
1051 g_mutex_unlock (g_messages_lock);
1053 if (local_glib_printerr_func)
1054 local_glib_printerr_func (string);
1057 const gchar *charset;
1059 if (g_get_charset (&charset))
1060 fputs (string, stderr); /* charset is UTF-8 already */
1063 gchar *lstring = strdup_convert (string, charset);
1065 fputs (lstring, stderr);
1074 g_printf_string_upper_bound (const gchar *format,
1078 return _g_vsnprintf (&c, 1, format, args) + 1;
1082 _g_messages_thread_init (void)
1084 g_messages_lock = g_mutex_new ();
1085 g_messages_prefixed_init ();
1090 _g_messages_thread_private_init (void)
1092 g_assert (g_log_depth == NULL);
1093 g_log_depth = g_private_new (NULL);
1096 gboolean _g_debug_initialized = FALSE;
1097 guint _g_debug_flags = 0;
1100 _g_debug_init (void)
1104 _g_debug_initialized = TRUE;
1106 val = g_getenv ("G_DEBUG");
1109 static const GDebugKey keys[] = {
1110 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1113 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1116 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1118 GLogLevelFlags fatal_mask;
1120 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1121 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1122 g_log_set_always_fatal (fatal_mask);
1126 #define __G_MESSAGES_C__
1127 #include "galiasdef.c"