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 /* --- structures --- */
49 typedef struct _GLogDomain GLogDomain;
50 typedef struct _GLogHandler GLogHandler;
54 GLogLevelFlags fatal_mask;
55 GLogHandler *handlers;
61 GLogLevelFlags log_level;
68 /* --- prototypes --- */
69 static inline guint printf_string_upper_bound (const gchar *format,
74 /* --- variables --- */
76 static GMutex* g_messages_lock = NULL;
78 const gchar *g_log_domain_glib = "GLib";
79 static GLogDomain *g_log_domains = NULL;
80 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
81 static GPrintFunc glib_print_func = NULL;
82 static GPrintFunc glib_printerr_func = NULL;
83 static GErrorFunc glib_error_func = NULL;
84 static GWarningFunc glib_warning_func = NULL;
85 static GPrintFunc glib_message_func = NULL;
87 static GPrivate* g_log_depth = NULL;
89 /* --- functions --- */
94 # include <process.h> /* For _getpid() */
96 static gboolean alloc_console_called = FALSE;
98 static gboolean gonna_abort = FALSE;
100 /* This default message will usually be overwritten. */
101 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
102 * with huge strings, is it? */
103 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
104 static char *fatal_msg_ptr = fatal_msg_buf;
106 /* Just use stdio. If we're out of memory, we're hosed anyway. */
115 memcpy (fatal_msg_ptr, buf, len);
116 fatal_msg_ptr += len;
121 fwrite (buf, len, 1, fd);
127 #define write(fd, buf, len) dowrite(fd, buf, len)
130 ensure_stdout_valid (void)
137 if (!alloc_console_called)
139 handle = GetStdHandle (STD_OUTPUT_HANDLE);
141 if (handle == INVALID_HANDLE_VALUE)
144 alloc_console_called = TRUE;
145 freopen ("CONOUT$", "w", stdout);
150 #define ensure_stdout_valid() /* Define as empty */
154 write_unsigned (gint fd,
163 g_return_if_fail (radix >= 2 && radix <= 36);
192 buffer[i] = c + 'a' - 10;
196 write (fd, buffer, n);
200 write_string (gint fd,
203 write (fd, string, strlen (string));
207 g_log_write_prefix (gint fd,
210 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
211 static gboolean initialized = FALSE;
213 g_mutex_lock (g_messages_lock);
220 val = g_getenv ("G_MESSAGES_PREFIXED");
224 static const GDebugKey keys[] = {
225 { "error", G_LOG_LEVEL_ERROR },
226 { "critical", G_LOG_LEVEL_CRITICAL },
227 { "warning", G_LOG_LEVEL_WARNING },
228 { "message", G_LOG_LEVEL_MESSAGE },
229 { "info", G_LOG_LEVEL_INFO },
230 { "debug", G_LOG_LEVEL_DEBUG }
233 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
237 g_mutex_unlock (g_messages_lock);
239 if ((g_log_msg_prefix & mask) == mask)
243 prg_name = g_get_prgname ();
246 write_string (fd, "(process:");
249 write_string (fd, prg_name);
250 write_string (fd, " (pid:");
253 write_unsigned (fd, getpid (), 10);
254 write_string (fd, "): ");
258 static inline GLogDomain*
259 g_log_find_domain (const gchar *log_domain)
261 register GLogDomain *domain;
263 g_mutex_lock (g_messages_lock);
264 domain = g_log_domains;
267 if (strcmp (domain->log_domain, log_domain) == 0)
269 g_mutex_unlock (g_messages_lock);
272 domain = domain->next;
274 g_mutex_unlock (g_messages_lock);
278 static inline GLogDomain*
279 g_log_domain_new (const gchar *log_domain)
281 register GLogDomain *domain;
283 domain = g_new (GLogDomain, 1);
284 domain->log_domain = g_strdup (log_domain);
285 domain->fatal_mask = G_LOG_FATAL_MASK;
286 domain->handlers = NULL;
288 g_mutex_lock (g_messages_lock);
289 domain->next = g_log_domains;
290 g_log_domains = domain;
291 g_mutex_unlock (g_messages_lock);
297 g_log_domain_check_free (GLogDomain *domain)
299 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
300 domain->handlers == NULL)
302 register GLogDomain *last, *work;
306 g_mutex_lock (g_messages_lock);
307 work = g_log_domains;
313 last->next = domain->next;
315 g_log_domains = domain->next;
316 g_free (domain->log_domain);
323 g_mutex_unlock (g_messages_lock);
327 static inline GLogFunc
328 g_log_domain_get_handler (GLogDomain *domain,
329 GLogLevelFlags log_level,
332 if (domain && log_level)
334 register GLogHandler *handler;
336 handler = domain->handlers;
339 if ((handler->log_level & log_level) == log_level)
341 *data = handler->data;
342 return handler->log_func;
344 handler = handler->next;
347 return g_log_default_handler;
351 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
353 GLogLevelFlags old_mask;
355 /* restrict the global mask to levels that are known to glib */
356 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
357 /* force errors to be fatal */
358 fatal_mask |= G_LOG_LEVEL_ERROR;
359 /* remove bogus flag */
360 fatal_mask &= ~G_LOG_FLAG_FATAL;
362 g_mutex_lock (g_messages_lock);
363 old_mask = g_log_always_fatal;
364 g_log_always_fatal = fatal_mask;
365 g_mutex_unlock (g_messages_lock);
371 g_log_set_fatal_mask (const gchar *log_domain,
372 GLogLevelFlags fatal_mask)
374 GLogLevelFlags old_flags;
375 register GLogDomain *domain;
380 /* force errors to be fatal */
381 fatal_mask |= G_LOG_LEVEL_ERROR;
382 /* remove bogus flag */
383 fatal_mask &= ~G_LOG_FLAG_FATAL;
385 domain = g_log_find_domain (log_domain);
387 domain = g_log_domain_new (log_domain);
388 old_flags = domain->fatal_mask;
390 domain->fatal_mask = fatal_mask;
391 g_log_domain_check_free (domain);
397 g_log_set_handler (const gchar *log_domain,
398 GLogLevelFlags log_levels,
402 register GLogDomain *domain;
403 register GLogHandler *handler;
404 static guint handler_id = 0;
406 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
407 g_return_val_if_fail (log_func != NULL, 0);
412 domain = g_log_find_domain (log_domain);
414 domain = g_log_domain_new (log_domain);
416 handler = g_new (GLogHandler, 1);
417 g_mutex_lock (g_messages_lock);
418 handler->id = ++handler_id;
419 g_mutex_unlock (g_messages_lock);
420 handler->log_level = log_levels;
421 handler->log_func = log_func;
422 handler->data = user_data;
423 handler->next = domain->handlers;
424 domain->handlers = handler;
430 g_log_remove_handler (const gchar *log_domain,
433 register GLogDomain *domain;
435 g_return_if_fail (handler_id > 0);
440 domain = g_log_find_domain (log_domain);
443 register GLogHandler *work, *last;
446 work = domain->handlers;
449 if (work->id == handler_id)
452 last->next = work->next;
454 domain->handlers = work->next;
456 g_log_domain_check_free (domain);
463 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
469 g_logv (const gchar *log_domain,
470 GLogLevelFlags log_level,
478 log_level &= G_LOG_LEVEL_MASK;
482 /* we use a stack buffer of fixed size, because we might get called
485 G_VA_COPY (args2, args1);
486 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
487 vsprintf (buffer, format, args2);
490 /* since we might be out of memory, we can't use g_vsnprintf(). */
491 #ifdef HAVE_VSNPRINTF
492 vsnprintf (buffer, 1024, format, args2);
493 #else /* !HAVE_VSNPRINTF */
494 /* we are out of luck here */
495 strncpy (buffer, format, 1024);
496 #endif /* !HAVE_VSNPRINTF */
501 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
503 register GLogLevelFlags test_level;
506 if (log_level & test_level)
508 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
511 gpointer data = NULL;
513 domain = g_log_find_domain (log_domain ? log_domain : "");
516 test_level |= G_LOG_FLAG_RECURSION;
519 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
521 g_mutex_lock (g_messages_lock);
522 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
523 g_log_always_fatal) & test_level) != 0)
524 test_level |= G_LOG_FLAG_FATAL;
525 g_mutex_unlock (g_messages_lock);
527 log_func = g_log_domain_get_handler (domain, test_level, &data);
528 log_func (log_domain, test_level, buffer, data);
530 /* *domain can be cluttered now */
532 if (test_level & G_LOG_FLAG_FATAL)
534 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
535 if (!(test_level & G_LOG_FLAG_RECURSION))
539 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
541 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
543 # if defined (_MSC_VER) && defined (_DEBUG)
544 /* let's see the call stack ... */
548 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
552 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
558 g_log (const gchar *log_domain,
559 GLogLevelFlags log_level,
565 va_start (args, format);
566 g_logv (log_domain, log_level, format, args);
571 g_log_default_handler (const gchar *log_domain,
572 GLogLevelFlags log_level,
573 const gchar *message,
574 gpointer unused_data)
581 gboolean in_recursion;
583 GErrorFunc local_glib_error_func;
584 GWarningFunc local_glib_warning_func;
585 GPrintFunc local_glib_message_func;
587 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
588 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
589 log_level &= G_LOG_LEVEL_MASK;
592 message = "g_log_default_handler(): (NULL) message";
595 /* Use just stdout as stderr is hard to get redirected from the
599 gonna_abort = is_fatal;
601 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
604 g_mutex_lock (g_messages_lock);
605 local_glib_error_func = glib_error_func;
606 local_glib_warning_func = glib_warning_func;
607 local_glib_message_func = glib_message_func;
608 g_mutex_unlock (g_messages_lock);
612 case G_LOG_LEVEL_ERROR:
613 if (!log_domain && local_glib_error_func)
615 /* compatibility code */
616 local_glib_error_func (message);
619 /* use write(2) for output, in case we are out of memeory */
620 ensure_stdout_valid ();
622 g_log_write_prefix (fd, log_level);
626 write (fd, log_domain, strlen (log_domain));
630 write (fd, "** ", 3);
632 write (fd, "ERROR (recursed) **: ", 21);
634 write (fd, "ERROR **: ", 10);
635 write (fd, message, strlen (message));
637 write (fd, "\naborting...\n", 13);
641 case G_LOG_LEVEL_CRITICAL:
642 ensure_stdout_valid ();
644 g_log_write_prefix (fd, log_level);
648 write (fd, log_domain, strlen (log_domain));
652 write (fd, "** ", 3);
654 write (fd, "CRITICAL (recursed) **: ", 24);
656 write (fd, "CRITICAL **: ", 13);
657 write (fd, message, strlen (message));
659 write (fd, "\naborting...\n", 13);
663 case G_LOG_LEVEL_WARNING:
664 if (!log_domain && local_glib_warning_func)
666 /* compatibility code */
667 local_glib_warning_func (message);
670 ensure_stdout_valid ();
672 g_log_write_prefix (fd, log_level);
676 write (fd, log_domain, strlen (log_domain));
680 write (fd, "** ", 3);
682 write (fd, "WARNING (recursed) **: ", 23);
684 write (fd, "WARNING **: ", 12);
685 write (fd, message, strlen (message));
687 write (fd, "\naborting...\n", 13);
691 case G_LOG_LEVEL_MESSAGE:
692 if (!log_domain && local_glib_message_func)
694 /* compatibility code */
695 local_glib_message_func (message);
698 ensure_stdout_valid ();
700 g_log_write_prefix (fd, log_level);
704 write (fd, log_domain, strlen (log_domain));
708 write (fd, "Message (recursed): ", 20);
710 write (fd, "Message: ", 9);
711 write (fd, message, strlen (message));
713 write (fd, "\naborting...\n", 13);
717 case G_LOG_LEVEL_INFO:
718 ensure_stdout_valid ();
720 g_log_write_prefix (fd, log_level);
724 write (fd, log_domain, strlen (log_domain));
728 write (fd, "INFO (recursed): ", 17);
730 write (fd, "INFO: ", 6);
731 write (fd, message, strlen (message));
733 write (fd, "\naborting...\n", 13);
737 case G_LOG_LEVEL_DEBUG:
738 ensure_stdout_valid ();
740 g_log_write_prefix (fd, log_level);
744 write (fd, log_domain, strlen (log_domain));
748 write (fd, "DEBUG (recursed): ", 18);
750 write (fd, "DEBUG: ", 7);
751 write (fd, message, strlen (message));
753 write (fd, "\naborting...\n", 13);
758 /* we are used for a log level that is not defined by GLib itself,
759 * try to make the best out of it.
761 ensure_stdout_valid ();
763 g_log_write_prefix (fd, log_level);
767 write (fd, log_domain, strlen (log_domain));
769 write (fd, "-LOG (recursed:", 15);
771 write (fd, "-LOG (", 6);
773 else if (in_recursion)
774 write (fd, "LOG (recursed:", 14);
776 write (fd, "LOG (", 5);
779 gchar string[] = "0x00): ";
780 gchar *p = string + 2;
783 i = g_bit_nth_msf (log_level, -1);
786 *p = '0' + (i & 0xf);
790 write (fd, string, 7);
793 write (fd, "): ", 3);
794 write (fd, message, strlen (message));
796 write (fd, "\naborting...\n", 13);
804 g_set_print_handler (GPrintFunc func)
806 GPrintFunc old_print_func;
808 g_mutex_lock (g_messages_lock);
809 old_print_func = glib_print_func;
810 glib_print_func = func;
811 g_mutex_unlock (g_messages_lock);
813 return old_print_func;
817 g_print (const gchar *format,
822 GPrintFunc local_glib_print_func;
824 g_return_if_fail (format != NULL);
826 va_start (args, format);
827 string = g_strdup_vprintf (format, args);
830 g_mutex_lock (g_messages_lock);
831 local_glib_print_func = glib_print_func;
832 g_mutex_unlock (g_messages_lock);
834 if (local_glib_print_func)
835 local_glib_print_func (string);
838 ensure_stdout_valid ();
839 fputs (string, stdout);
846 g_set_printerr_handler (GPrintFunc func)
848 GPrintFunc old_printerr_func;
850 g_mutex_lock (g_messages_lock);
851 old_printerr_func = glib_printerr_func;
852 glib_printerr_func = func;
853 g_mutex_unlock (g_messages_lock);
855 return old_printerr_func;
859 g_printerr (const gchar *format,
864 GPrintFunc local_glib_printerr_func;
866 g_return_if_fail (format != NULL);
868 va_start (args, format);
869 string = g_strdup_vprintf (format, args);
872 g_mutex_lock (g_messages_lock);
873 local_glib_printerr_func = glib_printerr_func;
874 g_mutex_unlock (g_messages_lock);
876 if (local_glib_printerr_func)
877 local_glib_printerr_func (string);
880 fputs (string, stderr);
886 /* compatibility code */
888 g_set_error_handler (GErrorFunc func)
890 GErrorFunc old_error_func;
892 g_mutex_lock (g_messages_lock);
893 old_error_func = glib_error_func;
894 glib_error_func = func;
895 g_mutex_unlock (g_messages_lock);
897 return old_error_func;
900 /* compatibility code */
902 g_set_warning_handler (GWarningFunc func)
904 GWarningFunc old_warning_func;
906 g_mutex_lock (g_messages_lock);
907 old_warning_func = glib_warning_func;
908 glib_warning_func = func;
909 g_mutex_unlock (g_messages_lock);
911 return old_warning_func;
914 /* compatibility code */
916 g_set_message_handler (GPrintFunc func)
918 GPrintFunc old_message_func;
920 g_mutex_lock (g_messages_lock);
921 old_message_func = glib_message_func;
922 glib_message_func = func;
923 g_mutex_unlock (g_messages_lock);
925 return old_message_func;
929 # define MB_LEN_MAX 8
936 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
937 gboolean add_space, add_sign, possible_sign, seen_precision;
938 gboolean mod_half, mod_long, mod_extra_long;
942 printf_string_upper_bound (const gchar *format,
946 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
954 register gchar c = *format++;
958 else /* (c == '%') */
960 PrintfArgSpec spec = { 0, };
961 gboolean seen_l = FALSE, conv_done = FALSE;
963 const gchar *spec_start = format;
970 GDoubleIEEE754 u_double;
973 const gchar *v_string;
975 /* beware of positional parameters
979 g_warning (G_GNUC_PRETTY_FUNCTION
980 "(): unable to handle positional parameters (%%n$)");
981 len += 1024; /* try adding some safety padding */
987 spec.alternate_format = TRUE;
990 spec.zero_padding = TRUE;
993 spec.adjust_left = TRUE;
996 spec.add_space = TRUE;
999 spec.add_sign = TRUE;
1002 spec.locale_grouping = TRUE;
1005 /* parse output size specifications
1008 spec.seen_precision = TRUE;
1021 while (c >= '0' && c <= '9')
1024 v_uint = v_uint * 10 + c - '0';
1027 if (spec.seen_precision)
1028 spec.precision = MAX (spec.precision, v_uint);
1030 spec.min_width = MAX (spec.min_width, v_uint);
1033 v_int = va_arg (args, int);
1034 if (spec.seen_precision)
1036 /* forget about negative precision */
1038 spec.precision = MAX (spec.precision, v_int);
1045 spec.adjust_left = TRUE;
1047 spec.min_width = MAX (spec.min_width, v_int);
1051 /* parse type modifiers
1054 spec.mod_half = TRUE;
1059 spec.mod_long = TRUE;
1063 /* else, fall through */
1066 spec.mod_long = TRUE;
1067 spec.mod_extra_long = TRUE;
1071 #if GLIB_SIZEOF_SIZE_T > 4
1072 spec.mod_long = TRUE;
1073 spec.mod_extra_long = TRUE;
1074 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1077 #if GLIB_SIZEOF_PTRDIFF_T > 4
1078 spec.mod_long = TRUE;
1079 spec.mod_extra_long = TRUE;
1080 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1083 #if GLIB_SIZEOF_INTMAX_T > 4
1084 spec.mod_long = TRUE;
1085 spec.mod_extra_long = TRUE;
1086 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1089 /* parse output conversions
1098 /* some C libraries feature long variants for these as well? */
1099 spec.mod_long = TRUE;
1106 conv_len += 1; /* sign */
1113 spec.possible_sign = TRUE;
1115 if (spec.mod_long && honour_longs)
1117 if (spec.mod_extra_long)
1119 if (spec.mod_extra_long)
1121 #ifdef G_HAVE_GINT64
1122 (void) va_arg (args, gint64);
1123 #else /* !G_HAVE_GINT64 */
1124 (void) va_arg (args, long);
1125 #endif /* !G_HAVE_GINT64 */
1127 else if (spec.mod_long)
1128 (void) va_arg (args, long);
1130 (void) va_arg (args, int);
1142 spec.possible_sign = TRUE;
1143 /* n . dddddddddddddddddddddddd E +- eeee */
1144 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1145 if (may_warn && spec.mod_extra_long)
1146 g_warning (G_GNUC_PRETTY_FUNCTION
1147 "(): unable to handle long double, collecting double only");
1148 #ifdef HAVE_LONG_DOUBLE
1149 #error need to implement special handling for long double
1151 u_double.v_double = va_arg (args, double);
1152 /* %f can expand up to all significant digits before '.' (308) */
1154 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1156 gint exp = u_double.mpn.biased_exponent;
1158 exp -= G_IEEE754_DOUBLE_BIAS;
1159 exp = exp * G_LOG_2_BASE_10 + 1;
1162 /* some printf() implementations require extra padding for rounding */
1164 /* we can't really handle locale specific grouping here */
1165 if (spec.locale_grouping)
1169 spec.mod_long = TRUE;
1172 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1173 (void) va_arg (args, int);
1176 spec.mod_long = TRUE;
1179 v_string = va_arg (args, char*);
1181 conv_len += 8; /* hold "(null)" */
1182 else if (spec.seen_precision)
1183 conv_len += spec.precision;
1185 conv_len += strlen (v_string);
1190 g_warning (G_GNUC_PRETTY_FUNCTION
1191 "(): unable to handle wide char strings");
1192 len += 1024; /* try adding some safety padding */
1195 case 'P': /* do we actually need this? */
1198 spec.alternate_format = TRUE;
1205 (void) va_arg (args, void*);
1208 /* there's not much we can do to be clever */
1209 v_string = g_strerror (errno);
1210 v_uint = v_string ? strlen (v_string) : 0;
1211 conv_len += MAX (256, v_uint);
1214 /* handle invalid cases
1217 /* no conversion specification, bad bad */
1218 conv_len += format - spec_start;
1222 g_warning (G_GNUC_PRETTY_FUNCTION
1223 "(): unable to handle `%c' while parsing format",
1227 conv_done |= conv_len > 0;
1230 /* handle width specifications */
1231 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1233 conv_len += spec.alternate_format ? 2 : 0;
1234 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1237 } /* else (c == '%') */
1238 } /* while (*format) */
1244 g_printf_string_upper_bound (const gchar *format,
1247 return printf_string_upper_bound (format, TRUE, args);
1251 g_messages_init (void)
1253 g_messages_lock = g_mutex_new();
1254 g_log_depth = g_private_new(NULL);