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/.
48 typedef FILE* GFileDescriptor;
50 typedef gint GFileDescriptor;
53 /* --- structures --- */
54 typedef struct _GLogDomain GLogDomain;
55 typedef struct _GLogHandler GLogHandler;
59 GLogLevelFlags fatal_mask;
60 GLogHandler *handlers;
66 GLogLevelFlags log_level;
73 /* --- prototypes --- */
74 #ifndef HAVE_C99_VSNPRINTF
75 static gsize printf_string_upper_bound (const gchar *format,
78 #endif /* !HAVE_C99_VSNPRINTF */
81 /* --- variables --- */
83 static GMutex* g_messages_lock = NULL;
85 static GLogDomain *g_log_domains = NULL;
86 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
87 static GPrintFunc glib_print_func = NULL;
88 static GPrintFunc glib_printerr_func = NULL;
90 static GPrivate* g_log_depth = NULL;
92 /* --- functions --- */
97 # include <process.h> /* For _getpid() */
99 static gboolean alloc_console_called = FALSE;
101 static gboolean gonna_abort = FALSE;
103 /* This default message will usually be overwritten. */
104 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
105 * with huge strings, is it? */
106 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
107 static char *fatal_msg_ptr = fatal_msg_buf;
109 /* Just use stdio. If we're out of memory, we're hosed anyway. */
112 dowrite (GFileDescriptor fd,
118 memcpy (fatal_msg_ptr, buf, len);
119 fatal_msg_ptr += len;
124 fwrite (buf, len, 1, fd);
130 #define write(fd, buf, len) dowrite(fd, buf, len)
133 ensure_stdout_valid (void)
140 if (!alloc_console_called)
142 handle = GetStdHandle (STD_OUTPUT_HANDLE);
144 if (handle == INVALID_HANDLE_VALUE)
147 alloc_console_called = TRUE;
148 freopen ("CONOUT$", "w", stdout);
153 #define ensure_stdout_valid() /* Define as empty */
157 write_unsigned (GFileDescriptor fd,
166 g_return_if_fail (radix >= 2 && radix <= 36);
195 buffer[i] = c + 'a' - 10;
199 write (fd, buffer, n);
203 write_string (GFileDescriptor fd,
206 write (fd, string, strlen (string));
210 g_log_write_prefix (GFileDescriptor fd,
213 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
214 static gboolean initialized = FALSE;
216 g_mutex_lock (g_messages_lock);
223 val = g_getenv ("G_MESSAGES_PREFIXED");
227 static const GDebugKey keys[] = {
228 { "error", G_LOG_LEVEL_ERROR },
229 { "critical", G_LOG_LEVEL_CRITICAL },
230 { "warning", G_LOG_LEVEL_WARNING },
231 { "message", G_LOG_LEVEL_MESSAGE },
232 { "info", G_LOG_LEVEL_INFO },
233 { "debug", G_LOG_LEVEL_DEBUG }
236 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
240 g_mutex_unlock (g_messages_lock);
242 if ((g_log_msg_prefix & mask) == mask)
246 prg_name = g_get_prgname ();
249 write_string (fd, "(process:");
252 write_string (fd, prg_name);
253 write_string (fd, " (pid:");
256 write_unsigned (fd, getpid (), 10);
257 write_string (fd, "): ");
261 static inline GLogDomain*
262 g_log_find_domain (const gchar *log_domain)
264 register GLogDomain *domain;
266 g_mutex_lock (g_messages_lock);
267 domain = g_log_domains;
270 if (strcmp (domain->log_domain, log_domain) == 0)
272 g_mutex_unlock (g_messages_lock);
275 domain = domain->next;
277 g_mutex_unlock (g_messages_lock);
281 static inline GLogDomain*
282 g_log_domain_new (const gchar *log_domain)
284 register GLogDomain *domain;
286 domain = g_new (GLogDomain, 1);
287 domain->log_domain = g_strdup (log_domain);
288 domain->fatal_mask = G_LOG_FATAL_MASK;
289 domain->handlers = NULL;
291 g_mutex_lock (g_messages_lock);
292 domain->next = g_log_domains;
293 g_log_domains = domain;
294 g_mutex_unlock (g_messages_lock);
300 g_log_domain_check_free (GLogDomain *domain)
302 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
303 domain->handlers == NULL)
305 register GLogDomain *last, *work;
309 g_mutex_lock (g_messages_lock);
310 work = g_log_domains;
316 last->next = domain->next;
318 g_log_domains = domain->next;
319 g_free (domain->log_domain);
326 g_mutex_unlock (g_messages_lock);
330 static inline GLogFunc
331 g_log_domain_get_handler (GLogDomain *domain,
332 GLogLevelFlags log_level,
335 if (domain && log_level)
337 register GLogHandler *handler;
339 handler = domain->handlers;
342 if ((handler->log_level & log_level) == log_level)
344 *data = handler->data;
345 return handler->log_func;
347 handler = handler->next;
350 return g_log_default_handler;
354 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
356 GLogLevelFlags old_mask;
358 /* restrict the global mask to levels that are known to glib */
359 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
360 /* force errors to be fatal */
361 fatal_mask |= G_LOG_LEVEL_ERROR;
362 /* remove bogus flag */
363 fatal_mask &= ~G_LOG_FLAG_FATAL;
365 g_mutex_lock (g_messages_lock);
366 old_mask = g_log_always_fatal;
367 g_log_always_fatal = fatal_mask;
368 g_mutex_unlock (g_messages_lock);
374 g_log_set_fatal_mask (const gchar *log_domain,
375 GLogLevelFlags fatal_mask)
377 GLogLevelFlags old_flags;
378 register GLogDomain *domain;
383 /* force errors to be fatal */
384 fatal_mask |= G_LOG_LEVEL_ERROR;
385 /* remove bogus flag */
386 fatal_mask &= ~G_LOG_FLAG_FATAL;
388 domain = g_log_find_domain (log_domain);
390 domain = g_log_domain_new (log_domain);
391 old_flags = domain->fatal_mask;
393 domain->fatal_mask = fatal_mask;
394 g_log_domain_check_free (domain);
400 g_log_set_handler (const gchar *log_domain,
401 GLogLevelFlags log_levels,
405 register GLogDomain *domain;
406 register GLogHandler *handler;
407 static guint handler_id = 0;
409 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
410 g_return_val_if_fail (log_func != NULL, 0);
415 domain = g_log_find_domain (log_domain);
417 domain = g_log_domain_new (log_domain);
419 handler = g_new (GLogHandler, 1);
420 g_mutex_lock (g_messages_lock);
421 handler->id = ++handler_id;
422 g_mutex_unlock (g_messages_lock);
423 handler->log_level = log_levels;
424 handler->log_func = log_func;
425 handler->data = user_data;
426 handler->next = domain->handlers;
427 domain->handlers = handler;
433 g_log_remove_handler (const gchar *log_domain,
436 register GLogDomain *domain;
438 g_return_if_fail (handler_id > 0);
443 domain = g_log_find_domain (log_domain);
446 register GLogHandler *work, *last;
449 work = domain->handlers;
452 if (work->id == handler_id)
455 last->next = work->next;
457 domain->handlers = work->next;
459 g_log_domain_check_free (domain);
466 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
472 g_logv (const gchar *log_domain,
473 GLogLevelFlags log_level,
480 #ifndef HAVE_VSNPRINTF
482 #endif /* HAVE_VSNPRINTF */
484 log_level &= G_LOG_LEVEL_MASK;
488 /* we use a stack buffer of fixed size, because we might get called
491 #ifdef HAVE_VSNPRINTF
492 vsnprintf (buffer, 1024, format, args1);
493 #else /* !HAVE_VSNPRINTF */
494 G_VA_COPY (args2, args1);
495 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
496 vsprintf (buffer, format, args2);
499 /* since we might be out of memory, we can't use g_vsnprintf(). */
500 /* we are out of luck here */
501 strncpy (buffer, format, 1024);
505 #endif /* !HAVE_VSNPRINTF */
507 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
509 register GLogLevelFlags test_level;
512 if (log_level & test_level)
514 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
517 gpointer data = NULL;
519 domain = g_log_find_domain (log_domain ? log_domain : "");
522 test_level |= G_LOG_FLAG_RECURSION;
525 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
527 g_mutex_lock (g_messages_lock);
528 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
529 g_log_always_fatal) & test_level) != 0)
530 test_level |= G_LOG_FLAG_FATAL;
531 g_mutex_unlock (g_messages_lock);
533 log_func = g_log_domain_get_handler (domain, test_level, &data);
534 log_func (log_domain, test_level, buffer, data);
536 /* *domain can be cluttered now */
538 if (test_level & G_LOG_FLAG_FATAL)
540 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
541 if (!(test_level & G_LOG_FLAG_RECURSION))
545 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
547 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
549 # if defined (_MSC_VER) && defined (_DEBUG)
550 /* let's see the call stack ... */
554 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
558 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
564 g_log (const gchar *log_domain,
565 GLogLevelFlags log_level,
571 va_start (args, format);
572 g_logv (log_domain, log_level, format, args);
577 g_log_default_handler (const gchar *log_domain,
578 GLogLevelFlags log_level,
579 const gchar *message,
580 gpointer unused_data)
583 gboolean in_recursion;
586 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
587 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
588 log_level &= G_LOG_LEVEL_MASK;
591 message = "g_log_default_handler(): (NULL) message";
594 /* Use just stdout as stderr is hard to get redirected from the
598 gonna_abort = is_fatal;
600 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
605 case G_LOG_LEVEL_ERROR:
606 /* use write(2) for output, in case we are out of memeory */
607 ensure_stdout_valid ();
609 g_log_write_prefix (fd, log_level);
613 write (fd, log_domain, strlen (log_domain));
617 write (fd, "** ", 3);
619 write (fd, "ERROR (recursed) **: ", 21);
621 write (fd, "ERROR **: ", 10);
622 write (fd, message, strlen (message));
624 write (fd, "\naborting...\n", 13);
628 case G_LOG_LEVEL_CRITICAL:
629 ensure_stdout_valid ();
631 g_log_write_prefix (fd, log_level);
635 write (fd, log_domain, strlen (log_domain));
639 write (fd, "** ", 3);
641 write (fd, "CRITICAL (recursed) **: ", 24);
643 write (fd, "CRITICAL **: ", 13);
644 write (fd, message, strlen (message));
646 write (fd, "\naborting...\n", 13);
650 case G_LOG_LEVEL_WARNING:
651 ensure_stdout_valid ();
653 g_log_write_prefix (fd, log_level);
657 write (fd, log_domain, strlen (log_domain));
661 write (fd, "** ", 3);
663 write (fd, "WARNING (recursed) **: ", 23);
665 write (fd, "WARNING **: ", 12);
666 write (fd, message, strlen (message));
668 write (fd, "\naborting...\n", 13);
672 case G_LOG_LEVEL_MESSAGE:
673 ensure_stdout_valid ();
675 g_log_write_prefix (fd, log_level);
679 write (fd, log_domain, strlen (log_domain));
683 write (fd, "Message (recursed): ", 20);
685 write (fd, "Message: ", 9);
686 write (fd, message, strlen (message));
688 write (fd, "\naborting...\n", 13);
692 case G_LOG_LEVEL_INFO:
693 ensure_stdout_valid ();
695 g_log_write_prefix (fd, log_level);
699 write (fd, log_domain, strlen (log_domain));
703 write (fd, "INFO (recursed): ", 17);
705 write (fd, "INFO: ", 6);
706 write (fd, message, strlen (message));
708 write (fd, "\naborting...\n", 13);
712 case G_LOG_LEVEL_DEBUG:
713 ensure_stdout_valid ();
715 g_log_write_prefix (fd, log_level);
719 write (fd, log_domain, strlen (log_domain));
723 write (fd, "DEBUG (recursed): ", 18);
725 write (fd, "DEBUG: ", 7);
726 write (fd, message, strlen (message));
728 write (fd, "\naborting...\n", 13);
733 /* we are used for a log level that is not defined by GLib itself,
734 * try to make the best out of it.
736 ensure_stdout_valid ();
738 g_log_write_prefix (fd, log_level);
742 write (fd, log_domain, strlen (log_domain));
744 write (fd, "-LOG (recursed:", 15);
746 write (fd, "-LOG (", 6);
748 else if (in_recursion)
749 write (fd, "LOG (recursed:", 14);
751 write (fd, "LOG (", 5);
754 gchar string[] = "0x00): ";
755 gchar *p = string + 2;
758 i = g_bit_nth_msf (log_level, -1);
761 *p = '0' + (i & 0xf);
765 write (fd, string, 7);
768 write (fd, "): ", 3);
769 write (fd, message, strlen (message));
771 write (fd, "\naborting...\n", 13);
779 g_set_print_handler (GPrintFunc func)
781 GPrintFunc old_print_func;
783 g_mutex_lock (g_messages_lock);
784 old_print_func = glib_print_func;
785 glib_print_func = func;
786 g_mutex_unlock (g_messages_lock);
788 return old_print_func;
792 g_print (const gchar *format,
797 GPrintFunc local_glib_print_func;
799 g_return_if_fail (format != NULL);
801 va_start (args, format);
802 string = g_strdup_vprintf (format, args);
805 g_mutex_lock (g_messages_lock);
806 local_glib_print_func = glib_print_func;
807 g_mutex_unlock (g_messages_lock);
809 if (local_glib_print_func)
810 local_glib_print_func (string);
813 ensure_stdout_valid ();
814 fputs (string, stdout);
821 g_set_printerr_handler (GPrintFunc func)
823 GPrintFunc old_printerr_func;
825 g_mutex_lock (g_messages_lock);
826 old_printerr_func = glib_printerr_func;
827 glib_printerr_func = func;
828 g_mutex_unlock (g_messages_lock);
830 return old_printerr_func;
834 g_printerr (const gchar *format,
839 GPrintFunc local_glib_printerr_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_printerr_func = glib_printerr_func;
849 g_mutex_unlock (g_messages_lock);
851 if (local_glib_printerr_func)
852 local_glib_printerr_func (string);
855 fputs (string, stderr);
862 # define MB_LEN_MAX 8
865 #ifndef HAVE_C99_VSNPRINTF
871 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
872 gboolean add_space, add_sign, possible_sign, seen_precision;
873 gboolean mod_half, mod_long, mod_extra_long;
877 printf_string_upper_bound (const gchar *format,
881 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
889 register gchar c = *format++;
893 else /* (c == '%') */
895 PrintfArgSpec spec = { 0, };
896 gboolean seen_l = FALSE, conv_done = FALSE;
898 const gchar *spec_start = format;
905 GDoubleIEEE754 u_double;
908 const gchar *v_string;
910 /* beware of positional parameters
914 g_warning (G_GNUC_PRETTY_FUNCTION
915 "(): unable to handle positional parameters (%%n$)");
916 len += 1024; /* try adding some safety padding */
923 spec.alternate_format = TRUE;
926 spec.zero_padding = TRUE;
929 spec.adjust_left = TRUE;
932 spec.add_space = TRUE;
935 spec.add_sign = TRUE;
938 spec.locale_grouping = TRUE;
941 /* parse output size specifications
944 spec.seen_precision = TRUE;
957 while (c >= '0' && c <= '9')
960 v_uint = v_uint * 10 + c - '0';
963 if (spec.seen_precision)
964 spec.precision = MAX (spec.precision, v_uint);
966 spec.min_width = MAX (spec.min_width, v_uint);
969 v_int = va_arg (args, int);
970 if (spec.seen_precision)
972 /* forget about negative precision */
974 spec.precision = MAX (spec.precision, v_int);
981 spec.adjust_left = TRUE;
983 spec.min_width = MAX (spec.min_width, v_int);
987 /* parse type modifiers
990 spec.mod_half = TRUE;
995 spec.mod_long = TRUE;
999 /* else, fall through */
1002 spec.mod_long = TRUE;
1003 spec.mod_extra_long = TRUE;
1007 #if GLIB_SIZEOF_SIZE_T > 4
1008 spec.mod_long = TRUE;
1009 spec.mod_extra_long = TRUE;
1010 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1013 #if GLIB_SIZEOF_PTRDIFF_T > 4
1014 spec.mod_long = TRUE;
1015 spec.mod_extra_long = TRUE;
1016 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1019 #if GLIB_SIZEOF_INTMAX_T > 4
1020 spec.mod_long = TRUE;
1021 spec.mod_extra_long = TRUE;
1022 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1025 /* parse output conversions
1034 /* some C libraries feature long variants for these as well? */
1035 spec.mod_long = TRUE;
1042 conv_len += 1; /* sign */
1049 spec.possible_sign = TRUE;
1051 if (spec.mod_long && honour_longs)
1053 if (spec.mod_extra_long)
1055 if (spec.mod_extra_long)
1057 (void) va_arg (args, gint64);
1059 else if (spec.mod_long)
1060 (void) va_arg (args, long);
1062 (void) va_arg (args, int);
1074 spec.possible_sign = TRUE;
1075 /* n . dddddddddddddddddddddddd E +- eeee */
1076 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1077 if (may_warn && spec.mod_extra_long)
1078 g_warning (G_GNUC_PRETTY_FUNCTION
1079 "(): unable to handle long double, collecting double only");
1080 #ifdef HAVE_LONG_DOUBLE
1081 #error need to implement special handling for long double
1083 u_double.v_double = va_arg (args, double);
1084 /* %f can expand up to all significant digits before '.' (308) */
1086 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1088 gint exp = u_double.mpn.biased_exponent;
1090 exp -= G_IEEE754_DOUBLE_BIAS;
1091 exp = exp * G_LOG_2_BASE_10 + 1;
1092 conv_len += ABS (exp); /* exp can be <0 */
1094 /* some printf() implementations require extra padding for rounding */
1096 /* we can't really handle locale specific grouping here */
1097 if (spec.locale_grouping)
1101 spec.mod_long = TRUE;
1104 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1105 (void) va_arg (args, int);
1108 spec.mod_long = TRUE;
1111 v_string = va_arg (args, char*);
1113 conv_len += 8; /* hold "(null)" */
1114 else if (spec.seen_precision)
1115 conv_len += spec.precision;
1117 conv_len += strlen (v_string);
1122 g_warning (G_GNUC_PRETTY_FUNCTION
1123 "(): unable to handle wide char strings");
1124 len += 1024; /* try adding some safety padding */
1127 case 'P': /* do we actually need this? */
1130 spec.alternate_format = TRUE;
1137 (void) va_arg (args, void*);
1140 /* there's not much we can do to be clever */
1141 v_string = g_strerror (errno);
1142 v_uint = v_string ? strlen (v_string) : 0;
1143 conv_len += MAX (256, v_uint);
1146 /* handle invalid cases
1149 /* no conversion specification, bad bad */
1150 conv_len += format - spec_start;
1154 g_warning (G_GNUC_PRETTY_FUNCTION
1155 "(): unable to handle `%c' while parsing format",
1159 conv_done |= conv_len > 0;
1162 /* handle width specifications */
1163 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1165 conv_len += spec.alternate_format ? 2 : 0;
1166 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1169 } /* else (c == '%') */
1170 } /* while (*format) */
1175 #endif /* !HAVE_C99_VSNPRINTF */
1179 g_printf_string_upper_bound (const gchar *format,
1182 #if HAVE_C99_VSNPRINTF
1184 return vsnprintf (&c, 1, format, args);
1186 return printf_string_upper_bound (format, TRUE, args);
1191 g_messages_init (void)
1193 g_messages_lock = g_mutex_new();
1194 g_log_depth = g_private_new(NULL);