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 --- */
94 # include <process.h> /* For _getpid() */
96 static gboolean alloc_console_called = FALSE;
98 /* Just use stdio. If we're out of memory, we're hosed anyway. */
105 fwrite (buf, len, 1, fd);
111 #define write(fd, buf, len) dowrite(fd, buf, len)
114 ensure_stdout_valid (void)
118 if (!alloc_console_called)
120 handle = GetStdHandle (STD_OUTPUT_HANDLE);
122 if (handle == INVALID_HANDLE_VALUE)
125 alloc_console_called = TRUE;
126 freopen ("CONOUT$", "w", stdout);
131 #define ensure_stdout_valid() /* Define as empty */
134 static inline GLogDomain*
135 g_log_find_domain (const gchar *log_domain)
137 register GLogDomain *domain;
139 g_mutex_lock (g_messages_lock);
140 domain = g_log_domains;
143 if (strcmp (domain->log_domain, log_domain) == 0)
145 g_mutex_unlock (g_messages_lock);
148 domain = domain->next;
150 g_mutex_unlock (g_messages_lock);
154 static inline GLogDomain*
155 g_log_domain_new (const gchar *log_domain)
157 register GLogDomain *domain;
159 domain = g_new (GLogDomain, 1);
160 domain->log_domain = g_strdup (log_domain);
161 domain->fatal_mask = G_LOG_FATAL_MASK;
162 domain->handlers = NULL;
164 g_mutex_lock (g_messages_lock);
165 domain->next = g_log_domains;
166 g_log_domains = domain;
167 g_mutex_unlock (g_messages_lock);
173 g_log_domain_check_free (GLogDomain *domain)
175 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
176 domain->handlers == NULL)
178 register GLogDomain *last, *work;
182 g_mutex_lock (g_messages_lock);
183 work = g_log_domains;
189 last->next = domain->next;
191 g_log_domains = domain->next;
192 g_free (domain->log_domain);
199 g_mutex_unlock (g_messages_lock);
203 static inline GLogFunc
204 g_log_domain_get_handler (GLogDomain *domain,
205 GLogLevelFlags log_level,
208 if (domain && log_level)
210 register GLogHandler *handler;
212 handler = domain->handlers;
215 if ((handler->log_level & log_level) == log_level)
217 *data = handler->data;
218 return handler->log_func;
220 handler = handler->next;
223 return g_log_default_handler;
227 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
229 GLogLevelFlags old_mask;
231 /* restrict the global mask to levels that are known to glib */
232 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
233 /* force errors to be fatal */
234 fatal_mask |= G_LOG_LEVEL_ERROR;
235 /* remove bogus flag */
236 fatal_mask &= ~G_LOG_FLAG_FATAL;
238 g_mutex_lock (g_messages_lock);
239 old_mask = g_log_always_fatal;
240 g_log_always_fatal = fatal_mask;
241 g_mutex_unlock (g_messages_lock);
247 g_log_set_fatal_mask (const gchar *log_domain,
248 GLogLevelFlags fatal_mask)
250 GLogLevelFlags old_flags;
251 register GLogDomain *domain;
256 /* force errors to be fatal */
257 fatal_mask |= G_LOG_LEVEL_ERROR;
258 /* remove bogus flag */
259 fatal_mask &= ~G_LOG_FLAG_FATAL;
261 domain = g_log_find_domain (log_domain);
263 domain = g_log_domain_new (log_domain);
264 old_flags = domain->fatal_mask;
266 domain->fatal_mask = fatal_mask;
267 g_log_domain_check_free (domain);
273 g_log_set_handler (const gchar *log_domain,
274 GLogLevelFlags log_levels,
278 register GLogDomain *domain;
279 register GLogHandler *handler;
280 static guint handler_id = 0;
282 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
283 g_return_val_if_fail (log_func != NULL, 0);
288 domain = g_log_find_domain (log_domain);
290 domain = g_log_domain_new (log_domain);
292 handler = g_new (GLogHandler, 1);
293 g_mutex_lock (g_messages_lock);
294 handler->id = ++handler_id;
295 g_mutex_unlock (g_messages_lock);
296 handler->log_level = log_levels;
297 handler->log_func = log_func;
298 handler->data = user_data;
299 handler->next = domain->handlers;
300 domain->handlers = handler;
306 g_log_remove_handler (const gchar *log_domain,
309 register GLogDomain *domain;
311 g_return_if_fail (handler_id > 0);
316 domain = g_log_find_domain (log_domain);
319 register GLogHandler *work, *last;
322 work = domain->handlers;
325 if (work->id == handler_id)
328 last->next = work->next;
330 domain->handlers = work->next;
332 g_log_domain_check_free (domain);
339 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
345 g_logv (const gchar *log_domain,
346 GLogLevelFlags log_level,
354 log_level &= G_LOG_LEVEL_MASK;
358 /* we use a stack buffer of fixed size, because we might get called
361 G_VA_COPY (args2, args1);
362 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
363 vsprintf (buffer, format, args2);
366 /* since we might be out of memory, we can't use g_vsnprintf(). */
367 #ifdef HAVE_VSNPRINTF
368 vsnprintf (buffer, 1024, format, args2);
369 #else /* !HAVE_VSNPRINTF */
370 /* we are out of luck here */
371 strncpy (buffer, format, 1024);
372 #endif /* !HAVE_VSNPRINTF */
377 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
379 register GLogLevelFlags test_level;
382 if (log_level & test_level)
384 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
387 gpointer data = NULL;
389 domain = g_log_find_domain (log_domain ? log_domain : "");
392 test_level |= G_LOG_FLAG_RECURSION;
395 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
397 g_mutex_lock (g_messages_lock);
398 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
399 g_log_always_fatal) & test_level) != 0)
400 test_level |= G_LOG_FLAG_FATAL;
401 g_mutex_unlock (g_messages_lock);
403 log_func = g_log_domain_get_handler (domain, test_level, &data);
404 log_func (log_domain, test_level, buffer, data);
406 /* *domain can be cluttered now */
408 if (test_level & G_LOG_FLAG_FATAL)
410 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
411 if (!(test_level & G_LOG_FLAG_RECURSION))
415 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
417 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
421 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
427 g_log (const gchar *log_domain,
428 GLogLevelFlags log_level,
434 va_start (args, format);
435 g_logv (log_domain, log_level, format, args);
440 g_log_default_handler (const gchar *log_domain,
441 GLogLevelFlags log_level,
442 const gchar *message,
443 gpointer unused_data)
450 gboolean in_recursion;
452 GErrorFunc local_glib_error_func;
453 GWarningFunc local_glib_warning_func;
454 GPrintFunc local_glib_message_func;
455 gchar prg_pid[64], *prg_name = g_get_prgname ();
457 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
458 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
459 log_level &= G_LOG_LEVEL_MASK;
462 message = "g_log_default_handler(): (NULL) message";
465 prg_name = "(process";
466 sprintf (prg_pid, ":%u): ", getpid ());
469 sprintf (prg_pid, " (pid:%u): ", getpid ());
472 /* Use just stdout as stderr is hard to get redirected from the
477 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
480 g_mutex_lock (g_messages_lock);
481 local_glib_error_func = glib_error_func;
482 local_glib_warning_func = glib_warning_func;
483 local_glib_message_func = glib_message_func;
484 g_mutex_unlock (g_messages_lock);
488 case G_LOG_LEVEL_ERROR:
489 if (!log_domain && local_glib_error_func)
491 /* compatibility code */
492 local_glib_error_func (message);
495 /* use write(2) for output, in case we are out of memeory */
496 ensure_stdout_valid ();
498 #ifdef G_ENABLE_MSG_PREFIX
499 write (fd, prg_name, strlen (prg_name));
500 write (fd, prg_pid, strlen (prg_pid));
501 #endif /* G_ENABLE_MSG_PREFIX */
504 write (fd, log_domain, strlen (log_domain));
508 write (fd, "** ", 3);
510 write (fd, "ERROR (recursed) **: ", 21);
512 write (fd, "ERROR **: ", 10);
513 write (fd, message, strlen (message));
515 write (fd, "\naborting...\n", 13);
519 case G_LOG_LEVEL_CRITICAL:
520 ensure_stdout_valid ();
522 #ifdef G_ENABLE_MSG_PREFIX
523 write (fd, prg_name, strlen (prg_name));
524 write (fd, prg_pid, strlen (prg_pid));
525 #endif /* G_ENABLE_MSG_PREFIX */
528 write (fd, log_domain, strlen (log_domain));
532 write (fd, "** ", 3);
534 write (fd, "CRITICAL (recursed) **: ", 24);
536 write (fd, "CRITICAL **: ", 13);
537 write (fd, message, strlen (message));
539 write (fd, "\naborting...\n", 13);
543 case G_LOG_LEVEL_WARNING:
544 if (!log_domain && local_glib_warning_func)
546 /* compatibility code */
547 local_glib_warning_func (message);
550 ensure_stdout_valid ();
552 #ifdef G_ENABLE_MSG_PREFIX
553 write (fd, prg_name, strlen (prg_name));
554 write (fd, prg_pid, strlen (prg_pid));
555 #endif /* G_ENABLE_MSG_PREFIX */
558 write (fd, log_domain, strlen (log_domain));
562 write (fd, "** ", 3);
564 write (fd, "WARNING (recursed) **: ", 23);
566 write (fd, "WARNING **: ", 12);
567 write (fd, message, strlen (message));
569 write (fd, "\naborting...\n", 13);
573 case G_LOG_LEVEL_MESSAGE:
574 if (!log_domain && local_glib_message_func)
576 /* compatibility code */
577 local_glib_message_func (message);
580 ensure_stdout_valid ();
581 #ifdef G_ENABLE_MSG_PREFIX
582 write (fd, prg_name, strlen (prg_name));
583 write (fd, prg_pid, strlen (prg_pid));
584 #endif /* G_ENABLE_MSG_PREFIX */
587 write (fd, log_domain, strlen (log_domain));
591 write (fd, "Message (recursed): ", 20);
593 write (fd, "Message: ", 9);
594 write (fd, message, strlen (message));
596 write (fd, "\naborting...\n", 13);
600 case G_LOG_LEVEL_INFO:
601 ensure_stdout_valid ();
602 #ifdef G_ENABLE_MSG_PREFIX
603 write (fd, prg_name, strlen (prg_name));
604 write (fd, prg_pid, strlen (prg_pid));
605 #endif /* G_ENABLE_MSG_PREFIX */
608 write (fd, log_domain, strlen (log_domain));
612 write (fd, "INFO (recursed): ", 17);
614 write (fd, "INFO: ", 6);
615 write (fd, message, strlen (message));
617 write (fd, "\naborting...\n", 13);
621 case G_LOG_LEVEL_DEBUG:
622 ensure_stdout_valid ();
623 #ifdef G_ENABLE_MSG_PREFIX
624 write (fd, prg_name, strlen (prg_name));
625 write (fd, prg_pid, strlen (prg_pid));
626 #endif /* G_ENABLE_MSG_PREFIX */
629 write (fd, log_domain, strlen (log_domain));
633 write (fd, "DEBUG (recursed): ", 18);
635 write (fd, "DEBUG: ", 7);
636 write (fd, message, strlen (message));
638 write (fd, "\naborting...\n", 13);
643 /* we are used for a log level that is not defined by GLib itself,
644 * try to make the best out of it.
646 ensure_stdout_valid ();
647 #ifdef G_ENABLE_MSG_PREFIX
648 write (fd, prg_name, strlen (prg_name));
649 write (fd, prg_pid, strlen (prg_pid));
650 #endif /* G_ENABLE_MSG_PREFIX */
653 write (fd, log_domain, strlen (log_domain));
655 write (fd, "-LOG (recursed:", 15);
657 write (fd, "-LOG (", 6);
659 else if (in_recursion)
660 write (fd, "LOG (recursed:", 14);
662 write (fd, "LOG (", 5);
665 gchar string[] = "0x00): ";
666 gchar *p = string + 2;
669 i = g_bit_nth_msf (log_level, -1);
672 *p = '0' + (i & 0xf);
676 write (fd, string, 7);
679 write (fd, "): ", 3);
680 write (fd, message, strlen (message));
682 write (fd, "\naborting...\n", 13);
690 g_set_print_handler (GPrintFunc func)
692 GPrintFunc old_print_func;
694 g_mutex_lock (g_messages_lock);
695 old_print_func = glib_print_func;
696 glib_print_func = func;
697 g_mutex_unlock (g_messages_lock);
699 return old_print_func;
703 g_print (const gchar *format,
708 GPrintFunc local_glib_print_func;
710 g_return_if_fail (format != NULL);
712 va_start (args, format);
713 string = g_strdup_vprintf (format, args);
716 g_mutex_lock (g_messages_lock);
717 local_glib_print_func = glib_print_func;
718 g_mutex_unlock (g_messages_lock);
720 if (local_glib_print_func)
721 local_glib_print_func (string);
724 ensure_stdout_valid ();
725 fputs (string, stdout);
732 g_set_printerr_handler (GPrintFunc func)
734 GPrintFunc old_printerr_func;
736 g_mutex_lock (g_messages_lock);
737 old_printerr_func = glib_printerr_func;
738 glib_printerr_func = func;
739 g_mutex_unlock (g_messages_lock);
741 return old_printerr_func;
745 g_printerr (const gchar *format,
750 GPrintFunc local_glib_printerr_func;
752 g_return_if_fail (format != NULL);
754 va_start (args, format);
755 string = g_strdup_vprintf (format, args);
758 g_mutex_lock (g_messages_lock);
759 local_glib_printerr_func = glib_printerr_func;
760 g_mutex_unlock (g_messages_lock);
762 if (local_glib_printerr_func)
763 local_glib_printerr_func (string);
766 fputs (string, stderr);
772 /* compatibility code */
774 g_set_error_handler (GErrorFunc func)
776 GErrorFunc old_error_func;
778 g_mutex_lock (g_messages_lock);
779 old_error_func = glib_error_func;
780 glib_error_func = func;
781 g_mutex_unlock (g_messages_lock);
783 return old_error_func;
786 /* compatibility code */
788 g_set_warning_handler (GWarningFunc func)
790 GWarningFunc old_warning_func;
792 g_mutex_lock (g_messages_lock);
793 old_warning_func = glib_warning_func;
794 glib_warning_func = func;
795 g_mutex_unlock (g_messages_lock);
797 return old_warning_func;
800 /* compatibility code */
802 g_set_message_handler (GPrintFunc func)
804 GPrintFunc old_message_func;
806 g_mutex_lock (g_messages_lock);
807 old_message_func = glib_message_func;
808 glib_message_func = func;
809 g_mutex_unlock (g_messages_lock);
811 return old_message_func;
815 # define MB_LEN_MAX 8
822 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
823 gboolean add_space, add_sign, possible_sign, seen_precision;
824 gboolean mod_half, mod_long, mod_extra_long;
828 printf_string_upper_bound (const gchar *format,
832 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
840 register gchar c = *format++;
844 else /* (c == '%') */
846 PrintfArgSpec spec = { 0, };
847 gboolean seen_l = FALSE, conv_done = FALSE;
849 const gchar *spec_start = format;
856 GDoubleIEEE754 u_double;
861 /* beware of positional parameters
865 g_warning (G_GNUC_PRETTY_FUNCTION
866 "(): unable to handle positional parameters (%%n$)");
867 len += 1024; /* try adding some safety padding */
873 spec.alternate_format = TRUE;
876 spec.zero_padding = TRUE;
879 spec.adjust_left = TRUE;
882 spec.add_space = TRUE;
885 spec.add_sign = TRUE;
888 spec.locale_grouping = TRUE;
891 /* parse output size specifications
894 spec.seen_precision = TRUE;
907 while (c >= '0' && c <= '9')
910 v_uint = v_uint * 10 + c - '0';
913 if (spec.seen_precision)
914 spec.precision = MAX (spec.precision, v_uint);
916 spec.min_width = MAX (spec.min_width, v_uint);
919 v_int = va_arg (args, int);
920 if (spec.seen_precision)
922 /* forget about negative precision */
924 spec.precision = MAX (spec.precision, v_int);
931 spec.adjust_left = TRUE;
933 spec.min_width = MAX (spec.min_width, v_int);
937 /* parse type modifiers
940 spec.mod_half = TRUE;
945 spec.mod_long = TRUE;
949 /* else, fall through */
952 spec.mod_long = TRUE;
953 spec.mod_extra_long = TRUE;
957 #if GLIB_SIZEOF_SIZE_T > 4
958 spec.mod_long = TRUE;
959 spec.mod_extra_long = TRUE;
960 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
963 #if GLIB_SIZEOF_PTRDIFF_T > 4
964 spec.mod_long = TRUE;
965 spec.mod_extra_long = TRUE;
966 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
969 #if GLIB_SIZEOF_INTMAX_T > 4
970 spec.mod_long = TRUE;
971 spec.mod_extra_long = TRUE;
972 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
975 /* parse output conversions
984 /* some C libraries feature long variants for these as well? */
985 spec.mod_long = TRUE;
992 conv_len += 1; /* sign */
999 spec.possible_sign = TRUE;
1001 if (spec.mod_long && honour_longs)
1003 if (spec.mod_extra_long)
1005 if (spec.mod_extra_long)
1007 #ifdef G_HAVE_GINT64
1008 (void) va_arg (args, gint64);
1009 #else /* !G_HAVE_GINT64 */
1010 (void) va_arg (args, long);
1011 #endif /* !G_HAVE_GINT64 */
1013 else if (spec.mod_long)
1014 (void) va_arg (args, long);
1016 (void) va_arg (args, int);
1028 spec.possible_sign = TRUE;
1029 /* n . dddddddddddddddddddddddd E +- eeee */
1030 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1031 if (may_warn && spec.mod_extra_long)
1032 g_warning (G_GNUC_PRETTY_FUNCTION
1033 "(): unable to handle long double, collecting double only");
1034 #ifdef HAVE_LONG_DOUBLE
1035 #error need to implement special handling for long double
1037 u_double.v_double = va_arg (args, double);
1038 /* %f can expand up to all significant digits before '.' (308) */
1040 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1042 gint exp = u_double.mpn.biased_exponent;
1044 exp -= G_IEEE754_DOUBLE_BIAS;
1045 exp = exp * G_LOG_2_BASE_10 + 1;
1048 /* some printf() implementations require extra padding for rounding */
1050 /* we can't really handle locale specific grouping here */
1051 if (spec.locale_grouping)
1055 spec.mod_long = TRUE;
1058 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1059 (void) va_arg (args, int);
1062 spec.mod_long = TRUE;
1065 v_string = va_arg (args, char*);
1067 conv_len += 8; /* hold "(null)" */
1068 else if (spec.seen_precision)
1069 conv_len += spec.precision;
1071 conv_len += strlen (v_string);
1076 g_warning (G_GNUC_PRETTY_FUNCTION
1077 "(): unable to handle wide char strings");
1078 len += 1024; /* try adding some safety padding */
1081 case 'P': /* do we actually need this? */
1084 spec.alternate_format = TRUE;
1091 (void) va_arg (args, void*);
1094 /* there's not much we can do to be clever */
1095 v_string = g_strerror (errno);
1096 v_uint = v_string ? strlen (v_string) : 0;
1097 conv_len += MAX (256, v_uint);
1100 /* handle invalid cases
1103 /* no conversion specification, bad bad */
1104 conv_len += format - spec_start;
1108 g_warning (G_GNUC_PRETTY_FUNCTION
1109 "(): unable to handle `%c' while parsing format",
1113 conv_done |= conv_len > 0;
1116 /* handle width specifications */
1117 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1119 conv_len += spec.alternate_format ? 2 : 0;
1120 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1123 } /* else (c == '%') */
1124 } /* while (*format) */
1130 g_printf_string_upper_bound (const gchar *format,
1133 return printf_string_upper_bound (format, TRUE, args);
1137 g_messages_init (void)
1139 g_messages_lock = g_mutex_new();
1140 g_log_depth = g_private_new(NULL);