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/.
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 /* --- prototypes --- */
75 #ifndef HAVE_C99_VSNPRINTF
76 static gsize printf_string_upper_bound (const gchar *format,
79 #endif /* !HAVE_C99_VSNPRINTF */
82 /* --- variables --- */
84 static GMutex* g_messages_lock = NULL;
86 static GLogDomain *g_log_domains = NULL;
87 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
88 static GPrintFunc glib_print_func = NULL;
89 static GPrintFunc glib_printerr_func = NULL;
91 static GPrivate* g_log_depth = NULL;
93 /* --- functions --- */
98 # include <process.h> /* For _getpid() */
100 static gboolean alloc_console_called = FALSE;
102 static gboolean gonna_abort = FALSE;
104 /* This default message will usually be overwritten. */
105 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
106 * with huge strings, is it? */
107 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
108 static char *fatal_msg_ptr = fatal_msg_buf;
110 /* Just use stdio. If we're out of memory, we're hosed anyway. */
113 dowrite (GFileDescriptor fd,
119 memcpy (fatal_msg_ptr, buf, len);
120 fatal_msg_ptr += len;
125 fwrite (buf, len, 1, fd);
131 #define write(fd, buf, len) dowrite(fd, buf, len)
134 ensure_stdout_valid (void)
141 if (!alloc_console_called)
143 handle = GetStdHandle (STD_OUTPUT_HANDLE);
145 if (handle == INVALID_HANDLE_VALUE)
148 alloc_console_called = TRUE;
149 freopen ("CONOUT$", "w", stdout);
154 #define ensure_stdout_valid() /* Define as empty */
158 write_unsigned (GFileDescriptor fd,
167 g_return_if_fail (radix >= 2 && radix <= 36);
196 buffer[i] = c + 'a' - 10;
200 write (fd, buffer, n);
204 write_string (GFileDescriptor fd,
207 write (fd, string, strlen (string));
210 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
213 g_log_msg_prefix_init ()
215 static gboolean initialized = FALSE;
221 val = g_getenv ("G_MESSAGES_PREFIXED");
225 static const GDebugKey keys[] = {
226 { "error", G_LOG_LEVEL_ERROR },
227 { "critical", G_LOG_LEVEL_CRITICAL },
228 { "warning", G_LOG_LEVEL_WARNING },
229 { "message", G_LOG_LEVEL_MESSAGE },
230 { "info", G_LOG_LEVEL_INFO },
231 { "debug", G_LOG_LEVEL_DEBUG }
234 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
240 g_log_write_prefix (GFileDescriptor fd,
243 g_log_msg_prefix_init ();
245 if ((g_log_msg_prefix & mask) == mask)
249 prg_name = g_get_prgname ();
252 write_string (fd, "(process:");
255 write_string (fd, prg_name);
256 write_string (fd, " (pid:");
259 write_unsigned (fd, getpid (), 10);
260 write_string (fd, "): ");
264 static inline GLogDomain*
265 g_log_find_domain (const gchar *log_domain)
267 register GLogDomain *domain;
269 g_mutex_lock (g_messages_lock);
270 domain = g_log_domains;
273 if (strcmp (domain->log_domain, log_domain) == 0)
275 g_mutex_unlock (g_messages_lock);
278 domain = domain->next;
280 g_mutex_unlock (g_messages_lock);
284 static inline GLogDomain*
285 g_log_domain_new (const gchar *log_domain)
287 register GLogDomain *domain;
289 domain = g_new (GLogDomain, 1);
290 domain->log_domain = g_strdup (log_domain);
291 domain->fatal_mask = G_LOG_FATAL_MASK;
292 domain->handlers = NULL;
294 g_mutex_lock (g_messages_lock);
295 domain->next = g_log_domains;
296 g_log_domains = domain;
297 g_mutex_unlock (g_messages_lock);
303 g_log_domain_check_free (GLogDomain *domain)
305 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
306 domain->handlers == NULL)
308 register GLogDomain *last, *work;
312 g_mutex_lock (g_messages_lock);
313 work = g_log_domains;
319 last->next = domain->next;
321 g_log_domains = domain->next;
322 g_free (domain->log_domain);
329 g_mutex_unlock (g_messages_lock);
333 static inline GLogFunc
334 g_log_domain_get_handler (GLogDomain *domain,
335 GLogLevelFlags log_level,
338 if (domain && log_level)
340 register GLogHandler *handler;
342 handler = domain->handlers;
345 if ((handler->log_level & log_level) == log_level)
347 *data = handler->data;
348 return handler->log_func;
350 handler = handler->next;
353 return g_log_default_handler;
357 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
359 GLogLevelFlags old_mask;
361 /* restrict the global mask to levels that are known to glib */
362 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
363 /* force errors to be fatal */
364 fatal_mask |= G_LOG_LEVEL_ERROR;
365 /* remove bogus flag */
366 fatal_mask &= ~G_LOG_FLAG_FATAL;
368 g_mutex_lock (g_messages_lock);
369 old_mask = g_log_always_fatal;
370 g_log_always_fatal = fatal_mask;
371 g_mutex_unlock (g_messages_lock);
377 g_log_set_fatal_mask (const gchar *log_domain,
378 GLogLevelFlags fatal_mask)
380 GLogLevelFlags old_flags;
381 register GLogDomain *domain;
386 /* force errors to be fatal */
387 fatal_mask |= G_LOG_LEVEL_ERROR;
388 /* remove bogus flag */
389 fatal_mask &= ~G_LOG_FLAG_FATAL;
391 domain = g_log_find_domain (log_domain);
393 domain = g_log_domain_new (log_domain);
394 old_flags = domain->fatal_mask;
396 domain->fatal_mask = fatal_mask;
397 g_log_domain_check_free (domain);
403 g_log_set_handler (const gchar *log_domain,
404 GLogLevelFlags log_levels,
408 register GLogDomain *domain;
409 register GLogHandler *handler;
410 static guint handler_id = 0;
412 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
413 g_return_val_if_fail (log_func != NULL, 0);
418 domain = g_log_find_domain (log_domain);
420 domain = g_log_domain_new (log_domain);
422 handler = g_new (GLogHandler, 1);
423 g_mutex_lock (g_messages_lock);
424 handler->id = ++handler_id;
425 g_mutex_unlock (g_messages_lock);
426 handler->log_level = log_levels;
427 handler->log_func = log_func;
428 handler->data = user_data;
429 handler->next = domain->handlers;
430 domain->handlers = handler;
436 g_log_remove_handler (const gchar *log_domain,
439 register GLogDomain *domain;
441 g_return_if_fail (handler_id > 0);
446 domain = g_log_find_domain (log_domain);
449 register GLogHandler *work, *last;
452 work = domain->handlers;
455 if (work->id == handler_id)
458 last->next = work->next;
460 domain->handlers = work->next;
462 g_log_domain_check_free (domain);
469 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
475 g_logv (const gchar *log_domain,
476 GLogLevelFlags log_level,
483 #ifndef HAVE_VSNPRINTF
485 #endif /* HAVE_VSNPRINTF */
487 log_level &= G_LOG_LEVEL_MASK;
491 /* we use a stack buffer of fixed size, because we might get called
494 #ifdef HAVE_VSNPRINTF
495 vsnprintf (buffer, 1024, format, args1);
496 #else /* !HAVE_VSNPRINTF */
497 G_VA_COPY (args2, args1);
498 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
499 vsprintf (buffer, format, args2);
502 /* since we might be out of memory, we can't use g_vsnprintf(). */
503 /* we are out of luck here */
504 strncpy (buffer, format, 1024);
508 #endif /* !HAVE_VSNPRINTF */
510 if (!_g_debug_initialized)
513 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
515 register GLogLevelFlags test_level;
518 if (log_level & test_level)
520 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
523 gpointer data = NULL;
525 domain = g_log_find_domain (log_domain ? log_domain : "");
528 test_level |= G_LOG_FLAG_RECURSION;
531 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
533 g_mutex_lock (g_messages_lock);
534 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
535 g_log_always_fatal) & test_level) != 0)
536 test_level |= G_LOG_FLAG_FATAL;
537 g_mutex_unlock (g_messages_lock);
539 log_func = g_log_domain_get_handler (domain, test_level, &data);
540 log_func (log_domain, test_level, buffer, data);
542 /* *domain can be cluttered now */
544 if (test_level & G_LOG_FLAG_FATAL)
547 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
549 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
550 if (!(test_level & G_LOG_FLAG_RECURSION))
554 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
556 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
560 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
566 g_log (const gchar *log_domain,
567 GLogLevelFlags log_level,
573 va_start (args, format);
574 g_logv (log_domain, log_level, format, args);
579 g_log_default_handler (const gchar *log_domain,
580 GLogLevelFlags log_level,
581 const gchar *message,
582 gpointer unused_data)
585 gboolean in_recursion;
588 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
589 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
590 log_level &= G_LOG_LEVEL_MASK;
593 message = "g_log_default_handler(): (NULL) message";
596 /* Use just stdout as stderr is hard to get redirected from the
600 gonna_abort = is_fatal;
602 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
607 case G_LOG_LEVEL_ERROR:
608 /* use write(2) for output, in case we are out of memeory */
609 ensure_stdout_valid ();
611 g_log_write_prefix (fd, log_level);
615 write (fd, log_domain, strlen (log_domain));
619 write (fd, "** ", 3);
621 write (fd, "ERROR (recursed) **: ", 21);
623 write (fd, "ERROR **: ", 10);
624 write (fd, message, strlen (message));
626 write (fd, "\naborting...\n", 13);
630 case G_LOG_LEVEL_CRITICAL:
631 ensure_stdout_valid ();
633 g_log_write_prefix (fd, log_level);
637 write (fd, log_domain, strlen (log_domain));
641 write (fd, "** ", 3);
643 write (fd, "CRITICAL (recursed) **: ", 24);
645 write (fd, "CRITICAL **: ", 13);
646 write (fd, message, strlen (message));
648 write (fd, "\naborting...\n", 13);
652 case G_LOG_LEVEL_WARNING:
653 ensure_stdout_valid ();
655 g_log_write_prefix (fd, log_level);
659 write (fd, log_domain, strlen (log_domain));
663 write (fd, "** ", 3);
665 write (fd, "WARNING (recursed) **: ", 23);
667 write (fd, "WARNING **: ", 12);
668 write (fd, message, strlen (message));
670 write (fd, "\naborting...\n", 13);
674 case G_LOG_LEVEL_MESSAGE:
675 ensure_stdout_valid ();
677 g_log_write_prefix (fd, log_level);
681 write (fd, log_domain, strlen (log_domain));
685 write (fd, "Message (recursed): ", 20);
687 write (fd, "Message: ", 9);
688 write (fd, message, strlen (message));
690 write (fd, "\naborting...\n", 13);
694 case G_LOG_LEVEL_INFO:
695 ensure_stdout_valid ();
697 g_log_write_prefix (fd, log_level);
701 write (fd, log_domain, strlen (log_domain));
705 write (fd, "INFO (recursed): ", 17);
707 write (fd, "INFO: ", 6);
708 write (fd, message, strlen (message));
710 write (fd, "\naborting...\n", 13);
714 case G_LOG_LEVEL_DEBUG:
715 ensure_stdout_valid ();
717 g_log_write_prefix (fd, log_level);
721 write (fd, log_domain, strlen (log_domain));
725 write (fd, "DEBUG (recursed): ", 18);
727 write (fd, "DEBUG: ", 7);
728 write (fd, message, strlen (message));
730 write (fd, "\naborting...\n", 13);
735 /* we are used for a log level that is not defined by GLib itself,
736 * try to make the best out of it.
738 ensure_stdout_valid ();
740 g_log_write_prefix (fd, log_level);
744 write (fd, log_domain, strlen (log_domain));
746 write (fd, "-LOG (recursed:", 15);
748 write (fd, "-LOG (", 6);
750 else if (in_recursion)
751 write (fd, "LOG (recursed:", 14);
753 write (fd, "LOG (", 5);
756 gchar string[] = "0x00): ";
757 gchar *p = string + 2;
760 i = g_bit_nth_msf (log_level, -1);
763 *p = '0' + (i & 0xf);
767 write (fd, string, 7);
770 write (fd, "): ", 3);
771 write (fd, message, strlen (message));
773 write (fd, "\naborting...\n", 13);
781 g_set_print_handler (GPrintFunc func)
783 GPrintFunc old_print_func;
785 g_mutex_lock (g_messages_lock);
786 old_print_func = glib_print_func;
787 glib_print_func = func;
788 g_mutex_unlock (g_messages_lock);
790 return old_print_func;
794 g_print (const gchar *format,
799 GPrintFunc local_glib_print_func;
801 g_return_if_fail (format != NULL);
803 va_start (args, format);
804 string = g_strdup_vprintf (format, args);
807 g_mutex_lock (g_messages_lock);
808 local_glib_print_func = glib_print_func;
809 g_mutex_unlock (g_messages_lock);
811 if (local_glib_print_func)
812 local_glib_print_func (string);
815 ensure_stdout_valid ();
816 fputs (string, stdout);
823 g_set_printerr_handler (GPrintFunc func)
825 GPrintFunc old_printerr_func;
827 g_mutex_lock (g_messages_lock);
828 old_printerr_func = glib_printerr_func;
829 glib_printerr_func = func;
830 g_mutex_unlock (g_messages_lock);
832 return old_printerr_func;
836 g_printerr (const gchar *format,
841 GPrintFunc local_glib_printerr_func;
843 g_return_if_fail (format != NULL);
845 va_start (args, format);
846 string = g_strdup_vprintf (format, args);
849 g_mutex_lock (g_messages_lock);
850 local_glib_printerr_func = glib_printerr_func;
851 g_mutex_unlock (g_messages_lock);
853 if (local_glib_printerr_func)
854 local_glib_printerr_func (string);
857 fputs (string, stderr);
864 # define MB_LEN_MAX 8
867 #ifndef HAVE_C99_VSNPRINTF
873 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
874 gboolean add_space, add_sign, possible_sign, seen_precision;
875 gboolean mod_half, mod_long, mod_extra_long;
879 printf_string_upper_bound (const gchar *format,
883 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
891 register gchar c = *format++;
895 else /* (c == '%') */
897 PrintfArgSpec spec = { 0, };
898 gboolean seen_l = FALSE, conv_done = FALSE;
900 const gchar *spec_start = format;
907 GDoubleIEEE754 u_double;
910 const gchar *v_string;
912 /* beware of positional parameters
916 g_warning (G_GNUC_PRETTY_FUNCTION
917 "(): unable to handle positional parameters (%%n$)");
918 len += 1024; /* try adding some safety padding */
925 spec.alternate_format = TRUE;
928 spec.zero_padding = TRUE;
931 spec.adjust_left = TRUE;
934 spec.add_space = TRUE;
937 spec.add_sign = TRUE;
940 spec.locale_grouping = TRUE;
943 /* parse output size specifications
946 spec.seen_precision = TRUE;
959 while (c >= '0' && c <= '9')
962 v_uint = v_uint * 10 + c - '0';
965 if (spec.seen_precision)
966 spec.precision = MAX (spec.precision, v_uint);
968 spec.min_width = MAX (spec.min_width, v_uint);
971 v_int = va_arg (args, int);
972 if (spec.seen_precision)
974 /* forget about negative precision */
976 spec.precision = MAX (spec.precision, v_int);
983 spec.adjust_left = TRUE;
985 spec.min_width = MAX (spec.min_width, v_int);
989 /* parse type modifiers
992 spec.mod_half = TRUE;
997 spec.mod_long = TRUE;
1001 /* else, fall through */
1004 spec.mod_long = TRUE;
1005 spec.mod_extra_long = TRUE;
1009 #if GLIB_SIZEOF_SIZE_T > 4
1010 spec.mod_long = TRUE;
1011 spec.mod_extra_long = TRUE;
1012 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1015 #if GLIB_SIZEOF_PTRDIFF_T > 4
1016 spec.mod_long = TRUE;
1017 spec.mod_extra_long = TRUE;
1018 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1021 #if GLIB_SIZEOF_INTMAX_T > 4
1022 spec.mod_long = TRUE;
1023 spec.mod_extra_long = TRUE;
1024 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1027 /* parse output conversions
1036 /* some C libraries feature long variants for these as well? */
1037 spec.mod_long = TRUE;
1044 conv_len += 1; /* sign */
1051 spec.possible_sign = TRUE;
1053 if (spec.mod_long && honour_longs)
1055 if (spec.mod_extra_long)
1057 if (spec.mod_extra_long)
1059 (void) va_arg (args, gint64);
1061 else if (spec.mod_long)
1062 (void) va_arg (args, long);
1064 (void) va_arg (args, int);
1076 spec.possible_sign = TRUE;
1077 /* n . dddddddddddddddddddddddd E +- eeee */
1078 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1079 if (may_warn && spec.mod_extra_long)
1080 g_warning (G_GNUC_PRETTY_FUNCTION
1081 "(): unable to handle long double, collecting double only");
1082 #ifdef HAVE_LONG_DOUBLE
1083 #error need to implement special handling for long double
1085 u_double.v_double = va_arg (args, double);
1086 /* %f can expand up to all significant digits before '.' (308) */
1088 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1090 gint exp = u_double.mpn.biased_exponent;
1092 exp -= G_IEEE754_DOUBLE_BIAS;
1093 exp = exp * G_LOG_2_BASE_10 + 1;
1094 conv_len += ABS (exp); /* exp can be <0 */
1096 /* some printf() implementations require extra padding for rounding */
1098 /* we can't really handle locale specific grouping here */
1099 if (spec.locale_grouping)
1103 spec.mod_long = TRUE;
1106 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1107 (void) va_arg (args, int);
1110 spec.mod_long = TRUE;
1113 v_string = va_arg (args, char*);
1115 conv_len += 8; /* hold "(null)" */
1116 else if (spec.seen_precision)
1117 conv_len += spec.precision;
1119 conv_len += strlen (v_string);
1124 g_warning (G_GNUC_PRETTY_FUNCTION
1125 "(): unable to handle wide char strings");
1126 len += 1024; /* try adding some safety padding */
1129 case 'P': /* do we actually need this? */
1132 spec.alternate_format = TRUE;
1139 (void) va_arg (args, void*);
1142 /* there's not much we can do to be clever */
1143 v_string = g_strerror (errno);
1144 v_uint = v_string ? strlen (v_string) : 0;
1145 conv_len += MAX (256, v_uint);
1148 /* handle invalid cases
1151 /* no conversion specification, bad bad */
1152 conv_len += format - spec_start;
1156 g_warning (G_GNUC_PRETTY_FUNCTION
1157 "(): unable to handle `%c' while parsing format",
1161 conv_done |= conv_len > 0;
1164 /* handle width specifications */
1165 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1167 conv_len += spec.alternate_format ? 2 : 0;
1168 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1171 } /* else (c == '%') */
1172 } /* while (*format) */
1177 #endif /* !HAVE_C99_VSNPRINTF */
1181 g_printf_string_upper_bound (const gchar *format,
1184 #if HAVE_C99_VSNPRINTF
1186 return vsnprintf (&c, 1, format, args) + 1;
1188 return printf_string_upper_bound (format, TRUE, args);
1193 g_messages_init (void)
1195 g_messages_lock = g_mutex_new ();
1196 g_log_depth = g_private_new (NULL);
1197 g_log_msg_prefix_init ();
1201 gboolean _g_debug_initialized = FALSE;
1202 guint _g_debug_flags = 0;
1204 void _g_debug_init ()
1208 _g_debug_initialized = TRUE;
1210 val = g_getenv ("G_DEBUG");
1213 static const GDebugKey keys[] = {
1214 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1217 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1220 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1222 GLogLevelFlags fatal_mask;
1224 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1225 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1226 g_log_set_always_fatal (fatal_mask);