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"
50 typedef FILE* GFileDescriptor;
52 typedef gint GFileDescriptor;
55 /* --- structures --- */
56 typedef struct _GLogDomain GLogDomain;
57 typedef struct _GLogHandler GLogHandler;
61 GLogLevelFlags fatal_mask;
62 GLogHandler *handlers;
68 GLogLevelFlags log_level;
75 /* --- variables --- */
76 static GMutex *g_messages_lock = NULL;
77 static GLogDomain *g_log_domains = NULL;
78 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
79 static GPrintFunc glib_print_func = NULL;
80 static GPrintFunc glib_printerr_func = NULL;
81 static GPrivate *g_log_depth = NULL;
82 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
85 /* --- functions --- */
90 # include <process.h> /* For _getpid() */
91 static gboolean win32_keep_fatal_message = FALSE;
93 /* This default message will usually be overwritten. */
94 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
95 * with huge strings, is it?
97 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
98 static gchar *fatal_msg_ptr = fatal_msg_buf;
100 /* Just use stdio. If we're out of memory, we're hosed anyway. */
103 dowrite (GFileDescriptor fd,
107 if (win32_keep_fatal_message)
109 memcpy (fatal_msg_ptr, buf, len);
110 fatal_msg_ptr += len;
115 fwrite (buf, len, 1, fd);
120 #define write(fd, buf, len) dowrite(fd, buf, len)
123 ensure_stdout_valid (void)
125 static gboolean alloc_console_called = FALSE;
128 if (win32_keep_fatal_message)
131 if (!alloc_console_called)
133 handle = (HANDLE) _get_osfhandle (fileno (stdout));
135 if (handle == INVALID_HANDLE_VALUE)
138 alloc_console_called = TRUE;
139 freopen ("CONOUT$", "w", stdout);
145 ensure_stderr_valid (void)
147 static gboolean alloc_console_called = FALSE;
150 if (win32_keep_fatal_message)
153 if (!alloc_console_called)
155 handle = (HANDLE) _get_osfhandle (fileno (stderr));
157 if (handle == INVALID_HANDLE_VALUE)
160 alloc_console_called = TRUE;
161 freopen ("CONOUT$", "w", stderr);
167 #define ensure_stdout_valid() /* Define as empty */
168 #define ensure_stderr_valid()
172 write_string (GFileDescriptor fd,
175 write (fd, string, strlen (string));
179 g_messages_prefixed_init (void)
181 static gboolean initialized = FALSE;
188 val = g_getenv ("G_MESSAGES_PREFIXED");
192 static const GDebugKey keys[] = {
193 { "error", G_LOG_LEVEL_ERROR },
194 { "critical", G_LOG_LEVEL_CRITICAL },
195 { "warning", G_LOG_LEVEL_WARNING },
196 { "message", G_LOG_LEVEL_MESSAGE },
197 { "info", G_LOG_LEVEL_INFO },
198 { "debug", G_LOG_LEVEL_DEBUG }
201 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
207 g_log_find_domain_L (const gchar *log_domain)
209 register GLogDomain *domain;
211 domain = g_log_domains;
214 if (strcmp (domain->log_domain, log_domain) == 0)
216 domain = domain->next;
222 g_log_domain_new_L (const gchar *log_domain)
224 register GLogDomain *domain;
226 domain = g_new (GLogDomain, 1);
227 domain->log_domain = g_strdup (log_domain);
228 domain->fatal_mask = G_LOG_FATAL_MASK;
229 domain->handlers = NULL;
231 domain->next = g_log_domains;
232 g_log_domains = domain;
238 g_log_domain_check_free_L (GLogDomain *domain)
240 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
241 domain->handlers == NULL)
243 register GLogDomain *last, *work;
247 work = g_log_domains;
253 last->next = domain->next;
255 g_log_domains = domain->next;
256 g_free (domain->log_domain);
267 g_log_domain_get_handler_L (GLogDomain *domain,
268 GLogLevelFlags log_level,
271 if (domain && log_level)
273 register GLogHandler *handler;
275 handler = domain->handlers;
278 if ((handler->log_level & log_level) == log_level)
280 *data = handler->data;
281 return handler->log_func;
283 handler = handler->next;
286 return g_log_default_handler;
290 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
292 GLogLevelFlags old_mask;
294 /* restrict the global mask to levels that are known to glib
295 * since this setting applies to all domains
297 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
298 /* force errors to be fatal */
299 fatal_mask |= G_LOG_LEVEL_ERROR;
300 /* remove bogus flag */
301 fatal_mask &= ~G_LOG_FLAG_FATAL;
303 g_mutex_lock (g_messages_lock);
304 old_mask = g_log_always_fatal;
305 g_log_always_fatal = fatal_mask;
306 g_mutex_unlock (g_messages_lock);
312 g_log_set_fatal_mask (const gchar *log_domain,
313 GLogLevelFlags fatal_mask)
315 GLogLevelFlags old_flags;
316 register GLogDomain *domain;
321 /* force errors to be fatal */
322 fatal_mask |= G_LOG_LEVEL_ERROR;
323 /* remove bogus flag */
324 fatal_mask &= ~G_LOG_FLAG_FATAL;
326 g_mutex_lock (g_messages_lock);
328 domain = g_log_find_domain_L (log_domain);
330 domain = g_log_domain_new_L (log_domain);
331 old_flags = domain->fatal_mask;
333 domain->fatal_mask = fatal_mask;
334 g_log_domain_check_free_L (domain);
336 g_mutex_unlock (g_messages_lock);
342 g_log_set_handler (const gchar *log_domain,
343 GLogLevelFlags log_levels,
347 static guint handler_id = 0;
349 GLogHandler *handler;
351 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
352 g_return_val_if_fail (log_func != NULL, 0);
357 handler = g_new (GLogHandler, 1);
359 g_mutex_lock (g_messages_lock);
361 domain = g_log_find_domain_L (log_domain);
363 domain = g_log_domain_new_L (log_domain);
365 handler->id = ++handler_id;
366 handler->log_level = log_levels;
367 handler->log_func = log_func;
368 handler->data = user_data;
369 handler->next = domain->handlers;
370 domain->handlers = handler;
372 g_mutex_unlock (g_messages_lock);
378 g_log_remove_handler (const gchar *log_domain,
381 register GLogDomain *domain;
383 g_return_if_fail (handler_id > 0);
388 g_mutex_lock (g_messages_lock);
389 domain = g_log_find_domain_L (log_domain);
392 GLogHandler *work, *last;
395 work = domain->handlers;
398 if (work->id == handler_id)
401 last->next = work->next;
403 domain->handlers = work->next;
404 g_log_domain_check_free_L (domain);
405 g_mutex_unlock (g_messages_lock);
413 g_mutex_unlock (g_messages_lock);
414 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
415 G_STRLOC, handler_id, log_domain);
419 g_logv (const gchar *log_domain,
420 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 /* we use a stack buffer of fixed size, because we might get called
436 _g_vsnprintf (buffer, 1024, format, args1);
438 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
440 register GLogLevelFlags test_level;
443 if (log_level & test_level)
445 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
448 guint domain_fatal_mask;
449 gpointer data = NULL;
452 test_level |= G_LOG_FLAG_FATAL;
454 test_level |= G_LOG_FLAG_RECURSION;
456 /* check recursion and lookup handler */
457 g_mutex_lock (g_messages_lock);
458 domain = g_log_find_domain_L (log_domain ? log_domain : "");
460 test_level |= G_LOG_FLAG_RECURSION;
462 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
463 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
464 test_level |= G_LOG_FLAG_FATAL;
465 if (test_level & G_LOG_FLAG_RECURSION)
466 log_func = _g_log_fallback_handler;
468 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
470 g_mutex_unlock (g_messages_lock);
472 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
474 /* had to defer debug initialization until we can keep track of recursion */
475 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
477 guint orig_test_level = test_level;
480 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
481 test_level |= G_LOG_FLAG_FATAL;
482 if (test_level != orig_test_level)
484 /* need a relookup, not nice, but not too bad either */
485 g_mutex_lock (g_messages_lock);
486 domain = g_log_find_domain_L (log_domain ? log_domain : "");
487 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
489 g_mutex_unlock (g_messages_lock);
493 log_func (log_domain, test_level, buffer, data);
495 if (test_level & G_LOG_FLAG_FATAL)
498 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
500 MessageBox (NULL, locale_msg, NULL, MB_OK);
502 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
503 if (!(test_level & G_LOG_FLAG_RECURSION))
507 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
509 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
513 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
519 g_log (const gchar *log_domain,
520 GLogLevelFlags log_level,
526 va_start (args, format);
527 g_logv (log_domain, log_level, format, args);
532 strdup_convert (const gchar *string,
533 const gchar *charset)
535 if (!g_utf8_validate (string, -1, NULL))
536 return g_strconcat ("[Invalid UTF-8] ", string, NULL);
541 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
546 /* Not thread-safe, but doesn't matter if we print the warning twice
548 static gboolean warned = FALSE;
552 ensure_stderr_valid ();
553 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
557 return g_strdup (string);
562 /* For a radix of 8 we need at most 3 output bytes for 1 input
563 * byte. Additionally we might need up to 2 output bytes for the
564 * readix prefix and 1 byte for the trailing NULL.
566 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
569 format_unsigned (gchar *buf,
577 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
579 if (radix != 8 && radix != 10 && radix != 16)
612 /* Again we can't use g_assert; actually this check should _never_ fail. */
613 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
626 buf[i] = c + 'a' - 10;
633 /* string size big enough to hold level prefix */
634 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
636 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
638 static GFileDescriptor
639 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
642 gboolean to_stdout = TRUE;
644 /* we may not call _any_ GLib functions here */
646 switch (log_level & G_LOG_LEVEL_MASK)
648 case G_LOG_LEVEL_ERROR:
649 strcpy (level_prefix, "ERROR");
652 case G_LOG_LEVEL_CRITICAL:
653 strcpy (level_prefix, "CRITICAL");
656 case G_LOG_LEVEL_WARNING:
657 strcpy (level_prefix, "WARNING");
660 case G_LOG_LEVEL_MESSAGE:
661 strcpy (level_prefix, "Message");
664 case G_LOG_LEVEL_INFO:
665 strcpy (level_prefix, "INFO");
667 case G_LOG_LEVEL_DEBUG:
668 strcpy (level_prefix, "DEBUG");
673 strcpy (level_prefix, "LOG-");
674 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
677 strcpy (level_prefix, "LOG");
680 if (log_level & G_LOG_FLAG_RECURSION)
681 strcat (level_prefix, " (recursed)");
682 if (log_level & ALERT_LEVELS)
683 strcat (level_prefix, " **");
686 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
690 ensure_stdout_valid ();
695 ensure_stderr_valid ();
699 return to_stdout ? 1 : 2;
704 _g_log_fallback_handler (const gchar *log_domain,
705 GLogLevelFlags log_level,
706 const gchar *message,
707 gpointer unused_data)
709 gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
710 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
713 /* we can not call _any_ GLib functions in this fallback handler,
714 * which is why we skip UTF-8 conversion, etc.
715 * since we either recursed or ran out of memory, we're in a pretty
716 * pathologic situation anyways, what we can do is giving the
717 * the process ID unconditionally however.
720 fd = mklevel_prefix (level_prefix, log_level);
722 message = "(NULL) message";
724 format_unsigned (pid_string, getpid (), 10);
727 write_string (fd, "\n");
729 write_string (fd, "\n** ");
730 write_string (fd, "(process:");
731 write_string (fd, pid_string);
732 write_string (fd, "): ");
735 write_string (fd, log_domain);
736 write_string (fd, "-");
738 write_string (fd, level_prefix);
739 write_string (fd, ": ");
740 write_string (fd, message);
742 write_string (fd, "\naborting...\n");
744 write_string (fd, "\n");
748 g_log_default_handler (const gchar *log_domain,
749 GLogLevelFlags log_level,
750 const gchar *message,
751 gpointer unused_data)
753 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
754 gchar level_prefix[STRING_BUFFER_SIZE], *string;
758 /* we can be called externally with recursion for whatever reason */
759 if (log_level & G_LOG_FLAG_RECURSION)
761 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
765 g_messages_prefixed_init ();
767 fd = mklevel_prefix (level_prefix, log_level);
769 gstring = g_string_new ("");
770 if (log_level & ALERT_LEVELS)
771 g_string_append (gstring, "\n");
773 g_string_append (gstring, "** ");
775 if ((g_log_msg_prefix & log_level) == log_level)
777 const gchar *prg_name = g_get_prgname ();
780 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
782 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
787 g_string_append (gstring, log_domain);
788 g_string_append_c (gstring, '-');
790 g_string_append (gstring, level_prefix);
792 g_string_append (gstring, ": ");
794 g_string_append (gstring, "(NULL) message");
797 const gchar *charset;
799 if (g_get_charset (&charset))
800 g_string_append (gstring, message); /* charset is UTF-8 already */
803 string = strdup_convert (message, charset);
804 g_string_append (gstring, string);
809 g_string_append (gstring, "\naborting...\n");
811 g_string_append (gstring, "\n");
813 string = g_string_free (gstring, FALSE);
815 write_string (fd, string);
820 g_set_print_handler (GPrintFunc func)
822 GPrintFunc old_print_func;
824 g_mutex_lock (g_messages_lock);
825 old_print_func = glib_print_func;
826 glib_print_func = func;
827 g_mutex_unlock (g_messages_lock);
829 return old_print_func;
833 g_print (const gchar *format,
838 GPrintFunc local_glib_print_func;
840 g_return_if_fail (format != NULL);
842 va_start (args, format);
843 string = g_strdup_vprintf (format, args);
846 g_mutex_lock (g_messages_lock);
847 local_glib_print_func = glib_print_func;
848 g_mutex_unlock (g_messages_lock);
850 if (local_glib_print_func)
851 local_glib_print_func (string);
854 const gchar *charset;
856 ensure_stdout_valid ();
857 if (g_get_charset (&charset))
858 fputs (string, stdout); /* charset is UTF-8 already */
861 gchar *lstring = strdup_convert (string, charset);
863 fputs (lstring, stdout);
872 g_set_printerr_handler (GPrintFunc func)
874 GPrintFunc old_printerr_func;
876 g_mutex_lock (g_messages_lock);
877 old_printerr_func = glib_printerr_func;
878 glib_printerr_func = func;
879 g_mutex_unlock (g_messages_lock);
881 return old_printerr_func;
885 g_printerr (const gchar *format,
890 GPrintFunc local_glib_printerr_func;
892 g_return_if_fail (format != NULL);
894 va_start (args, format);
895 string = g_strdup_vprintf (format, args);
898 g_mutex_lock (g_messages_lock);
899 local_glib_printerr_func = glib_printerr_func;
900 g_mutex_unlock (g_messages_lock);
902 if (local_glib_printerr_func)
903 local_glib_printerr_func (string);
906 const gchar *charset;
908 ensure_stderr_valid ();
909 if (g_get_charset (&charset))
910 fputs (string, stderr); /* charset is UTF-8 already */
913 gchar *lstring = strdup_convert (string, charset);
915 fputs (lstring, stderr);
924 g_printf_string_upper_bound (const gchar *format,
928 return _g_vsnprintf (&c, 1, format, args) + 1;
932 g_messages_init (void)
934 g_messages_lock = g_mutex_new ();
935 g_log_depth = g_private_new (NULL);
936 g_messages_prefixed_init ();
940 gboolean _g_debug_initialized = FALSE;
941 guint _g_debug_flags = 0;
948 _g_debug_initialized = TRUE;
950 val = g_getenv ("G_DEBUG");
953 static const GDebugKey keys[] = {
954 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
957 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
960 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
962 GLogLevelFlags fatal_mask;
964 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
965 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
966 g_log_set_always_fatal (fatal_mask);