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 static GLogDomain *g_log_domains = NULL;
84 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
85 static GPrintFunc glib_print_func = NULL;
86 static GPrintFunc glib_printerr_func = NULL;
88 static GPrivate* g_log_depth = NULL;
90 /* --- functions --- */
95 # include <process.h> /* For _getpid() */
97 static gboolean alloc_console_called = FALSE;
99 static gboolean gonna_abort = FALSE;
101 /* This default message will usually be overwritten. */
102 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
103 * with huge strings, is it? */
104 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
105 static char *fatal_msg_ptr = fatal_msg_buf;
107 /* Just use stdio. If we're out of memory, we're hosed anyway. */
110 dowrite (GFileDescriptor fd,
116 memcpy (fatal_msg_ptr, buf, len);
117 fatal_msg_ptr += len;
122 fwrite (buf, len, 1, fd);
128 #define write(fd, buf, len) dowrite(fd, buf, len)
131 ensure_stdout_valid (void)
138 if (!alloc_console_called)
140 handle = GetStdHandle (STD_OUTPUT_HANDLE);
142 if (handle == INVALID_HANDLE_VALUE)
145 alloc_console_called = TRUE;
146 freopen ("CONOUT$", "w", stdout);
151 #define ensure_stdout_valid() /* Define as empty */
155 write_unsigned (GFileDescriptor fd,
164 g_return_if_fail (radix >= 2 && radix <= 36);
193 buffer[i] = c + 'a' - 10;
197 write (fd, buffer, n);
201 write_string (GFileDescriptor fd,
204 write (fd, string, strlen (string));
208 g_log_write_prefix (GFileDescriptor fd,
211 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
212 static gboolean initialized = FALSE;
214 g_mutex_lock (g_messages_lock);
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));
238 g_mutex_unlock (g_messages_lock);
240 if ((g_log_msg_prefix & mask) == mask)
244 prg_name = g_get_prgname ();
247 write_string (fd, "(process:");
250 write_string (fd, prg_name);
251 write_string (fd, " (pid:");
254 write_unsigned (fd, getpid (), 10);
255 write_string (fd, "): ");
259 static inline GLogDomain*
260 g_log_find_domain (const gchar *log_domain)
262 register GLogDomain *domain;
264 g_mutex_lock (g_messages_lock);
265 domain = g_log_domains;
268 if (strcmp (domain->log_domain, log_domain) == 0)
270 g_mutex_unlock (g_messages_lock);
273 domain = domain->next;
275 g_mutex_unlock (g_messages_lock);
279 static inline GLogDomain*
280 g_log_domain_new (const gchar *log_domain)
282 register GLogDomain *domain;
284 domain = g_new (GLogDomain, 1);
285 domain->log_domain = g_strdup (log_domain);
286 domain->fatal_mask = G_LOG_FATAL_MASK;
287 domain->handlers = NULL;
289 g_mutex_lock (g_messages_lock);
290 domain->next = g_log_domains;
291 g_log_domains = domain;
292 g_mutex_unlock (g_messages_lock);
298 g_log_domain_check_free (GLogDomain *domain)
300 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
301 domain->handlers == NULL)
303 register GLogDomain *last, *work;
307 g_mutex_lock (g_messages_lock);
308 work = g_log_domains;
314 last->next = domain->next;
316 g_log_domains = domain->next;
317 g_free (domain->log_domain);
324 g_mutex_unlock (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 domain = g_log_find_domain (log_domain);
388 domain = g_log_domain_new (log_domain);
389 old_flags = domain->fatal_mask;
391 domain->fatal_mask = fatal_mask;
392 g_log_domain_check_free (domain);
398 g_log_set_handler (const gchar *log_domain,
399 GLogLevelFlags log_levels,
403 register GLogDomain *domain;
404 register GLogHandler *handler;
405 static guint handler_id = 0;
407 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
408 g_return_val_if_fail (log_func != NULL, 0);
413 domain = g_log_find_domain (log_domain);
415 domain = g_log_domain_new (log_domain);
417 handler = g_new (GLogHandler, 1);
418 g_mutex_lock (g_messages_lock);
419 handler->id = ++handler_id;
420 g_mutex_unlock (g_messages_lock);
421 handler->log_level = log_levels;
422 handler->log_func = log_func;
423 handler->data = user_data;
424 handler->next = domain->handlers;
425 domain->handlers = handler;
431 g_log_remove_handler (const gchar *log_domain,
434 register GLogDomain *domain;
436 g_return_if_fail (handler_id > 0);
441 domain = g_log_find_domain (log_domain);
444 register GLogHandler *work, *last;
447 work = domain->handlers;
450 if (work->id == handler_id)
453 last->next = work->next;
455 domain->handlers = work->next;
457 g_log_domain_check_free (domain);
464 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
470 g_logv (const gchar *log_domain,
471 GLogLevelFlags log_level,
478 #ifndef HAVE_VSNPRINTF
480 #endif /* !HAVE_VSNPRINTF */
482 log_level &= G_LOG_LEVEL_MASK;
486 /* we use a stack buffer of fixed size, because we might get called
489 #ifdef HAVE_VSNPRINTF
490 vsnprintf (buffer, 1024, format, args1);
491 #else /* !HAVE_VSNPRINTF */
492 G_VA_COPY (args2, args1);
493 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
494 vsprintf (buffer, format, args2);
497 /* since we might be out of memory, we can't use g_vsnprintf(). */
498 /* we are out of luck here */
499 strncpy (buffer, format, 1024);
503 #endif /* !HAVE_VSNPRINTF */
505 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
507 register GLogLevelFlags test_level;
510 if (log_level & test_level)
512 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
515 gpointer data = NULL;
517 domain = g_log_find_domain (log_domain ? log_domain : "");
520 test_level |= G_LOG_FLAG_RECURSION;
523 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
525 g_mutex_lock (g_messages_lock);
526 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
527 g_log_always_fatal) & test_level) != 0)
528 test_level |= G_LOG_FLAG_FATAL;
529 g_mutex_unlock (g_messages_lock);
531 log_func = g_log_domain_get_handler (domain, test_level, &data);
532 log_func (log_domain, test_level, buffer, data);
534 /* *domain can be cluttered now */
536 if (test_level & G_LOG_FLAG_FATAL)
538 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
539 if (!(test_level & G_LOG_FLAG_RECURSION))
543 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
545 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
547 # if defined (_MSC_VER) && defined (_DEBUG)
548 /* let's see the call stack ... */
552 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
556 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
562 g_log (const gchar *log_domain,
563 GLogLevelFlags log_level,
569 va_start (args, format);
570 g_logv (log_domain, log_level, format, args);
575 g_log_default_handler (const gchar *log_domain,
576 GLogLevelFlags log_level,
577 const gchar *message,
578 gpointer unused_data)
581 gboolean in_recursion;
584 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
585 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
586 log_level &= G_LOG_LEVEL_MASK;
589 message = "g_log_default_handler(): (NULL) message";
592 /* Use just stdout as stderr is hard to get redirected from the
596 gonna_abort = is_fatal;
598 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
603 case G_LOG_LEVEL_ERROR:
604 /* use write(2) for output, in case we are out of memeory */
605 ensure_stdout_valid ();
607 g_log_write_prefix (fd, log_level);
611 write (fd, log_domain, strlen (log_domain));
615 write (fd, "** ", 3);
617 write (fd, "ERROR (recursed) **: ", 21);
619 write (fd, "ERROR **: ", 10);
620 write (fd, message, strlen (message));
622 write (fd, "\naborting...\n", 13);
626 case G_LOG_LEVEL_CRITICAL:
627 ensure_stdout_valid ();
629 g_log_write_prefix (fd, log_level);
633 write (fd, log_domain, strlen (log_domain));
637 write (fd, "** ", 3);
639 write (fd, "CRITICAL (recursed) **: ", 24);
641 write (fd, "CRITICAL **: ", 13);
642 write (fd, message, strlen (message));
644 write (fd, "\naborting...\n", 13);
648 case G_LOG_LEVEL_WARNING:
649 ensure_stdout_valid ();
651 g_log_write_prefix (fd, log_level);
655 write (fd, log_domain, strlen (log_domain));
659 write (fd, "** ", 3);
661 write (fd, "WARNING (recursed) **: ", 23);
663 write (fd, "WARNING **: ", 12);
664 write (fd, message, strlen (message));
666 write (fd, "\naborting...\n", 13);
670 case G_LOG_LEVEL_MESSAGE:
671 ensure_stdout_valid ();
673 g_log_write_prefix (fd, log_level);
677 write (fd, log_domain, strlen (log_domain));
681 write (fd, "Message (recursed): ", 20);
683 write (fd, "Message: ", 9);
684 write (fd, message, strlen (message));
686 write (fd, "\naborting...\n", 13);
690 case G_LOG_LEVEL_INFO:
691 ensure_stdout_valid ();
693 g_log_write_prefix (fd, log_level);
697 write (fd, log_domain, strlen (log_domain));
701 write (fd, "INFO (recursed): ", 17);
703 write (fd, "INFO: ", 6);
704 write (fd, message, strlen (message));
706 write (fd, "\naborting...\n", 13);
710 case G_LOG_LEVEL_DEBUG:
711 ensure_stdout_valid ();
713 g_log_write_prefix (fd, log_level);
717 write (fd, log_domain, strlen (log_domain));
721 write (fd, "DEBUG (recursed): ", 18);
723 write (fd, "DEBUG: ", 7);
724 write (fd, message, strlen (message));
726 write (fd, "\naborting...\n", 13);
731 /* we are used for a log level that is not defined by GLib itself,
732 * try to make the best out of it.
734 ensure_stdout_valid ();
736 g_log_write_prefix (fd, log_level);
740 write (fd, log_domain, strlen (log_domain));
742 write (fd, "-LOG (recursed:", 15);
744 write (fd, "-LOG (", 6);
746 else if (in_recursion)
747 write (fd, "LOG (recursed:", 14);
749 write (fd, "LOG (", 5);
752 gchar string[] = "0x00): ";
753 gchar *p = string + 2;
756 i = g_bit_nth_msf (log_level, -1);
759 *p = '0' + (i & 0xf);
763 write (fd, string, 7);
766 write (fd, "): ", 3);
767 write (fd, message, strlen (message));
769 write (fd, "\naborting...\n", 13);
777 g_set_print_handler (GPrintFunc func)
779 GPrintFunc old_print_func;
781 g_mutex_lock (g_messages_lock);
782 old_print_func = glib_print_func;
783 glib_print_func = func;
784 g_mutex_unlock (g_messages_lock);
786 return old_print_func;
790 g_print (const gchar *format,
795 GPrintFunc local_glib_print_func;
797 g_return_if_fail (format != NULL);
799 va_start (args, format);
800 string = g_strdup_vprintf (format, args);
803 g_mutex_lock (g_messages_lock);
804 local_glib_print_func = glib_print_func;
805 g_mutex_unlock (g_messages_lock);
807 if (local_glib_print_func)
808 local_glib_print_func (string);
811 ensure_stdout_valid ();
812 fputs (string, stdout);
819 g_set_printerr_handler (GPrintFunc func)
821 GPrintFunc old_printerr_func;
823 g_mutex_lock (g_messages_lock);
824 old_printerr_func = glib_printerr_func;
825 glib_printerr_func = func;
826 g_mutex_unlock (g_messages_lock);
828 return old_printerr_func;
832 g_printerr (const gchar *format,
837 GPrintFunc local_glib_printerr_func;
839 g_return_if_fail (format != NULL);
841 va_start (args, format);
842 string = g_strdup_vprintf (format, args);
845 g_mutex_lock (g_messages_lock);
846 local_glib_printerr_func = glib_printerr_func;
847 g_mutex_unlock (g_messages_lock);
849 if (local_glib_printerr_func)
850 local_glib_printerr_func (string);
853 fputs (string, stderr);
860 # define MB_LEN_MAX 8
867 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
868 gboolean add_space, add_sign, possible_sign, seen_precision;
869 gboolean mod_half, mod_long, mod_extra_long;
873 printf_string_upper_bound (const gchar *format,
877 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
885 register gchar c = *format++;
889 else /* (c == '%') */
891 PrintfArgSpec spec = { 0, };
892 gboolean seen_l = FALSE, conv_done = FALSE;
894 const gchar *spec_start = format;
901 GDoubleIEEE754 u_double;
904 const gchar *v_string;
906 /* beware of positional parameters
910 g_warning (G_GNUC_PRETTY_FUNCTION
911 "(): unable to handle positional parameters (%%n$)");
912 len += 1024; /* try adding some safety padding */
919 spec.alternate_format = TRUE;
922 spec.zero_padding = TRUE;
925 spec.adjust_left = TRUE;
928 spec.add_space = TRUE;
931 spec.add_sign = TRUE;
934 spec.locale_grouping = TRUE;
937 /* parse output size specifications
940 spec.seen_precision = TRUE;
953 while (c >= '0' && c <= '9')
956 v_uint = v_uint * 10 + c - '0';
959 if (spec.seen_precision)
960 spec.precision = MAX (spec.precision, v_uint);
962 spec.min_width = MAX (spec.min_width, v_uint);
965 v_int = va_arg (args, int);
966 if (spec.seen_precision)
968 /* forget about negative precision */
970 spec.precision = MAX (spec.precision, v_int);
977 spec.adjust_left = TRUE;
979 spec.min_width = MAX (spec.min_width, v_int);
983 /* parse type modifiers
986 spec.mod_half = TRUE;
991 spec.mod_long = TRUE;
995 /* else, fall through */
998 spec.mod_long = TRUE;
999 spec.mod_extra_long = TRUE;
1003 #if GLIB_SIZEOF_SIZE_T > 4
1004 spec.mod_long = TRUE;
1005 spec.mod_extra_long = TRUE;
1006 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1009 #if GLIB_SIZEOF_PTRDIFF_T > 4
1010 spec.mod_long = TRUE;
1011 spec.mod_extra_long = TRUE;
1012 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1015 #if GLIB_SIZEOF_INTMAX_T > 4
1016 spec.mod_long = TRUE;
1017 spec.mod_extra_long = TRUE;
1018 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1021 /* parse output conversions
1030 /* some C libraries feature long variants for these as well? */
1031 spec.mod_long = TRUE;
1038 conv_len += 1; /* sign */
1045 spec.possible_sign = TRUE;
1047 if (spec.mod_long && honour_longs)
1049 if (spec.mod_extra_long)
1051 if (spec.mod_extra_long)
1053 (void) va_arg (args, gint64);
1055 else if (spec.mod_long)
1056 (void) va_arg (args, long);
1058 (void) va_arg (args, int);
1070 spec.possible_sign = TRUE;
1071 /* n . dddddddddddddddddddddddd E +- eeee */
1072 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1073 if (may_warn && spec.mod_extra_long)
1074 g_warning (G_GNUC_PRETTY_FUNCTION
1075 "(): unable to handle long double, collecting double only");
1076 #ifdef HAVE_LONG_DOUBLE
1077 #error need to implement special handling for long double
1079 u_double.v_double = va_arg (args, double);
1080 /* %f can expand up to all significant digits before '.' (308) */
1082 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1084 gint exp = u_double.mpn.biased_exponent;
1086 exp -= G_IEEE754_DOUBLE_BIAS;
1087 exp = exp * G_LOG_2_BASE_10 + 1;
1088 conv_len += ABS (exp); /* exp can be <0 */
1090 /* some printf() implementations require extra padding for rounding */
1092 /* we can't really handle locale specific grouping here */
1093 if (spec.locale_grouping)
1097 spec.mod_long = TRUE;
1100 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1101 (void) va_arg (args, int);
1104 spec.mod_long = TRUE;
1107 v_string = va_arg (args, char*);
1109 conv_len += 8; /* hold "(null)" */
1110 else if (spec.seen_precision)
1111 conv_len += spec.precision;
1113 conv_len += strlen (v_string);
1118 g_warning (G_GNUC_PRETTY_FUNCTION
1119 "(): unable to handle wide char strings");
1120 len += 1024; /* try adding some safety padding */
1123 case 'P': /* do we actually need this? */
1126 spec.alternate_format = TRUE;
1133 (void) va_arg (args, void*);
1136 /* there's not much we can do to be clever */
1137 v_string = g_strerror (errno);
1138 v_uint = v_string ? strlen (v_string) : 0;
1139 conv_len += MAX (256, v_uint);
1142 /* handle invalid cases
1145 /* no conversion specification, bad bad */
1146 conv_len += format - spec_start;
1150 g_warning (G_GNUC_PRETTY_FUNCTION
1151 "(): unable to handle `%c' while parsing format",
1155 conv_done |= conv_len > 0;
1158 /* handle width specifications */
1159 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1161 conv_len += spec.alternate_format ? 2 : 0;
1162 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1165 } /* else (c == '%') */
1166 } /* while (*format) */
1172 g_printf_string_upper_bound (const gchar *format,
1175 return printf_string_upper_bound (format, TRUE, args);
1179 g_messages_init (void)
1181 g_messages_lock = g_mutex_new();
1182 g_log_depth = g_private_new(NULL);