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,
426 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
427 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
430 log_level &= G_LOG_LEVEL_MASK;
434 /* we use a stack buffer of fixed size, because we might get called
437 _g_vsnprintf (buffer, 1024, format, args1);
439 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
441 register GLogLevelFlags test_level;
444 if (log_level & test_level)
446 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
449 guint domain_fatal_mask;
450 gpointer data = NULL;
453 test_level |= G_LOG_FLAG_FATAL;
455 test_level |= G_LOG_FLAG_RECURSION;
457 /* check recursion and lookup handler */
458 g_mutex_lock (g_messages_lock);
459 domain = g_log_find_domain_L (log_domain ? log_domain : "");
461 test_level |= G_LOG_FLAG_RECURSION;
463 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
464 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
465 test_level |= G_LOG_FLAG_FATAL;
466 if (test_level & G_LOG_FLAG_RECURSION)
467 log_func = _g_log_fallback_handler;
469 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
471 g_mutex_unlock (g_messages_lock);
473 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
475 /* had to defer debug initialization until we can keep track of recursion */
476 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
478 guint orig_test_level = test_level;
481 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
482 test_level |= G_LOG_FLAG_FATAL;
483 if (test_level != orig_test_level)
485 /* need a relookup, not nice, but not too bad either */
486 g_mutex_lock (g_messages_lock);
487 domain = g_log_find_domain_L (log_domain ? log_domain : "");
488 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
490 g_mutex_unlock (g_messages_lock);
494 log_func (log_domain, test_level, buffer, data);
496 if (test_level & G_LOG_FLAG_FATAL)
499 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
501 MessageBox (NULL, locale_msg, NULL, MB_OK);
503 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
504 if (!(test_level & G_LOG_FLAG_RECURSION))
508 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
510 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
514 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
520 g_log (const gchar *log_domain,
521 GLogLevelFlags log_level,
527 va_start (args, format);
528 g_logv (log_domain, log_level, format, args);
533 strdup_convert (const gchar *string,
534 const gchar *charset)
536 if (!g_utf8_validate (string, -1, NULL))
537 return g_strconcat ("[Invalid UTF-8] ", string, NULL);
542 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
547 /* Not thread-safe, but doesn't matter if we print the warning twice
549 static gboolean warned = FALSE;
553 ensure_stderr_valid ();
554 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
558 return g_strdup (string);
563 /* For a radix of 8 we need at most 3 output bytes for 1 input
564 * byte. Additionally we might need up to 2 output bytes for the
565 * readix prefix and 1 byte for the trailing NULL.
567 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
570 format_unsigned (gchar *buf,
578 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
580 if (radix != 8 && radix != 10 && radix != 16)
613 /* Again we can't use g_assert; actually this check should _never_ fail. */
614 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
627 buf[i] = c + 'a' - 10;
634 /* string size big enough to hold level prefix */
635 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
637 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
639 static GFileDescriptor
640 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
643 gboolean to_stdout = TRUE;
645 /* we may not call _any_ GLib functions here */
647 switch (log_level & G_LOG_LEVEL_MASK)
649 case G_LOG_LEVEL_ERROR:
650 strcpy (level_prefix, "ERROR");
653 case G_LOG_LEVEL_CRITICAL:
654 strcpy (level_prefix, "CRITICAL");
657 case G_LOG_LEVEL_WARNING:
658 strcpy (level_prefix, "WARNING");
661 case G_LOG_LEVEL_MESSAGE:
662 strcpy (level_prefix, "Message");
665 case G_LOG_LEVEL_INFO:
666 strcpy (level_prefix, "INFO");
668 case G_LOG_LEVEL_DEBUG:
669 strcpy (level_prefix, "DEBUG");
674 strcpy (level_prefix, "LOG-");
675 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
678 strcpy (level_prefix, "LOG");
681 if (log_level & G_LOG_FLAG_RECURSION)
682 strcat (level_prefix, " (recursed)");
683 if (log_level & ALERT_LEVELS)
684 strcat (level_prefix, " **");
687 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
691 ensure_stdout_valid ();
696 ensure_stderr_valid ();
700 return to_stdout ? 1 : 2;
705 _g_log_fallback_handler (const gchar *log_domain,
706 GLogLevelFlags log_level,
707 const gchar *message,
708 gpointer unused_data)
710 gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
711 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
714 /* we can not call _any_ GLib functions in this fallback handler,
715 * which is why we skip UTF-8 conversion, etc.
716 * since we either recursed or ran out of memory, we're in a pretty
717 * pathologic situation anyways, what we can do is giving the
718 * the process ID unconditionally however.
721 fd = mklevel_prefix (level_prefix, log_level);
723 message = "(NULL) message";
725 format_unsigned (pid_string, getpid (), 10);
728 write_string (fd, "\n");
730 write_string (fd, "\n** ");
731 write_string (fd, "(process:");
732 write_string (fd, pid_string);
733 write_string (fd, "): ");
736 write_string (fd, log_domain);
737 write_string (fd, "-");
739 write_string (fd, level_prefix);
740 write_string (fd, ": ");
741 write_string (fd, message);
743 write_string (fd, "\naborting...\n");
745 write_string (fd, "\n");
749 g_log_default_handler (const gchar *log_domain,
750 GLogLevelFlags log_level,
751 const gchar *message,
752 gpointer unused_data)
754 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
755 gchar level_prefix[STRING_BUFFER_SIZE], *string;
759 /* we can be called externally with recursion for whatever reason */
760 if (log_level & G_LOG_FLAG_RECURSION)
762 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
766 g_messages_prefixed_init ();
768 fd = mklevel_prefix (level_prefix, log_level);
770 gstring = g_string_new ("");
771 if (log_level & ALERT_LEVELS)
772 g_string_append (gstring, "\n");
774 g_string_append (gstring, "** ");
776 if ((g_log_msg_prefix & log_level) == log_level)
778 const gchar *prg_name = g_get_prgname ();
781 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
783 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
788 g_string_append (gstring, log_domain);
789 g_string_append_c (gstring, '-');
791 g_string_append (gstring, level_prefix);
793 g_string_append (gstring, ": ");
795 g_string_append (gstring, "(NULL) message");
798 const gchar *charset;
800 if (g_get_charset (&charset))
801 g_string_append (gstring, message); /* charset is UTF-8 already */
804 string = strdup_convert (message, charset);
805 g_string_append (gstring, string);
810 g_string_append (gstring, "\naborting...\n");
812 g_string_append (gstring, "\n");
814 string = g_string_free (gstring, FALSE);
816 write_string (fd, string);
821 g_set_print_handler (GPrintFunc func)
823 GPrintFunc old_print_func;
825 g_mutex_lock (g_messages_lock);
826 old_print_func = glib_print_func;
827 glib_print_func = func;
828 g_mutex_unlock (g_messages_lock);
830 return old_print_func;
834 g_print (const gchar *format,
839 GPrintFunc local_glib_print_func;
841 g_return_if_fail (format != NULL);
843 va_start (args, format);
844 string = g_strdup_vprintf (format, args);
847 g_mutex_lock (g_messages_lock);
848 local_glib_print_func = glib_print_func;
849 g_mutex_unlock (g_messages_lock);
851 if (local_glib_print_func)
852 local_glib_print_func (string);
855 const gchar *charset;
857 ensure_stdout_valid ();
858 if (g_get_charset (&charset))
859 fputs (string, stdout); /* charset is UTF-8 already */
862 gchar *lstring = strdup_convert (string, charset);
864 fputs (lstring, stdout);
873 g_set_printerr_handler (GPrintFunc func)
875 GPrintFunc old_printerr_func;
877 g_mutex_lock (g_messages_lock);
878 old_printerr_func = glib_printerr_func;
879 glib_printerr_func = func;
880 g_mutex_unlock (g_messages_lock);
882 return old_printerr_func;
886 g_printerr (const gchar *format,
891 GPrintFunc local_glib_printerr_func;
893 g_return_if_fail (format != NULL);
895 va_start (args, format);
896 string = g_strdup_vprintf (format, args);
899 g_mutex_lock (g_messages_lock);
900 local_glib_printerr_func = glib_printerr_func;
901 g_mutex_unlock (g_messages_lock);
903 if (local_glib_printerr_func)
904 local_glib_printerr_func (string);
907 const gchar *charset;
909 ensure_stderr_valid ();
910 if (g_get_charset (&charset))
911 fputs (string, stderr); /* charset is UTF-8 already */
914 gchar *lstring = strdup_convert (string, charset);
916 fputs (lstring, stderr);
925 g_printf_string_upper_bound (const gchar *format,
929 return _g_vsnprintf (&c, 1, format, args) + 1;
933 _g_messages_thread_init (void)
935 g_messages_lock = g_mutex_new ();
936 g_messages_prefixed_init ();
941 _g_messages_thread_private_init (void)
943 g_assert (g_log_depth == NULL);
944 g_log_depth = g_private_new (NULL);
947 gboolean _g_debug_initialized = FALSE;
948 guint _g_debug_flags = 0;
955 _g_debug_initialized = TRUE;
957 val = g_getenv ("G_DEBUG");
960 static const GDebugKey keys[] = {
961 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
964 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
967 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
969 GLogLevelFlags fatal_mask;
971 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
972 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
973 g_log_set_always_fatal (fatal_mask);