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"
51 typedef FILE* GFileDescriptor;
53 typedef gint GFileDescriptor;
56 /* --- structures --- */
57 typedef struct _GLogDomain GLogDomain;
58 typedef struct _GLogHandler GLogHandler;
62 GLogLevelFlags fatal_mask;
63 GLogHandler *handlers;
69 GLogLevelFlags log_level;
76 /* --- variables --- */
77 static GMutex *g_messages_lock = NULL;
78 static GLogDomain *g_log_domains = NULL;
79 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
80 static GPrintFunc glib_print_func = NULL;
81 static GPrintFunc glib_printerr_func = NULL;
82 static GPrivate *g_log_depth = NULL;
83 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
86 /* --- functions --- */
91 # include <process.h> /* For _getpid() */
92 static gboolean win32_keep_fatal_message = FALSE;
94 /* This default message will usually be overwritten. */
95 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
96 * with huge strings, is it?
98 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
99 static gchar *fatal_msg_ptr = fatal_msg_buf;
101 /* Just use stdio. If we're out of memory, we're hosed anyway. */
104 dowrite (GFileDescriptor fd,
108 if (win32_keep_fatal_message)
110 memcpy (fatal_msg_ptr, buf, len);
111 fatal_msg_ptr += len;
116 fwrite (buf, len, 1, fd);
121 #define write(fd, buf, len) dowrite(fd, buf, len)
124 ensure_stdout_valid (void)
126 static gboolean alloc_console_called = FALSE;
129 if (win32_keep_fatal_message)
132 if (!alloc_console_called)
134 handle = (HANDLE) _get_osfhandle (fileno (stdout));
136 if (handle == INVALID_HANDLE_VALUE)
139 alloc_console_called = TRUE;
140 freopen ("CONOUT$", "w", stdout);
146 ensure_stderr_valid (void)
148 static gboolean alloc_console_called = FALSE;
151 if (win32_keep_fatal_message)
154 if (!alloc_console_called)
156 handle = (HANDLE) _get_osfhandle (fileno (stderr));
158 if (handle == INVALID_HANDLE_VALUE)
161 alloc_console_called = TRUE;
162 freopen ("CONOUT$", "w", stderr);
168 #define ensure_stdout_valid() /* Define as empty */
169 #define ensure_stderr_valid()
173 write_string (GFileDescriptor fd,
176 write (fd, string, strlen (string));
180 g_messages_prefixed_init (void)
182 static gboolean initialized = FALSE;
189 val = g_getenv ("G_MESSAGES_PREFIXED");
193 static const GDebugKey keys[] = {
194 { "error", G_LOG_LEVEL_ERROR },
195 { "critical", G_LOG_LEVEL_CRITICAL },
196 { "warning", G_LOG_LEVEL_WARNING },
197 { "message", G_LOG_LEVEL_MESSAGE },
198 { "info", G_LOG_LEVEL_INFO },
199 { "debug", G_LOG_LEVEL_DEBUG }
202 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
208 g_log_find_domain_L (const gchar *log_domain)
210 register GLogDomain *domain;
212 domain = g_log_domains;
215 if (strcmp (domain->log_domain, log_domain) == 0)
217 domain = domain->next;
223 g_log_domain_new_L (const gchar *log_domain)
225 register GLogDomain *domain;
227 domain = g_new (GLogDomain, 1);
228 domain->log_domain = g_strdup (log_domain);
229 domain->fatal_mask = G_LOG_FATAL_MASK;
230 domain->handlers = NULL;
232 domain->next = g_log_domains;
233 g_log_domains = domain;
239 g_log_domain_check_free_L (GLogDomain *domain)
241 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
242 domain->handlers == NULL)
244 register GLogDomain *last, *work;
248 work = g_log_domains;
254 last->next = domain->next;
256 g_log_domains = domain->next;
257 g_free (domain->log_domain);
268 g_log_domain_get_handler_L (GLogDomain *domain,
269 GLogLevelFlags log_level,
272 if (domain && log_level)
274 register GLogHandler *handler;
276 handler = domain->handlers;
279 if ((handler->log_level & log_level) == log_level)
281 *data = handler->data;
282 return handler->log_func;
284 handler = handler->next;
287 return g_log_default_handler;
291 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
293 GLogLevelFlags old_mask;
295 /* restrict the global mask to levels that are known to glib
296 * since this setting applies to all domains
298 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
299 /* force errors to be fatal */
300 fatal_mask |= G_LOG_LEVEL_ERROR;
301 /* remove bogus flag */
302 fatal_mask &= ~G_LOG_FLAG_FATAL;
304 g_mutex_lock (g_messages_lock);
305 old_mask = g_log_always_fatal;
306 g_log_always_fatal = fatal_mask;
307 g_mutex_unlock (g_messages_lock);
313 g_log_set_fatal_mask (const gchar *log_domain,
314 GLogLevelFlags fatal_mask)
316 GLogLevelFlags old_flags;
317 register GLogDomain *domain;
322 /* force errors to be fatal */
323 fatal_mask |= G_LOG_LEVEL_ERROR;
324 /* remove bogus flag */
325 fatal_mask &= ~G_LOG_FLAG_FATAL;
327 g_mutex_lock (g_messages_lock);
329 domain = g_log_find_domain_L (log_domain);
331 domain = g_log_domain_new_L (log_domain);
332 old_flags = domain->fatal_mask;
334 domain->fatal_mask = fatal_mask;
335 g_log_domain_check_free_L (domain);
337 g_mutex_unlock (g_messages_lock);
343 g_log_set_handler (const gchar *log_domain,
344 GLogLevelFlags log_levels,
348 static guint handler_id = 0;
350 GLogHandler *handler;
352 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
353 g_return_val_if_fail (log_func != NULL, 0);
358 handler = g_new (GLogHandler, 1);
360 g_mutex_lock (g_messages_lock);
362 domain = g_log_find_domain_L (log_domain);
364 domain = g_log_domain_new_L (log_domain);
366 handler->id = ++handler_id;
367 handler->log_level = log_levels;
368 handler->log_func = log_func;
369 handler->data = user_data;
370 handler->next = domain->handlers;
371 domain->handlers = handler;
373 g_mutex_unlock (g_messages_lock);
379 g_log_remove_handler (const gchar *log_domain,
382 register GLogDomain *domain;
384 g_return_if_fail (handler_id > 0);
389 g_mutex_lock (g_messages_lock);
390 domain = g_log_find_domain_L (log_domain);
393 GLogHandler *work, *last;
396 work = domain->handlers;
399 if (work->id == handler_id)
402 last->next = work->next;
404 domain->handlers = work->next;
405 g_log_domain_check_free_L (domain);
406 g_mutex_unlock (g_messages_lock);
414 g_mutex_unlock (g_messages_lock);
415 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
416 G_STRLOC, handler_id, log_domain);
420 g_logv (const gchar *log_domain,
421 GLogLevelFlags log_level,
425 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
426 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
429 log_level &= G_LOG_LEVEL_MASK;
433 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
435 register GLogLevelFlags test_level;
438 if (log_level & test_level)
440 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
443 guint domain_fatal_mask;
444 gpointer data = NULL;
447 test_level |= G_LOG_FLAG_FATAL;
449 test_level |= G_LOG_FLAG_RECURSION;
451 /* check recursion and lookup handler */
452 g_mutex_lock (g_messages_lock);
453 domain = g_log_find_domain_L (log_domain ? log_domain : "");
455 test_level |= G_LOG_FLAG_RECURSION;
457 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
458 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
459 test_level |= G_LOG_FLAG_FATAL;
460 if (test_level & G_LOG_FLAG_RECURSION)
461 log_func = _g_log_fallback_handler;
463 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
465 g_mutex_unlock (g_messages_lock);
467 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
469 /* had to defer debug initialization until we can keep track of recursion */
470 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
472 guint orig_test_level = test_level;
475 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
476 test_level |= G_LOG_FLAG_FATAL;
477 if (test_level != orig_test_level)
479 /* need a relookup, not nice, but not too bad either */
480 g_mutex_lock (g_messages_lock);
481 domain = g_log_find_domain_L (log_domain ? log_domain : "");
482 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
484 g_mutex_unlock (g_messages_lock);
488 if (test_level & G_LOG_FLAG_RECURSION)
490 /* we use a stack buffer of fixed size, since we're likely
491 * in an out-of-memory situation
495 size = _g_vsnprintf (buffer, 1024, format, args1);
497 log_func (log_domain, test_level, buffer, data);
501 gchar *msg = g_strdup_vprintf (format, args1);
503 log_func (log_domain, test_level, msg, data);
508 if (test_level & G_LOG_FLAG_FATAL)
511 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
513 MessageBox (NULL, locale_msg, NULL,
514 MB_ICONERROR|MB_SETFOREGROUND);
516 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
517 if (!(test_level & G_LOG_FLAG_RECURSION))
521 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
523 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
527 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
533 g_log (const gchar *log_domain,
534 GLogLevelFlags log_level,
540 va_start (args, format);
541 g_logv (log_domain, log_level, format, args);
545 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
547 (wc >= 0x80 && wc < 0xa0)))
550 strdup_convert (const gchar *string,
551 const gchar *charset)
553 if (!g_utf8_validate (string, -1, NULL))
555 GString *gstring = g_string_new ("[Invalid UTF-8] ");
558 for (p = (guchar *)string; *p; p++)
560 if (CHAR_IS_SAFE(*p) &&
561 !(*p == '\r' && *(p + 1) != '\n') &&
563 g_string_append_c (gstring, *p);
565 g_string_append_printf (gstring, "\\%03o", *p);
568 return g_string_free (gstring, FALSE);
574 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
579 /* Not thread-safe, but doesn't matter if we print the warning twice
581 static gboolean warned = FALSE;
585 ensure_stderr_valid ();
586 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
590 return g_strdup (string);
595 /* For a radix of 8 we need at most 3 output bytes for 1 input
596 * byte. Additionally we might need up to 2 output bytes for the
597 * readix prefix and 1 byte for the trailing NULL.
599 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
602 format_unsigned (gchar *buf,
610 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
612 if (radix != 8 && radix != 10 && radix != 16)
645 /* Again we can't use g_assert; actually this check should _never_ fail. */
646 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
659 buf[i] = c + 'a' - 10;
666 /* string size big enough to hold level prefix */
667 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
669 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
671 static GFileDescriptor
672 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
675 gboolean to_stdout = TRUE;
677 /* we may not call _any_ GLib functions here */
679 switch (log_level & G_LOG_LEVEL_MASK)
681 case G_LOG_LEVEL_ERROR:
682 strcpy (level_prefix, "ERROR");
685 case G_LOG_LEVEL_CRITICAL:
686 strcpy (level_prefix, "CRITICAL");
689 case G_LOG_LEVEL_WARNING:
690 strcpy (level_prefix, "WARNING");
693 case G_LOG_LEVEL_MESSAGE:
694 strcpy (level_prefix, "Message");
697 case G_LOG_LEVEL_INFO:
698 strcpy (level_prefix, "INFO");
700 case G_LOG_LEVEL_DEBUG:
701 strcpy (level_prefix, "DEBUG");
706 strcpy (level_prefix, "LOG-");
707 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
710 strcpy (level_prefix, "LOG");
713 if (log_level & G_LOG_FLAG_RECURSION)
714 strcat (level_prefix, " (recursed)");
715 if (log_level & ALERT_LEVELS)
716 strcat (level_prefix, " **");
719 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
723 ensure_stdout_valid ();
728 ensure_stderr_valid ();
732 return to_stdout ? 1 : 2;
737 _g_log_fallback_handler (const gchar *log_domain,
738 GLogLevelFlags log_level,
739 const gchar *message,
740 gpointer unused_data)
742 gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
743 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
746 /* we can not call _any_ GLib functions in this fallback handler,
747 * which is why we skip UTF-8 conversion, etc.
748 * since we either recursed or ran out of memory, we're in a pretty
749 * pathologic situation anyways, what we can do is giving the
750 * the process ID unconditionally however.
753 fd = mklevel_prefix (level_prefix, log_level);
755 message = "(NULL) message";
757 format_unsigned (pid_string, getpid (), 10);
760 write_string (fd, "\n");
762 write_string (fd, "\n** ");
763 write_string (fd, "(process:");
764 write_string (fd, pid_string);
765 write_string (fd, "): ");
768 write_string (fd, log_domain);
769 write_string (fd, "-");
771 write_string (fd, level_prefix);
772 write_string (fd, ": ");
773 write_string (fd, message);
775 write_string (fd, "\naborting...\n");
777 write_string (fd, "\n");
781 escape_string (GString *string)
783 const char *p = string->str;
786 while (p < string->str + string->len)
790 wc = g_utf8_get_char (p);
793 safe = *(p + 1) == '\n';
797 safe = CHAR_IS_SAFE (wc);
804 g_string_erase (string, p - string->str, g_utf8_next_char (p) - p);
805 /* Largest char we escape is 0x0a, so we don't have to worry
806 * about 8-digit \Uxxxxyyyy
808 tmp = g_strdup_printf ("\\u%04x", wc);
809 g_string_insert (string, p - string->str, tmp);
812 p += 6; /* Skip over escape sequence */
815 p = g_utf8_next_char (p);
820 g_log_default_handler (const gchar *log_domain,
821 GLogLevelFlags log_level,
822 const gchar *message,
823 gpointer unused_data)
825 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
826 gchar level_prefix[STRING_BUFFER_SIZE], *string;
830 /* we can be called externally with recursion for whatever reason */
831 if (log_level & G_LOG_FLAG_RECURSION)
833 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
837 g_messages_prefixed_init ();
839 fd = mklevel_prefix (level_prefix, log_level);
841 gstring = g_string_new (NULL);
842 if (log_level & ALERT_LEVELS)
843 g_string_append (gstring, "\n");
845 g_string_append (gstring, "** ");
847 if ((g_log_msg_prefix & log_level) == log_level)
849 const gchar *prg_name = g_get_prgname ();
852 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
854 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
859 g_string_append (gstring, log_domain);
860 g_string_append_c (gstring, '-');
862 g_string_append (gstring, level_prefix);
864 g_string_append (gstring, ": ");
866 g_string_append (gstring, "(NULL) message");
869 const gchar *charset;
871 if (g_get_charset (&charset))
872 g_string_append (gstring, message); /* charset is UTF-8 already */
875 string = strdup_convert (message, charset);
876 g_string_append (gstring, string);
880 escape_string (gstring);
883 g_string_append (gstring, "\naborting...\n");
885 g_string_append (gstring, "\n");
887 string = g_string_free (gstring, FALSE);
889 write_string (fd, string);
894 g_set_print_handler (GPrintFunc func)
896 GPrintFunc old_print_func;
898 g_mutex_lock (g_messages_lock);
899 old_print_func = glib_print_func;
900 glib_print_func = func;
901 g_mutex_unlock (g_messages_lock);
903 return old_print_func;
907 g_print (const gchar *format,
912 GPrintFunc local_glib_print_func;
914 g_return_if_fail (format != NULL);
916 va_start (args, format);
917 string = g_strdup_vprintf (format, args);
920 g_mutex_lock (g_messages_lock);
921 local_glib_print_func = glib_print_func;
922 g_mutex_unlock (g_messages_lock);
924 if (local_glib_print_func)
925 local_glib_print_func (string);
928 const gchar *charset;
930 ensure_stdout_valid ();
931 if (g_get_charset (&charset))
932 fputs (string, stdout); /* charset is UTF-8 already */
935 gchar *lstring = strdup_convert (string, charset);
937 fputs (lstring, stdout);
946 g_set_printerr_handler (GPrintFunc func)
948 GPrintFunc old_printerr_func;
950 g_mutex_lock (g_messages_lock);
951 old_printerr_func = glib_printerr_func;
952 glib_printerr_func = func;
953 g_mutex_unlock (g_messages_lock);
955 return old_printerr_func;
959 g_printerr (const gchar *format,
964 GPrintFunc local_glib_printerr_func;
966 g_return_if_fail (format != NULL);
968 va_start (args, format);
969 string = g_strdup_vprintf (format, args);
972 g_mutex_lock (g_messages_lock);
973 local_glib_printerr_func = glib_printerr_func;
974 g_mutex_unlock (g_messages_lock);
976 if (local_glib_printerr_func)
977 local_glib_printerr_func (string);
980 const gchar *charset;
982 ensure_stderr_valid ();
983 if (g_get_charset (&charset))
984 fputs (string, stderr); /* charset is UTF-8 already */
987 gchar *lstring = strdup_convert (string, charset);
989 fputs (lstring, stderr);
998 g_printf_string_upper_bound (const gchar *format,
1002 return _g_vsnprintf (&c, 1, format, args) + 1;
1006 _g_messages_thread_init (void)
1008 g_messages_lock = g_mutex_new ();
1009 g_messages_prefixed_init ();
1014 _g_messages_thread_private_init (void)
1016 g_assert (g_log_depth == NULL);
1017 g_log_depth = g_private_new (NULL);
1020 gboolean _g_debug_initialized = FALSE;
1021 guint _g_debug_flags = 0;
1024 _g_debug_init (void)
1028 _g_debug_initialized = TRUE;
1030 val = g_getenv ("G_DEBUG");
1033 static const GDebugKey keys[] = {
1034 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1037 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1040 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1042 GLogLevelFlags fatal_mask;
1044 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1045 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1046 g_log_set_always_fatal (fatal_mask);