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 /* HOLDS g_messages_lock */
265 static inline GLogDomain*
266 g_log_find_domain (const gchar *log_domain)
268 register GLogDomain *domain;
270 domain = g_log_domains;
273 if (strcmp (domain->log_domain, log_domain) == 0)
275 domain = domain->next;
280 /* HOLDS 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 domain->next = g_log_domains;
292 g_log_domains = domain;
297 /* HOLDS g_messages_lock */
299 g_log_domain_check_free (GLogDomain *domain)
301 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
302 domain->handlers == NULL)
304 register GLogDomain *last, *work;
308 work = g_log_domains;
314 last->next = domain->next;
316 g_log_domains = domain->next;
317 g_free (domain->log_domain);
327 /* HOLDS g_messages_lock */
328 static inline GLogFunc
329 g_log_domain_get_handler (GLogDomain *domain,
330 GLogLevelFlags log_level,
333 if (domain && log_level)
335 register GLogHandler *handler;
337 handler = domain->handlers;
340 if ((handler->log_level & log_level) == log_level)
342 *data = handler->data;
343 return handler->log_func;
345 handler = handler->next;
348 return g_log_default_handler;
352 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
354 GLogLevelFlags old_mask;
356 /* restrict the global mask to levels that are known to glib */
357 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
358 /* force errors to be fatal */
359 fatal_mask |= G_LOG_LEVEL_ERROR;
360 /* remove bogus flag */
361 fatal_mask &= ~G_LOG_FLAG_FATAL;
363 g_mutex_lock (g_messages_lock);
364 old_mask = g_log_always_fatal;
365 g_log_always_fatal = fatal_mask;
366 g_mutex_unlock (g_messages_lock);
372 g_log_set_fatal_mask (const gchar *log_domain,
373 GLogLevelFlags fatal_mask)
375 GLogLevelFlags old_flags;
376 register GLogDomain *domain;
381 /* force errors to be fatal */
382 fatal_mask |= G_LOG_LEVEL_ERROR;
383 /* remove bogus flag */
384 fatal_mask &= ~G_LOG_FLAG_FATAL;
386 g_mutex_lock (g_messages_lock);
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);
396 g_mutex_unlock (g_messages_lock);
402 g_log_set_handler (const gchar *log_domain,
403 GLogLevelFlags log_levels,
407 register GLogDomain *domain;
408 register GLogHandler *handler;
409 static guint handler_id = 0;
411 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
412 g_return_val_if_fail (log_func != NULL, 0);
417 g_mutex_lock (g_messages_lock);
419 domain = g_log_find_domain (log_domain);
421 domain = g_log_domain_new (log_domain);
423 handler = g_new (GLogHandler, 1);
424 handler->id = ++handler_id;
425 handler->log_level = log_levels;
426 handler->log_func = log_func;
427 handler->data = user_data;
428 handler->next = domain->handlers;
429 domain->handlers = handler;
431 g_mutex_unlock (g_messages_lock);
437 g_log_remove_handler (const gchar *log_domain,
440 register GLogDomain *domain;
442 g_return_if_fail (handler_id > 0);
447 g_mutex_lock (g_messages_lock);
449 domain = g_log_find_domain (log_domain);
452 register GLogHandler *work, *last;
455 work = domain->handlers;
458 if (work->id == handler_id)
461 last->next = work->next;
463 domain->handlers = work->next;
465 g_log_domain_check_free (domain);
467 g_mutex_unlock (g_messages_lock);
475 g_mutex_unlock (g_messages_lock);
476 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
482 g_logv (const gchar *log_domain,
483 GLogLevelFlags log_level,
490 #ifndef HAVE_VSNPRINTF
492 #endif /* HAVE_VSNPRINTF */
494 log_level &= G_LOG_LEVEL_MASK;
498 /* we use a stack buffer of fixed size, because we might get called
501 #ifdef HAVE_VSNPRINTF
502 vsnprintf (buffer, 1024, format, args1);
503 #else /* !HAVE_VSNPRINTF */
504 G_VA_COPY (args2, args1);
505 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
506 vsprintf (buffer, format, args2);
509 /* since we might be out of memory, we can't use g_vsnprintf(). */
510 /* we are out of luck here */
511 strncpy (buffer, format, 1024);
515 #endif /* !HAVE_VSNPRINTF */
517 if (!_g_debug_initialized)
520 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
522 register GLogLevelFlags test_level;
525 if (log_level & test_level)
527 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
530 gpointer data = NULL;
532 g_mutex_lock (g_messages_lock);
534 domain = g_log_find_domain (log_domain ? log_domain : "");
537 test_level |= G_LOG_FLAG_RECURSION;
540 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
542 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
543 g_log_always_fatal) & test_level) != 0)
544 test_level |= G_LOG_FLAG_FATAL;
546 log_func = g_log_domain_get_handler (domain, test_level, &data);
547 g_mutex_unlock (g_messages_lock);
549 log_func (log_domain, test_level, buffer, data);
551 /* *domain can be cluttered now */
553 if (test_level & G_LOG_FLAG_FATAL)
556 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
558 #if defined (G_ENABLE_DEBUG) && (defined (SIGTRAP) || defined (G_OS_WIN32))
559 if (!(test_level & G_LOG_FLAG_RECURSION))
563 #else /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
565 #endif /* !G_ENABLE_DEBUG || !(SIGTRAP || G_OS_WIN32) */
569 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
575 g_log (const gchar *log_domain,
576 GLogLevelFlags log_level,
582 va_start (args, format);
583 g_logv (log_domain, log_level, format, args);
588 g_log_default_handler (const gchar *log_domain,
589 GLogLevelFlags log_level,
590 const gchar *message,
591 gpointer unused_data)
594 gboolean in_recursion;
597 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
598 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
599 log_level &= G_LOG_LEVEL_MASK;
602 message = "g_log_default_handler(): (NULL) message";
605 /* Use just stdout as stderr is hard to get redirected from the
609 gonna_abort = is_fatal;
611 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
616 case G_LOG_LEVEL_ERROR:
617 /* use write(2) for output, in case we are out of memeory */
618 ensure_stdout_valid ();
620 g_log_write_prefix (fd, log_level);
624 write (fd, log_domain, strlen (log_domain));
628 write (fd, "** ", 3);
630 write (fd, "ERROR (recursed) **: ", 21);
632 write (fd, "ERROR **: ", 10);
633 write (fd, message, strlen (message));
635 write (fd, "\naborting...\n", 13);
639 case G_LOG_LEVEL_CRITICAL:
640 ensure_stdout_valid ();
642 g_log_write_prefix (fd, log_level);
646 write (fd, log_domain, strlen (log_domain));
650 write (fd, "** ", 3);
652 write (fd, "CRITICAL (recursed) **: ", 24);
654 write (fd, "CRITICAL **: ", 13);
655 write (fd, message, strlen (message));
657 write (fd, "\naborting...\n", 13);
661 case G_LOG_LEVEL_WARNING:
662 ensure_stdout_valid ();
664 g_log_write_prefix (fd, log_level);
668 write (fd, log_domain, strlen (log_domain));
672 write (fd, "** ", 3);
674 write (fd, "WARNING (recursed) **: ", 23);
676 write (fd, "WARNING **: ", 12);
677 write (fd, message, strlen (message));
679 write (fd, "\naborting...\n", 13);
683 case G_LOG_LEVEL_MESSAGE:
684 ensure_stdout_valid ();
686 g_log_write_prefix (fd, log_level);
690 write (fd, log_domain, strlen (log_domain));
694 write (fd, "Message (recursed): ", 20);
696 write (fd, "Message: ", 9);
697 write (fd, message, strlen (message));
699 write (fd, "\naborting...\n", 13);
703 case G_LOG_LEVEL_INFO:
704 ensure_stdout_valid ();
706 g_log_write_prefix (fd, log_level);
710 write (fd, log_domain, strlen (log_domain));
714 write (fd, "INFO (recursed): ", 17);
716 write (fd, "INFO: ", 6);
717 write (fd, message, strlen (message));
719 write (fd, "\naborting...\n", 13);
723 case G_LOG_LEVEL_DEBUG:
724 ensure_stdout_valid ();
726 g_log_write_prefix (fd, log_level);
730 write (fd, log_domain, strlen (log_domain));
734 write (fd, "DEBUG (recursed): ", 18);
736 write (fd, "DEBUG: ", 7);
737 write (fd, message, strlen (message));
739 write (fd, "\naborting...\n", 13);
744 /* we are used for a log level that is not defined by GLib itself,
745 * try to make the best out of it.
747 ensure_stdout_valid ();
749 g_log_write_prefix (fd, log_level);
753 write (fd, log_domain, strlen (log_domain));
755 write (fd, "-LOG (recursed:", 15);
757 write (fd, "-LOG (", 6);
759 else if (in_recursion)
760 write (fd, "LOG (recursed:", 14);
762 write (fd, "LOG (", 5);
765 gchar string[] = "0x00): ";
766 gchar *p = string + 2;
769 i = g_bit_nth_msf (log_level, -1);
772 *p = '0' + (i & 0xf);
776 write (fd, string, 7);
779 write (fd, "): ", 3);
780 write (fd, message, strlen (message));
782 write (fd, "\naborting...\n", 13);
790 g_set_print_handler (GPrintFunc func)
792 GPrintFunc old_print_func;
794 g_mutex_lock (g_messages_lock);
795 old_print_func = glib_print_func;
796 glib_print_func = func;
797 g_mutex_unlock (g_messages_lock);
799 return old_print_func;
803 g_print (const gchar *format,
808 GPrintFunc local_glib_print_func;
810 g_return_if_fail (format != NULL);
812 va_start (args, format);
813 string = g_strdup_vprintf (format, args);
816 g_mutex_lock (g_messages_lock);
817 local_glib_print_func = glib_print_func;
818 g_mutex_unlock (g_messages_lock);
820 if (local_glib_print_func)
821 local_glib_print_func (string);
824 ensure_stdout_valid ();
825 fputs (string, stdout);
832 g_set_printerr_handler (GPrintFunc func)
834 GPrintFunc old_printerr_func;
836 g_mutex_lock (g_messages_lock);
837 old_printerr_func = glib_printerr_func;
838 glib_printerr_func = func;
839 g_mutex_unlock (g_messages_lock);
841 return old_printerr_func;
845 g_printerr (const gchar *format,
850 GPrintFunc local_glib_printerr_func;
852 g_return_if_fail (format != NULL);
854 va_start (args, format);
855 string = g_strdup_vprintf (format, args);
858 g_mutex_lock (g_messages_lock);
859 local_glib_printerr_func = glib_printerr_func;
860 g_mutex_unlock (g_messages_lock);
862 if (local_glib_printerr_func)
863 local_glib_printerr_func (string);
866 fputs (string, stderr);
873 # define MB_LEN_MAX 8
876 #ifndef HAVE_C99_VSNPRINTF
882 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
883 gboolean add_space, add_sign, possible_sign, seen_precision;
884 gboolean mod_half, mod_long, mod_extra_long;
888 printf_string_upper_bound (const gchar *format,
892 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
900 register gchar c = *format++;
904 else /* (c == '%') */
906 PrintfArgSpec spec = { 0, };
907 gboolean seen_l = FALSE, conv_done = FALSE;
909 const gchar *spec_start = format;
916 GDoubleIEEE754 u_double;
919 const gchar *v_string;
921 /* beware of positional parameters
925 g_warning (G_GNUC_PRETTY_FUNCTION
926 "(): unable to handle positional parameters (%%n$)");
927 len += 1024; /* try adding some safety padding */
934 spec.alternate_format = TRUE;
937 spec.zero_padding = TRUE;
940 spec.adjust_left = TRUE;
943 spec.add_space = TRUE;
946 spec.add_sign = TRUE;
949 spec.locale_grouping = TRUE;
952 /* parse output size specifications
955 spec.seen_precision = TRUE;
968 while (c >= '0' && c <= '9')
971 v_uint = v_uint * 10 + c - '0';
974 if (spec.seen_precision)
975 spec.precision = MAX (spec.precision, v_uint);
977 spec.min_width = MAX (spec.min_width, v_uint);
980 v_int = va_arg (args, int);
981 if (spec.seen_precision)
983 /* forget about negative precision */
985 spec.precision = MAX (spec.precision, v_int);
992 spec.adjust_left = TRUE;
994 spec.min_width = MAX (spec.min_width, v_int);
998 /* parse type modifiers
1001 spec.mod_half = TRUE;
1006 spec.mod_long = TRUE;
1010 /* else, fall through */
1013 spec.mod_long = TRUE;
1014 spec.mod_extra_long = TRUE;
1018 #if GLIB_SIZEOF_SIZE_T > 4
1019 spec.mod_long = TRUE;
1020 spec.mod_extra_long = TRUE;
1021 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1024 #if GLIB_SIZEOF_PTRDIFF_T > 4
1025 spec.mod_long = TRUE;
1026 spec.mod_extra_long = TRUE;
1027 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1030 #if GLIB_SIZEOF_INTMAX_T > 4
1031 spec.mod_long = TRUE;
1032 spec.mod_extra_long = TRUE;
1033 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1036 /* parse output conversions
1045 /* some C libraries feature long variants for these as well? */
1046 spec.mod_long = TRUE;
1053 conv_len += 1; /* sign */
1060 spec.possible_sign = TRUE;
1062 if (spec.mod_long && honour_longs)
1064 if (spec.mod_extra_long)
1066 if (spec.mod_extra_long)
1068 (void) va_arg (args, gint64);
1070 else if (spec.mod_long)
1071 (void) va_arg (args, long);
1073 (void) va_arg (args, int);
1085 spec.possible_sign = TRUE;
1086 /* n . dddddddddddddddddddddddd E +- eeee */
1087 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1088 if (may_warn && spec.mod_extra_long)
1089 g_warning (G_GNUC_PRETTY_FUNCTION
1090 "(): unable to handle long double, collecting double only");
1091 #ifdef HAVE_LONG_DOUBLE
1092 #error need to implement special handling for long double
1094 u_double.v_double = va_arg (args, double);
1095 /* %f can expand up to all significant digits before '.' (308) */
1097 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1099 gint exp = u_double.mpn.biased_exponent;
1101 exp -= G_IEEE754_DOUBLE_BIAS;
1102 exp = exp * G_LOG_2_BASE_10 + 1;
1103 conv_len += ABS (exp); /* exp can be <0 */
1105 /* some printf() implementations require extra padding for rounding */
1107 /* we can't really handle locale specific grouping here */
1108 if (spec.locale_grouping)
1112 spec.mod_long = TRUE;
1115 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1116 (void) va_arg (args, int);
1119 spec.mod_long = TRUE;
1122 v_string = va_arg (args, char*);
1124 conv_len += 8; /* hold "(null)" */
1125 else if (spec.seen_precision)
1126 conv_len += spec.precision;
1128 conv_len += strlen (v_string);
1133 g_warning (G_GNUC_PRETTY_FUNCTION
1134 "(): unable to handle wide char strings");
1135 len += 1024; /* try adding some safety padding */
1138 case 'P': /* do we actually need this? */
1141 spec.alternate_format = TRUE;
1148 (void) va_arg (args, void*);
1151 /* there's not much we can do to be clever */
1152 v_string = g_strerror (errno);
1153 v_uint = v_string ? strlen (v_string) : 0;
1154 conv_len += MAX (256, v_uint);
1157 /* handle invalid cases
1160 /* no conversion specification, bad bad */
1161 conv_len += format - spec_start;
1165 g_warning (G_GNUC_PRETTY_FUNCTION
1166 "(): unable to handle `%c' while parsing format",
1170 conv_done |= conv_len > 0;
1173 /* handle width specifications */
1174 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1176 conv_len += spec.alternate_format ? 2 : 0;
1177 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1180 } /* else (c == '%') */
1181 } /* while (*format) */
1186 #endif /* !HAVE_C99_VSNPRINTF */
1190 g_printf_string_upper_bound (const gchar *format,
1193 #if HAVE_C99_VSNPRINTF
1195 return vsnprintf (&c, 1, format, args) + 1;
1197 return printf_string_upper_bound (format, TRUE, args);
1202 g_messages_init (void)
1204 g_messages_lock = g_mutex_new ();
1205 g_log_depth = g_private_new (NULL);
1206 g_log_msg_prefix_init ();
1210 gboolean _g_debug_initialized = FALSE;
1211 guint _g_debug_flags = 0;
1213 void _g_debug_init ()
1217 _g_debug_initialized = TRUE;
1219 val = g_getenv ("G_DEBUG");
1222 static const GDebugKey keys[] = {
1223 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS}
1226 _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1229 if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1231 GLogLevelFlags fatal_mask;
1233 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1234 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1235 g_log_set_always_fatal (fatal_mask);