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 static gsize printf_string_upper_bound (const gchar *format,
79 /* --- variables --- */
81 static GMutex* g_messages_lock = NULL;
83 const gchar *g_log_domain_glib = "GLib";
84 static GLogDomain *g_log_domains = NULL;
85 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
86 static GPrintFunc glib_print_func = NULL;
87 static GPrintFunc glib_printerr_func = NULL;
89 static GPrivate* g_log_depth = NULL;
91 /* --- functions --- */
96 # include <process.h> /* For _getpid() */
98 static gboolean alloc_console_called = FALSE;
100 static gboolean gonna_abort = FALSE;
102 /* This default message will usually be overwritten. */
103 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
104 * with huge strings, is it? */
105 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
106 static char *fatal_msg_ptr = fatal_msg_buf;
108 /* Just use stdio. If we're out of memory, we're hosed anyway. */
111 dowrite (GFileDescriptor fd,
117 memcpy (fatal_msg_ptr, buf, len);
118 fatal_msg_ptr += len;
123 fwrite (buf, len, 1, fd);
129 #define write(fd, buf, len) dowrite(fd, buf, len)
132 ensure_stdout_valid (void)
139 if (!alloc_console_called)
141 handle = GetStdHandle (STD_OUTPUT_HANDLE);
143 if (handle == INVALID_HANDLE_VALUE)
146 alloc_console_called = TRUE;
147 freopen ("CONOUT$", "w", stdout);
152 #define ensure_stdout_valid() /* Define as empty */
156 write_unsigned (GFileDescriptor fd,
165 g_return_if_fail (radix >= 2 && radix <= 36);
194 buffer[i] = c + 'a' - 10;
198 write (fd, buffer, n);
202 write_string (GFileDescriptor fd,
205 write (fd, string, strlen (string));
209 g_log_write_prefix (GFileDescriptor fd,
212 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
213 static gboolean initialized = FALSE;
215 g_mutex_lock (g_messages_lock);
222 val = g_getenv ("G_MESSAGES_PREFIXED");
226 static const GDebugKey keys[] = {
227 { "error", G_LOG_LEVEL_ERROR },
228 { "critical", G_LOG_LEVEL_CRITICAL },
229 { "warning", G_LOG_LEVEL_WARNING },
230 { "message", G_LOG_LEVEL_MESSAGE },
231 { "info", G_LOG_LEVEL_INFO },
232 { "debug", G_LOG_LEVEL_DEBUG }
235 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
239 g_mutex_unlock (g_messages_lock);
241 if ((g_log_msg_prefix & mask) == mask)
245 prg_name = g_get_prgname ();
248 write_string (fd, "(process:");
251 write_string (fd, prg_name);
252 write_string (fd, " (pid:");
255 write_unsigned (fd, getpid (), 10);
256 write_string (fd, "): ");
260 static inline GLogDomain*
261 g_log_find_domain (const gchar *log_domain)
263 register GLogDomain *domain;
265 g_mutex_lock (g_messages_lock);
266 domain = g_log_domains;
269 if (strcmp (domain->log_domain, log_domain) == 0)
271 g_mutex_unlock (g_messages_lock);
274 domain = domain->next;
276 g_mutex_unlock (g_messages_lock);
280 static inline GLogDomain*
281 g_log_domain_new (const gchar *log_domain)
283 register GLogDomain *domain;
285 domain = g_new (GLogDomain, 1);
286 domain->log_domain = g_strdup (log_domain);
287 domain->fatal_mask = G_LOG_FATAL_MASK;
288 domain->handlers = NULL;
290 g_mutex_lock (g_messages_lock);
291 domain->next = g_log_domains;
292 g_log_domains = domain;
293 g_mutex_unlock (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 g_mutex_lock (g_messages_lock);
309 work = g_log_domains;
315 last->next = domain->next;
317 g_log_domains = domain->next;
318 g_free (domain->log_domain);
325 g_mutex_unlock (g_messages_lock);
329 static inline GLogFunc
330 g_log_domain_get_handler (GLogDomain *domain,
331 GLogLevelFlags log_level,
334 if (domain && log_level)
336 register GLogHandler *handler;
338 handler = domain->handlers;
341 if ((handler->log_level & log_level) == log_level)
343 *data = handler->data;
344 return handler->log_func;
346 handler = handler->next;
349 return g_log_default_handler;
353 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
355 GLogLevelFlags old_mask;
357 /* restrict the global mask to levels that are known to glib */
358 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
359 /* force errors to be fatal */
360 fatal_mask |= G_LOG_LEVEL_ERROR;
361 /* remove bogus flag */
362 fatal_mask &= ~G_LOG_FLAG_FATAL;
364 g_mutex_lock (g_messages_lock);
365 old_mask = g_log_always_fatal;
366 g_log_always_fatal = fatal_mask;
367 g_mutex_unlock (g_messages_lock);
373 g_log_set_fatal_mask (const gchar *log_domain,
374 GLogLevelFlags fatal_mask)
376 GLogLevelFlags old_flags;
377 register GLogDomain *domain;
382 /* force errors to be fatal */
383 fatal_mask |= G_LOG_LEVEL_ERROR;
384 /* remove bogus flag */
385 fatal_mask &= ~G_LOG_FLAG_FATAL;
387 domain = g_log_find_domain (log_domain);
389 domain = g_log_domain_new (log_domain);
390 old_flags = domain->fatal_mask;
392 domain->fatal_mask = fatal_mask;
393 g_log_domain_check_free (domain);
399 g_log_set_handler (const gchar *log_domain,
400 GLogLevelFlags log_levels,
404 register GLogDomain *domain;
405 register GLogHandler *handler;
406 static guint handler_id = 0;
408 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
409 g_return_val_if_fail (log_func != NULL, 0);
414 domain = g_log_find_domain (log_domain);
416 domain = g_log_domain_new (log_domain);
418 handler = g_new (GLogHandler, 1);
419 g_mutex_lock (g_messages_lock);
420 handler->id = ++handler_id;
421 g_mutex_unlock (g_messages_lock);
422 handler->log_level = log_levels;
423 handler->log_func = log_func;
424 handler->data = user_data;
425 handler->next = domain->handlers;
426 domain->handlers = handler;
432 g_log_remove_handler (const gchar *log_domain,
435 register GLogDomain *domain;
437 g_return_if_fail (handler_id > 0);
442 domain = g_log_find_domain (log_domain);
445 register GLogHandler *work, *last;
448 work = domain->handlers;
451 if (work->id == handler_id)
454 last->next = work->next;
456 domain->handlers = work->next;
458 g_log_domain_check_free (domain);
465 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
471 g_logv (const gchar *log_domain,
472 GLogLevelFlags log_level,
479 #ifndef HAVE_VSNPRINTF
481 #endif /* !HAVE_VSNPRINTF */
483 log_level &= G_LOG_LEVEL_MASK;
487 /* we use a stack buffer of fixed size, because we might get called
490 #ifdef HAVE_VSNPRINTF
491 vsnprintf (buffer, 1024, format, args1);
492 #else /* !HAVE_VSNPRINTF */
493 G_VA_COPY (args2, args1);
494 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
495 vsprintf (buffer, format, args2);
498 /* since we might be out of memory, we can't use g_vsnprintf(). */
499 /* we are out of luck here */
500 strncpy (buffer, format, 1024);
504 #endif /* !HAVE_VSNPRINTF */
506 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
508 register GLogLevelFlags test_level;
511 if (log_level & test_level)
513 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
516 gpointer data = NULL;
518 domain = g_log_find_domain (log_domain ? log_domain : "");
521 test_level |= G_LOG_FLAG_RECURSION;
524 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
526 g_mutex_lock (g_messages_lock);
527 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
528 g_log_always_fatal) & test_level) != 0)
529 test_level |= G_LOG_FLAG_FATAL;
530 g_mutex_unlock (g_messages_lock);
532 log_func = g_log_domain_get_handler (domain, test_level, &data);
533 log_func (log_domain, test_level, buffer, data);
535 /* *domain can be cluttered now */
537 if (test_level & G_LOG_FLAG_FATAL)
539 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
540 if (!(test_level & G_LOG_FLAG_RECURSION))
544 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
546 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
548 # if defined (_MSC_VER) && defined (_DEBUG)
549 /* let's see the call stack ... */
553 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
557 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
563 g_log (const gchar *log_domain,
564 GLogLevelFlags log_level,
570 va_start (args, format);
571 g_logv (log_domain, log_level, format, args);
576 g_log_default_handler (const gchar *log_domain,
577 GLogLevelFlags log_level,
578 const gchar *message,
579 gpointer unused_data)
582 gboolean in_recursion;
585 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
586 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
587 log_level &= G_LOG_LEVEL_MASK;
590 message = "g_log_default_handler(): (NULL) message";
593 /* Use just stdout as stderr is hard to get redirected from the
597 gonna_abort = is_fatal;
599 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
604 case G_LOG_LEVEL_ERROR:
605 /* use write(2) for output, in case we are out of memeory */
606 ensure_stdout_valid ();
608 g_log_write_prefix (fd, log_level);
612 write (fd, log_domain, strlen (log_domain));
616 write (fd, "** ", 3);
618 write (fd, "ERROR (recursed) **: ", 21);
620 write (fd, "ERROR **: ", 10);
621 write (fd, message, strlen (message));
623 write (fd, "\naborting...\n", 13);
627 case G_LOG_LEVEL_CRITICAL:
628 ensure_stdout_valid ();
630 g_log_write_prefix (fd, log_level);
634 write (fd, log_domain, strlen (log_domain));
638 write (fd, "** ", 3);
640 write (fd, "CRITICAL (recursed) **: ", 24);
642 write (fd, "CRITICAL **: ", 13);
643 write (fd, message, strlen (message));
645 write (fd, "\naborting...\n", 13);
649 case G_LOG_LEVEL_WARNING:
650 ensure_stdout_valid ();
652 g_log_write_prefix (fd, log_level);
656 write (fd, log_domain, strlen (log_domain));
660 write (fd, "** ", 3);
662 write (fd, "WARNING (recursed) **: ", 23);
664 write (fd, "WARNING **: ", 12);
665 write (fd, message, strlen (message));
667 write (fd, "\naborting...\n", 13);
671 case G_LOG_LEVEL_MESSAGE:
672 ensure_stdout_valid ();
674 g_log_write_prefix (fd, log_level);
678 write (fd, log_domain, strlen (log_domain));
682 write (fd, "Message (recursed): ", 20);
684 write (fd, "Message: ", 9);
685 write (fd, message, strlen (message));
687 write (fd, "\naborting...\n", 13);
691 case G_LOG_LEVEL_INFO:
692 ensure_stdout_valid ();
694 g_log_write_prefix (fd, log_level);
698 write (fd, log_domain, strlen (log_domain));
702 write (fd, "INFO (recursed): ", 17);
704 write (fd, "INFO: ", 6);
705 write (fd, message, strlen (message));
707 write (fd, "\naborting...\n", 13);
711 case G_LOG_LEVEL_DEBUG:
712 ensure_stdout_valid ();
714 g_log_write_prefix (fd, log_level);
718 write (fd, log_domain, strlen (log_domain));
722 write (fd, "DEBUG (recursed): ", 18);
724 write (fd, "DEBUG: ", 7);
725 write (fd, message, strlen (message));
727 write (fd, "\naborting...\n", 13);
732 /* we are used for a log level that is not defined by GLib itself,
733 * try to make the best out of it.
735 ensure_stdout_valid ();
737 g_log_write_prefix (fd, log_level);
741 write (fd, log_domain, strlen (log_domain));
743 write (fd, "-LOG (recursed:", 15);
745 write (fd, "-LOG (", 6);
747 else if (in_recursion)
748 write (fd, "LOG (recursed:", 14);
750 write (fd, "LOG (", 5);
753 gchar string[] = "0x00): ";
754 gchar *p = string + 2;
757 i = g_bit_nth_msf (log_level, -1);
760 *p = '0' + (i & 0xf);
764 write (fd, string, 7);
767 write (fd, "): ", 3);
768 write (fd, message, strlen (message));
770 write (fd, "\naborting...\n", 13);
778 g_set_print_handler (GPrintFunc func)
780 GPrintFunc old_print_func;
782 g_mutex_lock (g_messages_lock);
783 old_print_func = glib_print_func;
784 glib_print_func = func;
785 g_mutex_unlock (g_messages_lock);
787 return old_print_func;
791 g_print (const gchar *format,
796 GPrintFunc local_glib_print_func;
798 g_return_if_fail (format != NULL);
800 va_start (args, format);
801 string = g_strdup_vprintf (format, args);
804 g_mutex_lock (g_messages_lock);
805 local_glib_print_func = glib_print_func;
806 g_mutex_unlock (g_messages_lock);
808 if (local_glib_print_func)
809 local_glib_print_func (string);
812 ensure_stdout_valid ();
813 fputs (string, stdout);
820 g_set_printerr_handler (GPrintFunc func)
822 GPrintFunc old_printerr_func;
824 g_mutex_lock (g_messages_lock);
825 old_printerr_func = glib_printerr_func;
826 glib_printerr_func = func;
827 g_mutex_unlock (g_messages_lock);
829 return old_printerr_func;
833 g_printerr (const gchar *format,
838 GPrintFunc local_glib_printerr_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_printerr_func = glib_printerr_func;
848 g_mutex_unlock (g_messages_lock);
850 if (local_glib_printerr_func)
851 local_glib_printerr_func (string);
854 fputs (string, stderr);
861 # define MB_LEN_MAX 8
868 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
869 gboolean add_space, add_sign, possible_sign, seen_precision;
870 gboolean mod_half, mod_long, mod_extra_long;
874 printf_string_upper_bound (const gchar *format,
878 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
886 register gchar c = *format++;
890 else /* (c == '%') */
892 PrintfArgSpec spec = { 0, };
893 gboolean seen_l = FALSE, conv_done = FALSE;
895 const gchar *spec_start = format;
902 GDoubleIEEE754 u_double;
905 const gchar *v_string;
907 /* beware of positional parameters
911 g_warning (G_GNUC_PRETTY_FUNCTION
912 "(): unable to handle positional parameters (%%n$)");
913 len += 1024; /* try adding some safety padding */
920 spec.alternate_format = TRUE;
923 spec.zero_padding = TRUE;
926 spec.adjust_left = TRUE;
929 spec.add_space = TRUE;
932 spec.add_sign = TRUE;
935 spec.locale_grouping = TRUE;
938 /* parse output size specifications
941 spec.seen_precision = TRUE;
954 while (c >= '0' && c <= '9')
957 v_uint = v_uint * 10 + c - '0';
960 if (spec.seen_precision)
961 spec.precision = MAX (spec.precision, v_uint);
963 spec.min_width = MAX (spec.min_width, v_uint);
966 v_int = va_arg (args, int);
967 if (spec.seen_precision)
969 /* forget about negative precision */
971 spec.precision = MAX (spec.precision, v_int);
978 spec.adjust_left = TRUE;
980 spec.min_width = MAX (spec.min_width, v_int);
984 /* parse type modifiers
987 spec.mod_half = TRUE;
992 spec.mod_long = TRUE;
996 /* else, fall through */
999 spec.mod_long = TRUE;
1000 spec.mod_extra_long = TRUE;
1004 #if GLIB_SIZEOF_SIZE_T > 4
1005 spec.mod_long = TRUE;
1006 spec.mod_extra_long = TRUE;
1007 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1010 #if GLIB_SIZEOF_PTRDIFF_T > 4
1011 spec.mod_long = TRUE;
1012 spec.mod_extra_long = TRUE;
1013 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1016 #if GLIB_SIZEOF_INTMAX_T > 4
1017 spec.mod_long = TRUE;
1018 spec.mod_extra_long = TRUE;
1019 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1022 /* parse output conversions
1031 /* some C libraries feature long variants for these as well? */
1032 spec.mod_long = TRUE;
1039 conv_len += 1; /* sign */
1046 spec.possible_sign = TRUE;
1048 if (spec.mod_long && honour_longs)
1050 if (spec.mod_extra_long)
1052 if (spec.mod_extra_long)
1054 (void) va_arg (args, gint64);
1056 else if (spec.mod_long)
1057 (void) va_arg (args, long);
1059 (void) va_arg (args, int);
1071 spec.possible_sign = TRUE;
1072 /* n . dddddddddddddddddddddddd E +- eeee */
1073 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1074 if (may_warn && spec.mod_extra_long)
1075 g_warning (G_GNUC_PRETTY_FUNCTION
1076 "(): unable to handle long double, collecting double only");
1077 #ifdef HAVE_LONG_DOUBLE
1078 #error need to implement special handling for long double
1080 u_double.v_double = va_arg (args, double);
1081 /* %f can expand up to all significant digits before '.' (308) */
1083 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1085 gint exp = u_double.mpn.biased_exponent;
1087 exp -= G_IEEE754_DOUBLE_BIAS;
1088 exp = exp * G_LOG_2_BASE_10 + 1;
1089 conv_len += ABS (exp); /* exp can be <0 */
1091 /* some printf() implementations require extra padding for rounding */
1093 /* we can't really handle locale specific grouping here */
1094 if (spec.locale_grouping)
1098 spec.mod_long = TRUE;
1101 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1102 (void) va_arg (args, int);
1105 spec.mod_long = TRUE;
1108 v_string = va_arg (args, char*);
1110 conv_len += 8; /* hold "(null)" */
1111 else if (spec.seen_precision)
1112 conv_len += spec.precision;
1114 conv_len += strlen (v_string);
1119 g_warning (G_GNUC_PRETTY_FUNCTION
1120 "(): unable to handle wide char strings");
1121 len += 1024; /* try adding some safety padding */
1124 case 'P': /* do we actually need this? */
1127 spec.alternate_format = TRUE;
1134 (void) va_arg (args, void*);
1137 /* there's not much we can do to be clever */
1138 v_string = g_strerror (errno);
1139 v_uint = v_string ? strlen (v_string) : 0;
1140 conv_len += MAX (256, v_uint);
1143 /* handle invalid cases
1146 /* no conversion specification, bad bad */
1147 conv_len += format - spec_start;
1151 g_warning (G_GNUC_PRETTY_FUNCTION
1152 "(): unable to handle `%c' while parsing format",
1156 conv_done |= conv_len > 0;
1159 /* handle width specifications */
1160 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1162 conv_len += spec.alternate_format ? 2 : 0;
1163 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1166 } /* else (c == '%') */
1167 } /* while (*format) */
1173 g_printf_string_upper_bound (const gchar *format,
1176 return printf_string_upper_bound (format, TRUE, args);
1180 g_messages_init (void)
1182 g_messages_lock = g_mutex_new();
1183 g_log_depth = g_private_new(NULL);