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 alloc_console_called = FALSE;
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)
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);
143 #define ensure_stdout_valid() /* Define as empty */
147 write_string (GFileDescriptor fd,
150 write (fd, string, strlen (string));
154 g_messages_prefixed_init (void)
156 static gboolean initialized = FALSE;
163 val = g_getenv ("G_MESSAGES_PREFIXED");
167 static const GDebugKey keys[] = {
168 { "error", G_LOG_LEVEL_ERROR },
169 { "critical", G_LOG_LEVEL_CRITICAL },
170 { "warning", G_LOG_LEVEL_WARNING },
171 { "message", G_LOG_LEVEL_MESSAGE },
172 { "info", G_LOG_LEVEL_INFO },
173 { "debug", G_LOG_LEVEL_DEBUG }
176 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
182 g_log_find_domain_L (const gchar *log_domain)
184 register GLogDomain *domain;
186 domain = g_log_domains;
189 if (strcmp (domain->log_domain, log_domain) == 0)
191 domain = domain->next;
197 g_log_domain_new_L (const gchar *log_domain)
199 register GLogDomain *domain;
201 domain = g_new (GLogDomain, 1);
202 domain->log_domain = g_strdup (log_domain);
203 domain->fatal_mask = G_LOG_FATAL_MASK;
204 domain->handlers = NULL;
206 domain->next = g_log_domains;
207 g_log_domains = domain;
213 g_log_domain_check_free_L (GLogDomain *domain)
215 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
216 domain->handlers == NULL)
218 register GLogDomain *last, *work;
222 work = g_log_domains;
228 last->next = domain->next;
230 g_log_domains = domain->next;
231 g_free (domain->log_domain);
242 g_log_domain_get_handler_L (GLogDomain *domain,
243 GLogLevelFlags log_level,
246 if (domain && log_level)
248 register GLogHandler *handler;
250 handler = domain->handlers;
253 if ((handler->log_level & log_level) == log_level)
255 *data = handler->data;
256 return handler->log_func;
258 handler = handler->next;
261 return g_log_default_handler;
265 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
267 GLogLevelFlags old_mask;
269 /* restrict the global mask to levels that are known to glib
270 * since this setting applies to all domains
272 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
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);
279 old_mask = g_log_always_fatal;
280 g_log_always_fatal = fatal_mask;
281 g_mutex_unlock (g_messages_lock);
287 g_log_set_fatal_mask (const gchar *log_domain,
288 GLogLevelFlags fatal_mask)
290 GLogLevelFlags old_flags;
291 register GLogDomain *domain;
296 /* force errors to be fatal */
297 fatal_mask |= G_LOG_LEVEL_ERROR;
298 /* remove bogus flag */
299 fatal_mask &= ~G_LOG_FLAG_FATAL;
301 g_mutex_lock (g_messages_lock);
303 domain = g_log_find_domain_L (log_domain);
305 domain = g_log_domain_new_L (log_domain);
306 old_flags = domain->fatal_mask;
308 domain->fatal_mask = fatal_mask;
309 g_log_domain_check_free_L (domain);
311 g_mutex_unlock (g_messages_lock);
317 g_log_set_handler (const gchar *log_domain,
318 GLogLevelFlags log_levels,
322 static guint handler_id = 0;
324 GLogHandler *handler;
326 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
327 g_return_val_if_fail (log_func != NULL, 0);
332 handler = g_new (GLogHandler, 1);
334 g_mutex_lock (g_messages_lock);
336 domain = g_log_find_domain_L (log_domain);
338 domain = g_log_domain_new_L (log_domain);
340 handler->id = ++handler_id;
341 handler->log_level = log_levels;
342 handler->log_func = log_func;
343 handler->data = user_data;
344 handler->next = domain->handlers;
345 domain->handlers = handler;
347 g_mutex_unlock (g_messages_lock);
353 g_log_remove_handler (const gchar *log_domain,
356 register GLogDomain *domain;
358 g_return_if_fail (handler_id > 0);
363 g_mutex_lock (g_messages_lock);
364 domain = g_log_find_domain_L (log_domain);
367 GLogHandler *work, *last;
370 work = domain->handlers;
373 if (work->id == handler_id)
376 last->next = work->next;
378 domain->handlers = work->next;
379 g_log_domain_check_free_L (domain);
380 g_mutex_unlock (g_messages_lock);
388 g_mutex_unlock (g_messages_lock);
389 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
390 G_STRLOC, handler_id, log_domain);
394 g_logv (const gchar *log_domain,
395 GLogLevelFlags log_level,
400 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
401 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
404 log_level &= G_LOG_LEVEL_MASK;
408 /* we use a stack buffer of fixed size, because we might get called
411 _g_vsnprintf (buffer, 1024, format, args1);
413 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
415 register GLogLevelFlags test_level;
418 if (log_level & test_level)
420 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
423 guint domain_fatal_mask;
424 gpointer data = NULL;
427 test_level |= G_LOG_FLAG_FATAL;
429 test_level |= G_LOG_FLAG_RECURSION;
431 /* check recursion and lookup handler */
432 g_mutex_lock (g_messages_lock);
433 domain = g_log_find_domain_L (log_domain ? log_domain : "");
435 test_level |= G_LOG_FLAG_RECURSION;
437 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
438 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
439 test_level |= G_LOG_FLAG_FATAL;
440 if (test_level & G_LOG_FLAG_RECURSION)
441 log_func = _g_log_fallback_handler;
443 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
445 g_mutex_unlock (g_messages_lock);
447 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
449 /* had to defer debug initialization until we can keep track of recursion */
450 if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
452 guint orig_test_level = test_level;
455 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
456 test_level |= G_LOG_FLAG_FATAL;
457 if (test_level != orig_test_level)
459 /* need a relookup, not nice, but not too bad either */
460 g_mutex_lock (g_messages_lock);
461 domain = g_log_find_domain_L (log_domain ? log_domain : "");
462 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
464 g_mutex_unlock (g_messages_lock);
468 log_func (log_domain, test_level, buffer, data);
470 if (test_level & G_LOG_FLAG_FATAL)
473 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
475 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
476 if (!(test_level & G_LOG_FLAG_RECURSION))
480 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
482 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
486 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
492 g_log (const gchar *log_domain,
493 GLogLevelFlags log_level,
499 va_start (args, format);
500 g_logv (log_domain, log_level, format, args);
505 strdup_convert (const gchar *string,
506 const gchar *charset)
508 if (!g_utf8_validate (string, -1, NULL))
509 return g_strconcat ("[Invalid UTF-8] ", string, NULL);
514 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
519 /* Not thread-safe, but doesn't matter if we print the warning twice
521 static gboolean warned = FALSE;
525 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
529 return g_strdup (string);
534 /* For a radix of 8 we need at most 3 output bytes for 1 input
535 * byte. Additionally we might need up to 2 output bytes for the
536 * readix prefix and 1 byte for the trailing NULL.
538 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
541 format_unsigned (gchar *buf,
549 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
551 if (radix != 8 && radix != 10 && radix != 16)
584 /* Again we can't use g_assert; actually this check should _never_ fail. */
585 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
598 buf[i] = c + 'a' - 10;
605 /* string size big enough to hold level prefix */
606 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
608 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
610 static GFileDescriptor
611 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
614 gboolean to_stdout = TRUE;
616 /* we may not call _any_ GLib functions here */
618 switch (log_level & G_LOG_LEVEL_MASK)
620 case G_LOG_LEVEL_ERROR:
621 strcpy (level_prefix, "ERROR");
624 case G_LOG_LEVEL_CRITICAL:
625 strcpy (level_prefix, "CRITICAL");
628 case G_LOG_LEVEL_WARNING:
629 strcpy (level_prefix, "WARNING");
632 case G_LOG_LEVEL_MESSAGE:
633 strcpy (level_prefix, "Message");
636 case G_LOG_LEVEL_INFO:
637 strcpy (level_prefix, "INFO");
639 case G_LOG_LEVEL_DEBUG:
640 strcpy (level_prefix, "DEBUG");
645 strcpy (level_prefix, "LOG-");
646 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
649 strcpy (level_prefix, "LOG");
652 if (log_level & G_LOG_FLAG_RECURSION)
653 strcat (level_prefix, " (recursed)");
654 if (log_level & ALERT_LEVELS)
655 strcat (level_prefix, " **");
657 ensure_stdout_valid ();
659 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
660 /* Use just stdout as stderr is hard to get redirected from the DOS prompt. */
663 return to_stdout ? 1 : 2;
668 _g_log_fallback_handler (const gchar *log_domain,
669 GLogLevelFlags log_level,
670 const gchar *message,
671 gpointer unused_data)
673 gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE];
674 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
677 /* we can not call _any_ GLib functions in this fallback handler,
678 * which is why we skip UTF-8 conversion, etc.
679 * since we either recursed or ran out of memory, we're in a pretty
680 * pathologic situation anyways, what we can do is giving the
681 * the process ID unconditionally however.
684 fd = mklevel_prefix (level_prefix, log_level);
686 message = "(NULL) message";
688 format_unsigned (pid_string, getpid (), 10);
691 write_string (fd, "\n");
693 write_string (fd, "\n** ");
694 write_string (fd, "(process:");
695 write_string (fd, pid_string);
696 write_string (fd, "): ");
699 write_string (fd, log_domain);
700 write_string (fd, "-");
702 write_string (fd, level_prefix);
703 write_string (fd, ": ");
704 write_string (fd, message);
706 write_string (fd, "\naborting...\n");
708 write_string (fd, "\n");
712 g_log_default_handler (const gchar *log_domain,
713 GLogLevelFlags log_level,
714 const gchar *message,
715 gpointer unused_data)
717 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
718 gchar level_prefix[STRING_BUFFER_SIZE], *string;
722 /* we can be called externally with recursion for whatever reason */
723 if (log_level & G_LOG_FLAG_RECURSION)
725 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
729 g_messages_prefixed_init ();
731 fd = mklevel_prefix (level_prefix, log_level);
733 gstring = g_string_new ("");
734 if (log_level & ALERT_LEVELS)
735 g_string_append (gstring, "\n");
737 g_string_append (gstring, "** ");
739 if ((g_log_msg_prefix & log_level) == log_level)
741 const gchar *prg_name = g_get_prgname ();
744 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
746 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
751 g_string_append (gstring, log_domain);
752 g_string_append_c (gstring, '-');
754 g_string_append (gstring, level_prefix);
756 g_string_append (gstring, ": ");
758 g_string_append (gstring, "(NULL) message");
761 const gchar *charset;
763 if (g_get_charset (&charset))
764 g_string_append (gstring, message); /* charset is UTF-8 already */
767 string = strdup_convert (message, charset);
768 g_string_append (gstring, string);
773 g_string_append (gstring, "\naborting...\n");
775 g_string_append (gstring, "\n");
777 string = g_string_free (gstring, FALSE);
779 write_string (fd, string);
784 g_set_print_handler (GPrintFunc func)
786 GPrintFunc old_print_func;
788 g_mutex_lock (g_messages_lock);
789 old_print_func = glib_print_func;
790 glib_print_func = func;
791 g_mutex_unlock (g_messages_lock);
793 return old_print_func;
797 g_print (const gchar *format,
802 GPrintFunc local_glib_print_func;
804 g_return_if_fail (format != NULL);
806 va_start (args, format);
807 string = g_strdup_vprintf (format, args);
810 g_mutex_lock (g_messages_lock);
811 local_glib_print_func = glib_print_func;
812 g_mutex_unlock (g_messages_lock);
814 if (local_glib_print_func)
815 local_glib_print_func (string);
818 const gchar *charset;
820 ensure_stdout_valid ();
821 if (g_get_charset (&charset))
822 fputs (string, stdout); /* charset is UTF-8 already */
825 gchar *lstring = strdup_convert (string, charset);
827 fputs (lstring, stdout);
836 g_set_printerr_handler (GPrintFunc func)
838 GPrintFunc old_printerr_func;
840 g_mutex_lock (g_messages_lock);
841 old_printerr_func = glib_printerr_func;
842 glib_printerr_func = func;
843 g_mutex_unlock (g_messages_lock);
845 return old_printerr_func;
849 g_printerr (const gchar *format,
854 GPrintFunc local_glib_printerr_func;
856 g_return_if_fail (format != NULL);
858 va_start (args, format);
859 string = g_strdup_vprintf (format, args);
862 g_mutex_lock (g_messages_lock);
863 local_glib_printerr_func = glib_printerr_func;
864 g_mutex_unlock (g_messages_lock);
866 if (local_glib_printerr_func)
867 local_glib_printerr_func (string);
870 const gchar *charset;
872 if (g_get_charset (&charset))
873 fputs (string, stderr); /* charset is UTF-8 already */
876 gchar *lstring = strdup_convert (string, charset);
878 fputs (lstring, stderr);
887 g_printf_string_upper_bound (const gchar *format,
891 return _g_vsnprintf (&c, 1, format, args) + 1;
895 g_messages_init (void)
897 g_messages_lock = g_mutex_new ();
898 g_log_depth = g_private_new (NULL);
899 g_messages_prefixed_init ();
903 gboolean _g_debug_initialized = FALSE;
904 guint _g_debug_flags = 0;
911 _g_debug_initialized = TRUE;
913 val = g_getenv ("G_DEBUG");
916 static const GDebugKey keys[] = {
917 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
920 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
923 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
925 GLogLevelFlags fatal_mask;
927 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
928 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
929 g_log_set_always_fatal (fatal_mask);