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;
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. */
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 */
154 static inline GLogDomain*
155 g_log_find_domain (const gchar *log_domain)
157 register GLogDomain *domain;
159 g_mutex_lock (g_messages_lock);
160 domain = g_log_domains;
163 if (strcmp (domain->log_domain, log_domain) == 0)
165 g_mutex_unlock (g_messages_lock);
168 domain = domain->next;
170 g_mutex_unlock (g_messages_lock);
174 static inline GLogDomain*
175 g_log_domain_new (const gchar *log_domain)
177 register GLogDomain *domain;
179 domain = g_new (GLogDomain, 1);
180 domain->log_domain = g_strdup (log_domain);
181 domain->fatal_mask = G_LOG_FATAL_MASK;
182 domain->handlers = NULL;
184 g_mutex_lock (g_messages_lock);
185 domain->next = g_log_domains;
186 g_log_domains = domain;
187 g_mutex_unlock (g_messages_lock);
193 g_log_domain_check_free (GLogDomain *domain)
195 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
196 domain->handlers == NULL)
198 register GLogDomain *last, *work;
202 g_mutex_lock (g_messages_lock);
203 work = g_log_domains;
209 last->next = domain->next;
211 g_log_domains = domain->next;
212 g_free (domain->log_domain);
219 g_mutex_unlock (g_messages_lock);
223 static inline GLogFunc
224 g_log_domain_get_handler (GLogDomain *domain,
225 GLogLevelFlags log_level,
228 if (domain && log_level)
230 register GLogHandler *handler;
232 handler = domain->handlers;
235 if ((handler->log_level & log_level) == log_level)
237 *data = handler->data;
238 return handler->log_func;
240 handler = handler->next;
243 return g_log_default_handler;
247 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
249 GLogLevelFlags old_mask;
251 /* restrict the global mask to levels that are known to glib */
252 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
253 /* force errors to be fatal */
254 fatal_mask |= G_LOG_LEVEL_ERROR;
255 /* remove bogus flag */
256 fatal_mask &= ~G_LOG_FLAG_FATAL;
258 g_mutex_lock (g_messages_lock);
259 old_mask = g_log_always_fatal;
260 g_log_always_fatal = fatal_mask;
261 g_mutex_unlock (g_messages_lock);
267 g_log_set_fatal_mask (const gchar *log_domain,
268 GLogLevelFlags fatal_mask)
270 GLogLevelFlags old_flags;
271 register GLogDomain *domain;
276 /* force errors to be fatal */
277 fatal_mask |= G_LOG_LEVEL_ERROR;
278 /* remove bogus flag */
279 fatal_mask &= ~G_LOG_FLAG_FATAL;
281 domain = g_log_find_domain (log_domain);
283 domain = g_log_domain_new (log_domain);
284 old_flags = domain->fatal_mask;
286 domain->fatal_mask = fatal_mask;
287 g_log_domain_check_free (domain);
293 g_log_set_handler (const gchar *log_domain,
294 GLogLevelFlags log_levels,
298 register GLogDomain *domain;
299 register GLogHandler *handler;
300 static guint handler_id = 0;
302 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
303 g_return_val_if_fail (log_func != NULL, 0);
308 domain = g_log_find_domain (log_domain);
310 domain = g_log_domain_new (log_domain);
312 handler = g_new (GLogHandler, 1);
313 g_mutex_lock (g_messages_lock);
314 handler->id = ++handler_id;
315 g_mutex_unlock (g_messages_lock);
316 handler->log_level = log_levels;
317 handler->log_func = log_func;
318 handler->data = user_data;
319 handler->next = domain->handlers;
320 domain->handlers = handler;
326 g_log_remove_handler (const gchar *log_domain,
329 register GLogDomain *domain;
331 g_return_if_fail (handler_id > 0);
336 domain = g_log_find_domain (log_domain);
339 register GLogHandler *work, *last;
342 work = domain->handlers;
345 if (work->id == handler_id)
348 last->next = work->next;
350 domain->handlers = work->next;
352 g_log_domain_check_free (domain);
359 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
365 g_logv (const gchar *log_domain,
366 GLogLevelFlags log_level,
374 log_level &= G_LOG_LEVEL_MASK;
378 /* we use a stack buffer of fixed size, because we might get called
381 G_VA_COPY (args2, args1);
382 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
383 vsprintf (buffer, format, args2);
386 /* since we might be out of memory, we can't use g_vsnprintf(). */
387 #ifdef HAVE_VSNPRINTF
388 vsnprintf (buffer, 1024, format, args2);
389 #else /* !HAVE_VSNPRINTF */
390 /* we are out of luck here */
391 strncpy (buffer, format, 1024);
392 #endif /* !HAVE_VSNPRINTF */
397 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
399 register GLogLevelFlags test_level;
402 if (log_level & test_level)
404 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
407 gpointer data = NULL;
409 domain = g_log_find_domain (log_domain ? log_domain : "");
412 test_level |= G_LOG_FLAG_RECURSION;
415 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
417 g_mutex_lock (g_messages_lock);
418 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
419 g_log_always_fatal) & test_level) != 0)
420 test_level |= G_LOG_FLAG_FATAL;
421 g_mutex_unlock (g_messages_lock);
423 log_func = g_log_domain_get_handler (domain, test_level, &data);
424 log_func (log_domain, test_level, buffer, data);
426 /* *domain can be cluttered now */
428 if (test_level & G_LOG_FLAG_FATAL)
430 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
431 if (!(test_level & G_LOG_FLAG_RECURSION))
435 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
437 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
439 # if defined (_MSC_VER) && defined (_DEBUG)
440 /* let's see the call stack ... */
444 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
448 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
454 g_log (const gchar *log_domain,
455 GLogLevelFlags log_level,
461 va_start (args, format);
462 g_logv (log_domain, log_level, format, args);
467 g_log_default_handler (const gchar *log_domain,
468 GLogLevelFlags log_level,
469 const gchar *message,
470 gpointer unused_data)
477 gboolean in_recursion;
479 GErrorFunc local_glib_error_func;
480 GWarningFunc local_glib_warning_func;
481 GPrintFunc local_glib_message_func;
482 gchar prg_pid[64], *prg_name = g_get_prgname ();
484 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
485 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
486 log_level &= G_LOG_LEVEL_MASK;
489 message = "g_log_default_handler(): (NULL) message";
492 prg_name = "(process";
493 sprintf (prg_pid, ":%u): ", getpid ());
496 sprintf (prg_pid, " (pid:%u): ", getpid ());
499 /* Use just stdout as stderr is hard to get redirected from the
503 gonna_abort = is_fatal;
505 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
508 g_mutex_lock (g_messages_lock);
509 local_glib_error_func = glib_error_func;
510 local_glib_warning_func = glib_warning_func;
511 local_glib_message_func = glib_message_func;
512 g_mutex_unlock (g_messages_lock);
516 case G_LOG_LEVEL_ERROR:
517 if (!log_domain && local_glib_error_func)
519 /* compatibility code */
520 local_glib_error_func (message);
523 /* use write(2) for output, in case we are out of memeory */
524 ensure_stdout_valid ();
526 #ifdef G_ENABLE_MSG_PREFIX
527 write (fd, prg_name, strlen (prg_name));
528 write (fd, prg_pid, strlen (prg_pid));
529 #endif /* G_ENABLE_MSG_PREFIX */
532 write (fd, log_domain, strlen (log_domain));
536 write (fd, "** ", 3);
538 write (fd, "ERROR (recursed) **: ", 21);
540 write (fd, "ERROR **: ", 10);
541 write (fd, message, strlen (message));
543 write (fd, "\naborting...\n", 13);
547 case G_LOG_LEVEL_CRITICAL:
548 ensure_stdout_valid ();
550 #ifdef G_ENABLE_MSG_PREFIX
551 write (fd, prg_name, strlen (prg_name));
552 write (fd, prg_pid, strlen (prg_pid));
553 #endif /* G_ENABLE_MSG_PREFIX */
556 write (fd, log_domain, strlen (log_domain));
560 write (fd, "** ", 3);
562 write (fd, "CRITICAL (recursed) **: ", 24);
564 write (fd, "CRITICAL **: ", 13);
565 write (fd, message, strlen (message));
567 write (fd, "\naborting...\n", 13);
571 case G_LOG_LEVEL_WARNING:
572 if (!log_domain && local_glib_warning_func)
574 /* compatibility code */
575 local_glib_warning_func (message);
578 ensure_stdout_valid ();
580 #ifdef G_ENABLE_MSG_PREFIX
581 write (fd, prg_name, strlen (prg_name));
582 write (fd, prg_pid, strlen (prg_pid));
583 #endif /* G_ENABLE_MSG_PREFIX */
586 write (fd, log_domain, strlen (log_domain));
590 write (fd, "** ", 3);
592 write (fd, "WARNING (recursed) **: ", 23);
594 write (fd, "WARNING **: ", 12);
595 write (fd, message, strlen (message));
597 write (fd, "\naborting...\n", 13);
601 case G_LOG_LEVEL_MESSAGE:
602 if (!log_domain && local_glib_message_func)
604 /* compatibility code */
605 local_glib_message_func (message);
608 ensure_stdout_valid ();
609 #ifdef G_ENABLE_MSG_PREFIX
610 write (fd, prg_name, strlen (prg_name));
611 write (fd, prg_pid, strlen (prg_pid));
612 #endif /* G_ENABLE_MSG_PREFIX */
615 write (fd, log_domain, strlen (log_domain));
619 write (fd, "Message (recursed): ", 20);
621 write (fd, "Message: ", 9);
622 write (fd, message, strlen (message));
624 write (fd, "\naborting...\n", 13);
628 case G_LOG_LEVEL_INFO:
629 ensure_stdout_valid ();
630 #ifdef G_ENABLE_MSG_PREFIX
631 write (fd, prg_name, strlen (prg_name));
632 write (fd, prg_pid, strlen (prg_pid));
633 #endif /* G_ENABLE_MSG_PREFIX */
636 write (fd, log_domain, strlen (log_domain));
640 write (fd, "INFO (recursed): ", 17);
642 write (fd, "INFO: ", 6);
643 write (fd, message, strlen (message));
645 write (fd, "\naborting...\n", 13);
649 case G_LOG_LEVEL_DEBUG:
650 ensure_stdout_valid ();
651 #ifdef G_ENABLE_MSG_PREFIX
652 write (fd, prg_name, strlen (prg_name));
653 write (fd, prg_pid, strlen (prg_pid));
654 #endif /* G_ENABLE_MSG_PREFIX */
657 write (fd, log_domain, strlen (log_domain));
661 write (fd, "DEBUG (recursed): ", 18);
663 write (fd, "DEBUG: ", 7);
664 write (fd, message, strlen (message));
666 write (fd, "\naborting...\n", 13);
671 /* we are used for a log level that is not defined by GLib itself,
672 * try to make the best out of it.
674 ensure_stdout_valid ();
675 #ifdef G_ENABLE_MSG_PREFIX
676 write (fd, prg_name, strlen (prg_name));
677 write (fd, prg_pid, strlen (prg_pid));
678 #endif /* G_ENABLE_MSG_PREFIX */
681 write (fd, log_domain, strlen (log_domain));
683 write (fd, "-LOG (recursed:", 15);
685 write (fd, "-LOG (", 6);
687 else if (in_recursion)
688 write (fd, "LOG (recursed:", 14);
690 write (fd, "LOG (", 5);
693 gchar string[] = "0x00): ";
694 gchar *p = string + 2;
697 i = g_bit_nth_msf (log_level, -1);
700 *p = '0' + (i & 0xf);
704 write (fd, string, 7);
707 write (fd, "): ", 3);
708 write (fd, message, strlen (message));
710 write (fd, "\naborting...\n", 13);
718 g_set_print_handler (GPrintFunc func)
720 GPrintFunc old_print_func;
722 g_mutex_lock (g_messages_lock);
723 old_print_func = glib_print_func;
724 glib_print_func = func;
725 g_mutex_unlock (g_messages_lock);
727 return old_print_func;
731 g_print (const gchar *format,
736 GPrintFunc local_glib_print_func;
738 g_return_if_fail (format != NULL);
740 va_start (args, format);
741 string = g_strdup_vprintf (format, args);
744 g_mutex_lock (g_messages_lock);
745 local_glib_print_func = glib_print_func;
746 g_mutex_unlock (g_messages_lock);
748 if (local_glib_print_func)
749 local_glib_print_func (string);
752 ensure_stdout_valid ();
753 fputs (string, stdout);
760 g_set_printerr_handler (GPrintFunc func)
762 GPrintFunc old_printerr_func;
764 g_mutex_lock (g_messages_lock);
765 old_printerr_func = glib_printerr_func;
766 glib_printerr_func = func;
767 g_mutex_unlock (g_messages_lock);
769 return old_printerr_func;
773 g_printerr (const gchar *format,
778 GPrintFunc local_glib_printerr_func;
780 g_return_if_fail (format != NULL);
782 va_start (args, format);
783 string = g_strdup_vprintf (format, args);
786 g_mutex_lock (g_messages_lock);
787 local_glib_printerr_func = glib_printerr_func;
788 g_mutex_unlock (g_messages_lock);
790 if (local_glib_printerr_func)
791 local_glib_printerr_func (string);
794 fputs (string, stderr);
800 /* compatibility code */
802 g_set_error_handler (GErrorFunc func)
804 GErrorFunc old_error_func;
806 g_mutex_lock (g_messages_lock);
807 old_error_func = glib_error_func;
808 glib_error_func = func;
809 g_mutex_unlock (g_messages_lock);
811 return old_error_func;
814 /* compatibility code */
816 g_set_warning_handler (GWarningFunc func)
818 GWarningFunc old_warning_func;
820 g_mutex_lock (g_messages_lock);
821 old_warning_func = glib_warning_func;
822 glib_warning_func = func;
823 g_mutex_unlock (g_messages_lock);
825 return old_warning_func;
828 /* compatibility code */
830 g_set_message_handler (GPrintFunc func)
832 GPrintFunc old_message_func;
834 g_mutex_lock (g_messages_lock);
835 old_message_func = glib_message_func;
836 glib_message_func = func;
837 g_mutex_unlock (g_messages_lock);
839 return old_message_func;
843 # define MB_LEN_MAX 8
850 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
851 gboolean add_space, add_sign, possible_sign, seen_precision;
852 gboolean mod_half, mod_long, mod_extra_long;
856 printf_string_upper_bound (const gchar *format,
860 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
868 register gchar c = *format++;
872 else /* (c == '%') */
874 PrintfArgSpec spec = { 0, };
875 gboolean seen_l = FALSE, conv_done = FALSE;
877 const gchar *spec_start = format;
884 GDoubleIEEE754 u_double;
887 const gchar *v_string;
889 /* beware of positional parameters
893 g_warning (G_GNUC_PRETTY_FUNCTION
894 "(): unable to handle positional parameters (%%n$)");
895 len += 1024; /* try adding some safety padding */
901 spec.alternate_format = TRUE;
904 spec.zero_padding = TRUE;
907 spec.adjust_left = TRUE;
910 spec.add_space = TRUE;
913 spec.add_sign = TRUE;
916 spec.locale_grouping = TRUE;
919 /* parse output size specifications
922 spec.seen_precision = TRUE;
935 while (c >= '0' && c <= '9')
938 v_uint = v_uint * 10 + c - '0';
941 if (spec.seen_precision)
942 spec.precision = MAX (spec.precision, v_uint);
944 spec.min_width = MAX (spec.min_width, v_uint);
947 v_int = va_arg (args, int);
948 if (spec.seen_precision)
950 /* forget about negative precision */
952 spec.precision = MAX (spec.precision, v_int);
959 spec.adjust_left = TRUE;
961 spec.min_width = MAX (spec.min_width, v_int);
965 /* parse type modifiers
968 spec.mod_half = TRUE;
973 spec.mod_long = TRUE;
977 /* else, fall through */
980 spec.mod_long = TRUE;
981 spec.mod_extra_long = TRUE;
985 #if GLIB_SIZEOF_SIZE_T > 4
986 spec.mod_long = TRUE;
987 spec.mod_extra_long = TRUE;
988 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
991 #if GLIB_SIZEOF_PTRDIFF_T > 4
992 spec.mod_long = TRUE;
993 spec.mod_extra_long = TRUE;
994 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
997 #if GLIB_SIZEOF_INTMAX_T > 4
998 spec.mod_long = TRUE;
999 spec.mod_extra_long = TRUE;
1000 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1003 /* parse output conversions
1012 /* some C libraries feature long variants for these as well? */
1013 spec.mod_long = TRUE;
1020 conv_len += 1; /* sign */
1027 spec.possible_sign = TRUE;
1029 if (spec.mod_long && honour_longs)
1031 if (spec.mod_extra_long)
1033 if (spec.mod_extra_long)
1035 #ifdef G_HAVE_GINT64
1036 (void) va_arg (args, gint64);
1037 #else /* !G_HAVE_GINT64 */
1038 (void) va_arg (args, long);
1039 #endif /* !G_HAVE_GINT64 */
1041 else if (spec.mod_long)
1042 (void) va_arg (args, long);
1044 (void) va_arg (args, int);
1056 spec.possible_sign = TRUE;
1057 /* n . dddddddddddddddddddddddd E +- eeee */
1058 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1059 if (may_warn && spec.mod_extra_long)
1060 g_warning (G_GNUC_PRETTY_FUNCTION
1061 "(): unable to handle long double, collecting double only");
1062 #ifdef HAVE_LONG_DOUBLE
1063 #error need to implement special handling for long double
1065 u_double.v_double = va_arg (args, double);
1066 /* %f can expand up to all significant digits before '.' (308) */
1068 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1070 gint exp = u_double.mpn.biased_exponent;
1072 exp -= G_IEEE754_DOUBLE_BIAS;
1073 exp = exp * G_LOG_2_BASE_10 + 1;
1076 /* some printf() implementations require extra padding for rounding */
1078 /* we can't really handle locale specific grouping here */
1079 if (spec.locale_grouping)
1083 spec.mod_long = TRUE;
1086 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1087 (void) va_arg (args, int);
1090 spec.mod_long = TRUE;
1093 v_string = va_arg (args, char*);
1095 conv_len += 8; /* hold "(null)" */
1096 else if (spec.seen_precision)
1097 conv_len += spec.precision;
1099 conv_len += strlen (v_string);
1104 g_warning (G_GNUC_PRETTY_FUNCTION
1105 "(): unable to handle wide char strings");
1106 len += 1024; /* try adding some safety padding */
1109 case 'P': /* do we actually need this? */
1112 spec.alternate_format = TRUE;
1119 (void) va_arg (args, void*);
1122 /* there's not much we can do to be clever */
1123 v_string = g_strerror (errno);
1124 v_uint = v_string ? strlen (v_string) : 0;
1125 conv_len += MAX (256, v_uint);
1128 /* handle invalid cases
1131 /* no conversion specification, bad bad */
1132 conv_len += format - spec_start;
1136 g_warning (G_GNUC_PRETTY_FUNCTION
1137 "(): unable to handle `%c' while parsing format",
1141 conv_done |= conv_len > 0;
1144 /* handle width specifications */
1145 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1147 conv_len += spec.alternate_format ? 2 : 0;
1148 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1151 } /* else (c == '%') */
1152 } /* while (*format) */
1158 g_printf_string_upper_bound (const gchar *format,
1161 return printf_string_upper_bound (format, TRUE, args);
1165 g_messages_init (void)
1167 g_messages_lock = g_mutex_new();
1168 g_log_depth = g_private_new(NULL);