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"
49 typedef FILE* GFileDescriptor;
51 typedef gint GFileDescriptor;
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;
84 /* --- functions --- */
89 # include <process.h> /* For _getpid() */
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 * 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;
99 /* Just use stdio. If we're out of memory, we're hosed anyway. */
102 dowrite (GFileDescriptor fd,
106 if (win32_keep_fatal_message)
108 memcpy (fatal_msg_ptr, buf, len);
109 fatal_msg_ptr += len;
114 fwrite (buf, len, 1, fd);
119 #define write(fd, buf, len) dowrite(fd, buf, len)
122 ensure_stdout_valid (void)
124 static gboolean alloc_console_called = FALSE;
127 if (win32_keep_fatal_message)
130 if (!alloc_console_called)
132 handle = GetStdHandle (STD_OUTPUT_HANDLE);
134 if (handle == INVALID_HANDLE_VALUE)
137 alloc_console_called = TRUE;
138 freopen ("CONOUT$", "w", stdout);
144 ensure_stderr_valid (void)
146 static gboolean alloc_console_called = FALSE;
149 if (win32_keep_fatal_message)
152 if (!alloc_console_called)
154 handle = GetStdHandle (STD_ERROR_HANDLE);
156 if (handle == INVALID_HANDLE_VALUE)
159 alloc_console_called = TRUE;
160 freopen ("CONOUT$", "w", stderr);
166 #define ensure_stdout_valid() /* Define as empty */
167 #define ensure_stderr_valid()
171 write_string (GFileDescriptor fd,
174 write (fd, string, strlen (string));
178 g_messages_prefixed_init (void)
180 static gboolean initialized = FALSE;
187 val = g_getenv ("G_MESSAGES_PREFIXED");
191 static const GDebugKey keys[] = {
192 { "error", G_LOG_LEVEL_ERROR },
193 { "critical", G_LOG_LEVEL_CRITICAL },
194 { "warning", G_LOG_LEVEL_WARNING },
195 { "message", G_LOG_LEVEL_MESSAGE },
196 { "info", G_LOG_LEVEL_INFO },
197 { "debug", G_LOG_LEVEL_DEBUG }
200 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
206 g_log_find_domain_L (const gchar *log_domain)
208 register GLogDomain *domain;
210 domain = g_log_domains;
213 if (strcmp (domain->log_domain, log_domain) == 0)
215 domain = domain->next;
221 g_log_domain_new_L (const gchar *log_domain)
223 register GLogDomain *domain;
225 domain = g_new (GLogDomain, 1);
226 domain->log_domain = g_strdup (log_domain);
227 domain->fatal_mask = G_LOG_FATAL_MASK;
228 domain->handlers = NULL;
230 domain->next = g_log_domains;
231 g_log_domains = domain;
237 g_log_domain_check_free_L (GLogDomain *domain)
239 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
240 domain->handlers == NULL)
242 register GLogDomain *last, *work;
246 work = g_log_domains;
252 last->next = domain->next;
254 g_log_domains = domain->next;
255 g_free (domain->log_domain);
266 g_log_domain_get_handler_L (GLogDomain *domain,
267 GLogLevelFlags log_level,
270 if (domain && log_level)
272 register GLogHandler *handler;
274 handler = domain->handlers;
277 if ((handler->log_level & log_level) == log_level)
279 *data = handler->data;
280 return handler->log_func;
282 handler = handler->next;
285 return g_log_default_handler;
289 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
291 GLogLevelFlags old_mask;
293 /* restrict the global mask to levels that are known to glib
294 * since this setting applies to all domains
296 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
297 /* force errors to be fatal */
298 fatal_mask |= G_LOG_LEVEL_ERROR;
299 /* remove bogus flag */
300 fatal_mask &= ~G_LOG_FLAG_FATAL;
302 g_mutex_lock (g_messages_lock);
303 old_mask = g_log_always_fatal;
304 g_log_always_fatal = fatal_mask;
305 g_mutex_unlock (g_messages_lock);
311 g_log_set_fatal_mask (const gchar *log_domain,
312 GLogLevelFlags fatal_mask)
314 GLogLevelFlags old_flags;
315 register GLogDomain *domain;
320 /* force errors to be fatal */
321 fatal_mask |= G_LOG_LEVEL_ERROR;
322 /* remove bogus flag */
323 fatal_mask &= ~G_LOG_FLAG_FATAL;
325 g_mutex_lock (g_messages_lock);
327 domain = g_log_find_domain_L (log_domain);
329 domain = g_log_domain_new_L (log_domain);
330 old_flags = domain->fatal_mask;
332 domain->fatal_mask = fatal_mask;
333 g_log_domain_check_free_L (domain);
335 g_mutex_unlock (g_messages_lock);
341 g_log_set_handler (const gchar *log_domain,
342 GLogLevelFlags log_levels,
346 static guint handler_id = 0;
348 GLogHandler *handler;
350 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
351 g_return_val_if_fail (log_func != NULL, 0);
356 handler = g_new (GLogHandler, 1);
358 g_mutex_lock (g_messages_lock);
360 domain = g_log_find_domain_L (log_domain);
362 domain = g_log_domain_new_L (log_domain);
364 handler->id = ++handler_id;
365 handler->log_level = log_levels;
366 handler->log_func = log_func;
367 handler->data = user_data;
368 handler->next = domain->handlers;
369 domain->handlers = handler;
371 g_mutex_unlock (g_messages_lock);
377 g_log_remove_handler (const gchar *log_domain,
380 register GLogDomain *domain;
382 g_return_if_fail (handler_id > 0);
387 g_mutex_lock (g_messages_lock);
388 domain = g_log_find_domain_L (log_domain);
391 GLogHandler *work, *last;
394 work = domain->handlers;
397 if (work->id == handler_id)
400 last->next = work->next;
402 domain->handlers = work->next;
403 g_log_domain_check_free_L (domain);
404 g_mutex_unlock (g_messages_lock);
412 g_mutex_unlock (g_messages_lock);
413 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
414 G_STRLOC, handler_id, log_domain);
418 g_logv (const gchar *log_domain,
419 GLogLevelFlags log_level,
424 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
425 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
428 log_level &= G_LOG_LEVEL_MASK;
432 /* we use a stack buffer of fixed size, because we might get called
435 _g_vsnprintf (buffer, 1024, format, args1);
437 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
439 register GLogLevelFlags test_level;
442 if (log_level & test_level)
444 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
447 guint domain_fatal_mask;
448 gpointer data = NULL;
451 test_level |= G_LOG_FLAG_FATAL;
453 test_level |= G_LOG_FLAG_RECURSION;
455 /* check recursion and lookup handler */
456 g_mutex_lock (g_messages_lock);
457 domain = g_log_find_domain_L (log_domain ? log_domain : "");
459 test_level |= G_LOG_FLAG_RECURSION;
461 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
462 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
463 test_level |= G_LOG_FLAG_FATAL;
464 if (test_level & G_LOG_FLAG_RECURSION)
465 log_func = _g_log_fallback_handler;
467 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
469 g_mutex_unlock (g_messages_lock);
471 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
473 /* had to defer debug initialization until we can keep track of recursion */
474 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
476 guint orig_test_level = test_level;
479 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
480 test_level |= G_LOG_FLAG_FATAL;
481 if (test_level != orig_test_level)
483 /* need a relookup, not nice, but not too bad either */
484 g_mutex_lock (g_messages_lock);
485 domain = g_log_find_domain_L (log_domain ? log_domain : "");
486 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
488 g_mutex_unlock (g_messages_lock);
492 log_func (log_domain, test_level, buffer, data);
494 if (test_level & G_LOG_FLAG_FATAL)
497 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
499 MessageBox (NULL, locale_msg, NULL, MB_OK);
501 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
502 if (!(test_level & G_LOG_FLAG_RECURSION))
506 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
508 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || 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 strdup_convert (const gchar *string,
532 const gchar *charset)
534 if (!g_utf8_validate (string, -1, NULL))
535 return g_strconcat ("[Invalid UTF-8] ", string, NULL);
540 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
545 /* Not thread-safe, but doesn't matter if we print the warning twice
547 static gboolean warned = FALSE;
551 ensure_stderr_valid ();
552 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
556 return g_strdup (string);
561 /* For a radix of 8 we need at most 3 output bytes for 1 input
562 * byte. Additionally we might need up to 2 output bytes for the
563 * readix prefix and 1 byte for the trailing NULL.
565 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
568 format_unsigned (gchar *buf,
576 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
578 if (radix != 8 && radix != 10 && radix != 16)
611 /* Again we can't use g_assert; actually this check should _never_ fail. */
612 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
625 buf[i] = c + 'a' - 10;
632 /* string size big enough to hold level prefix */
633 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
635 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
637 static GFileDescriptor
638 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
641 gboolean to_stdout = TRUE;
643 /* we may not call _any_ GLib functions here */
645 switch (log_level & G_LOG_LEVEL_MASK)
647 case G_LOG_LEVEL_ERROR:
648 strcpy (level_prefix, "ERROR");
651 case G_LOG_LEVEL_CRITICAL:
652 strcpy (level_prefix, "CRITICAL");
655 case G_LOG_LEVEL_WARNING:
656 strcpy (level_prefix, "WARNING");
659 case G_LOG_LEVEL_MESSAGE:
660 strcpy (level_prefix, "Message");
663 case G_LOG_LEVEL_INFO:
664 strcpy (level_prefix, "INFO");
666 case G_LOG_LEVEL_DEBUG:
667 strcpy (level_prefix, "DEBUG");
672 strcpy (level_prefix, "LOG-");
673 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
676 strcpy (level_prefix, "LOG");
679 if (log_level & G_LOG_FLAG_RECURSION)
680 strcat (level_prefix, " (recursed)");
681 if (log_level & ALERT_LEVELS)
682 strcat (level_prefix, " **");
685 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
689 ensure_stdout_valid ();
694 ensure_stderr_valid ();
698 return to_stdout ? 1 : 2;
703 _g_log_fallback_handler (const gchar *log_domain,
704 GLogLevelFlags log_level,
705 const gchar *message,
706 gpointer unused_data)
708 gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
709 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
712 /* we can not call _any_ GLib functions in this fallback handler,
713 * which is why we skip UTF-8 conversion, etc.
714 * since we either recursed or ran out of memory, we're in a pretty
715 * pathologic situation anyways, what we can do is giving the
716 * the process ID unconditionally however.
719 fd = mklevel_prefix (level_prefix, log_level);
721 message = "(NULL) message";
723 format_unsigned (pid_string, getpid (), 10);
726 write_string (fd, "\n");
728 write_string (fd, "\n** ");
729 write_string (fd, "(process:");
730 write_string (fd, pid_string);
731 write_string (fd, "): ");
734 write_string (fd, log_domain);
735 write_string (fd, "-");
737 write_string (fd, level_prefix);
738 write_string (fd, ": ");
739 write_string (fd, message);
741 write_string (fd, "\naborting...\n");
743 write_string (fd, "\n");
747 g_log_default_handler (const gchar *log_domain,
748 GLogLevelFlags log_level,
749 const gchar *message,
750 gpointer unused_data)
752 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
753 gchar level_prefix[STRING_BUFFER_SIZE], *string;
757 /* we can be called externally with recursion for whatever reason */
758 if (log_level & G_LOG_FLAG_RECURSION)
760 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
764 g_messages_prefixed_init ();
766 fd = mklevel_prefix (level_prefix, log_level);
768 gstring = g_string_new ("");
769 if (log_level & ALERT_LEVELS)
770 g_string_append (gstring, "\n");
772 g_string_append (gstring, "** ");
774 if ((g_log_msg_prefix & log_level) == log_level)
776 const gchar *prg_name = g_get_prgname ();
779 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
781 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
786 g_string_append (gstring, log_domain);
787 g_string_append_c (gstring, '-');
789 g_string_append (gstring, level_prefix);
791 g_string_append (gstring, ": ");
793 g_string_append (gstring, "(NULL) message");
796 const gchar *charset;
798 if (g_get_charset (&charset))
799 g_string_append (gstring, message); /* charset is UTF-8 already */
802 string = strdup_convert (message, charset);
803 g_string_append (gstring, string);
808 g_string_append (gstring, "\naborting...\n");
810 g_string_append (gstring, "\n");
812 string = g_string_free (gstring, FALSE);
814 write_string (fd, string);
819 g_set_print_handler (GPrintFunc func)
821 GPrintFunc old_print_func;
823 g_mutex_lock (g_messages_lock);
824 old_print_func = glib_print_func;
825 glib_print_func = func;
826 g_mutex_unlock (g_messages_lock);
828 return old_print_func;
832 g_print (const gchar *format,
837 GPrintFunc local_glib_print_func;
839 g_return_if_fail (format != NULL);
841 va_start (args, format);
842 string = g_strdup_vprintf (format, args);
845 g_mutex_lock (g_messages_lock);
846 local_glib_print_func = glib_print_func;
847 g_mutex_unlock (g_messages_lock);
849 if (local_glib_print_func)
850 local_glib_print_func (string);
853 const gchar *charset;
855 ensure_stdout_valid ();
856 if (g_get_charset (&charset))
857 fputs (string, stdout); /* charset is UTF-8 already */
860 gchar *lstring = strdup_convert (string, charset);
862 fputs (lstring, stdout);
871 g_set_printerr_handler (GPrintFunc func)
873 GPrintFunc old_printerr_func;
875 g_mutex_lock (g_messages_lock);
876 old_printerr_func = glib_printerr_func;
877 glib_printerr_func = func;
878 g_mutex_unlock (g_messages_lock);
880 return old_printerr_func;
884 g_printerr (const gchar *format,
889 GPrintFunc local_glib_printerr_func;
891 g_return_if_fail (format != NULL);
893 va_start (args, format);
894 string = g_strdup_vprintf (format, args);
897 g_mutex_lock (g_messages_lock);
898 local_glib_printerr_func = glib_printerr_func;
899 g_mutex_unlock (g_messages_lock);
901 if (local_glib_printerr_func)
902 local_glib_printerr_func (string);
905 const gchar *charset;
907 ensure_stderr_valid ();
908 if (g_get_charset (&charset))
909 fputs (string, stderr); /* charset is UTF-8 already */
912 gchar *lstring = strdup_convert (string, charset);
914 fputs (lstring, stderr);
923 g_printf_string_upper_bound (const gchar *format,
927 return _g_vsnprintf (&c, 1, format, args) + 1;
931 g_messages_init (void)
933 g_messages_lock = g_mutex_new ();
934 g_log_depth = g_private_new (NULL);
935 g_messages_prefixed_init ();
939 gboolean _g_debug_initialized = FALSE;
940 guint _g_debug_flags = 0;
947 _g_debug_initialized = TRUE;
949 val = g_getenv ("G_DEBUG");
952 static const GDebugKey keys[] = {
953 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
956 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
959 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
961 GLogLevelFlags fatal_mask;
963 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
964 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
965 g_log_set_always_fatal (fatal_mask);