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 ensure_stdout_valid (void)
115 if (!alloc_console_called)
117 handle = GetStdHandle (STD_OUTPUT_HANDLE);
119 if (handle == INVALID_HANDLE_VALUE)
122 alloc_console_called = TRUE;
123 freopen ("CONOUT$", "w", stdout);
128 #define ensure_stdout_valid() /* Define as empty */
131 static inline GLogDomain*
132 g_log_find_domain (const gchar *log_domain)
134 register GLogDomain *domain;
136 g_mutex_lock (g_messages_lock);
137 domain = g_log_domains;
140 if (strcmp (domain->log_domain, log_domain) == 0)
142 g_mutex_unlock (g_messages_lock);
145 domain = domain->next;
147 g_mutex_unlock (g_messages_lock);
151 static inline GLogDomain*
152 g_log_domain_new (const gchar *log_domain)
154 register GLogDomain *domain;
156 domain = g_new (GLogDomain, 1);
157 domain->log_domain = g_strdup (log_domain);
158 domain->fatal_mask = G_LOG_FATAL_MASK;
159 domain->handlers = NULL;
161 g_mutex_lock (g_messages_lock);
162 domain->next = g_log_domains;
163 g_log_domains = domain;
164 g_mutex_unlock (g_messages_lock);
170 g_log_domain_check_free (GLogDomain *domain)
172 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
173 domain->handlers == NULL)
175 register GLogDomain *last, *work;
179 g_mutex_lock (g_messages_lock);
180 work = g_log_domains;
186 last->next = domain->next;
188 g_log_domains = domain->next;
189 g_free (domain->log_domain);
195 g_mutex_unlock (g_messages_lock);
199 static inline GLogFunc
200 g_log_domain_get_handler (GLogDomain *domain,
201 GLogLevelFlags log_level,
204 if (domain && log_level)
206 register GLogHandler *handler;
208 handler = domain->handlers;
211 if ((handler->log_level & log_level) == log_level)
213 *data = handler->data;
214 return handler->log_func;
216 handler = handler->next;
219 return g_log_default_handler;
223 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
225 GLogLevelFlags old_mask;
227 /* restrict the global mask to levels that are known to glib */
228 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
229 /* force errors to be fatal */
230 fatal_mask |= G_LOG_LEVEL_ERROR;
231 /* remove bogus flag */
232 fatal_mask &= ~G_LOG_FLAG_FATAL;
234 g_mutex_lock (g_messages_lock);
235 old_mask = g_log_always_fatal;
236 g_log_always_fatal = fatal_mask;
237 g_mutex_unlock (g_messages_lock);
243 g_log_set_fatal_mask (const gchar *log_domain,
244 GLogLevelFlags fatal_mask)
246 GLogLevelFlags old_flags;
247 register GLogDomain *domain;
252 /* force errors to be fatal */
253 fatal_mask |= G_LOG_LEVEL_ERROR;
254 /* remove bogus flag */
255 fatal_mask &= ~G_LOG_FLAG_FATAL;
257 domain = g_log_find_domain (log_domain);
259 domain = g_log_domain_new (log_domain);
260 old_flags = domain->fatal_mask;
262 domain->fatal_mask = fatal_mask;
263 g_log_domain_check_free (domain);
269 g_log_set_handler (const gchar *log_domain,
270 GLogLevelFlags log_levels,
274 register GLogDomain *domain;
275 register GLogHandler *handler;
276 static guint handler_id = 0;
278 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
279 g_return_val_if_fail (log_func != NULL, 0);
284 domain = g_log_find_domain (log_domain);
286 domain = g_log_domain_new (log_domain);
288 handler = g_new (GLogHandler, 1);
289 g_mutex_lock (g_messages_lock);
290 handler->id = ++handler_id;
291 g_mutex_unlock (g_messages_lock);
292 handler->log_level = log_levels;
293 handler->log_func = log_func;
294 handler->data = user_data;
295 handler->next = domain->handlers;
296 domain->handlers = handler;
302 g_log_remove_handler (const gchar *log_domain,
305 register GLogDomain *domain;
307 g_return_if_fail (handler_id > 0);
312 domain = g_log_find_domain (log_domain);
315 register GLogHandler *work, *last;
318 work = domain->handlers;
321 if (work->id == handler_id)
324 last->next = work->next;
326 domain->handlers = work->next;
328 g_log_domain_check_free (domain);
334 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
340 g_logv (const gchar *log_domain,
341 GLogLevelFlags log_level,
349 log_level &= G_LOG_LEVEL_MASK;
353 /* we use a stack buffer of fixed size, because we might get called
356 G_VA_COPY (args2, args1);
357 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
358 vsprintf (buffer, format, args2);
361 /* since we might be out of memory, we can't use g_vsnprintf(). */
362 #ifdef HAVE_VSNPRINTF
363 vsnprintf (buffer, 1024, format, args2);
364 #else /* !HAVE_VSNPRINTF */
365 /* we are out of luck here */
366 strncpy (buffer, format, 1024);
367 #endif /* !HAVE_VSNPRINTF */
372 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
374 register GLogLevelFlags test_level;
377 if (log_level & test_level)
379 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
382 gpointer data = NULL;
384 domain = g_log_find_domain (log_domain ? log_domain : "");
387 test_level |= G_LOG_FLAG_RECURSION;
390 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
392 g_mutex_lock (g_messages_lock);
393 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
394 g_log_always_fatal) & test_level) != 0)
395 test_level |= G_LOG_FLAG_FATAL;
396 g_mutex_unlock (g_messages_lock);
398 log_func = g_log_domain_get_handler (domain, test_level, &data);
399 log_func (log_domain, test_level, buffer, data);
401 /* *domain can be cluttered now */
403 if (test_level & G_LOG_FLAG_FATAL)
405 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
406 if (!(test_level & G_LOG_FLAG_RECURSION))
410 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
412 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
416 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
422 g_log (const gchar *log_domain,
423 GLogLevelFlags log_level,
429 va_start (args, format);
430 g_logv (log_domain, log_level, format, args);
435 g_log_default_handler (const gchar *log_domain,
436 GLogLevelFlags log_level,
437 const gchar *message,
438 gpointer unused_data)
445 gboolean in_recursion;
447 GErrorFunc local_glib_error_func;
448 GWarningFunc local_glib_warning_func;
449 GPrintFunc local_glib_message_func;
450 gchar prg_pid[64], *prg_name = g_get_prgname ();
452 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
453 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
454 log_level &= G_LOG_LEVEL_MASK;
457 message = "g_log_default_handler(): (NULL) message";
460 prg_name = "(process";
461 sprintf (prg_pid, ":%u): ", getpid ());
464 sprintf (prg_pid, " (pid:%u): ", getpid ());
467 /* Use just stdout as stderr is hard to get redirected from the
472 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
475 g_mutex_lock (g_messages_lock);
476 local_glib_error_func = glib_error_func;
477 local_glib_warning_func = glib_warning_func;
478 local_glib_message_func = glib_message_func;
479 g_mutex_unlock (g_messages_lock);
483 case G_LOG_LEVEL_ERROR:
484 if (!log_domain && local_glib_error_func)
486 /* compatibility code */
487 local_glib_error_func (message);
490 /* use write(2) for output, in case we are out of memeory */
491 ensure_stdout_valid ();
493 #ifdef G_ENABLE_MSG_PREFIX
494 write (fd, prg_name, strlen (prg_name));
495 write (fd, prg_pid, strlen (prg_pid));
496 #endif /* G_ENABLE_MSG_PREFIX */
499 write (fd, log_domain, strlen (log_domain));
503 write (fd, "** ", 3);
505 write (fd, "ERROR (recursed) **: ", 21);
507 write (fd, "ERROR **: ", 10);
508 write (fd, message, strlen (message));
510 write (fd, "\naborting...\n", 13);
514 case G_LOG_LEVEL_CRITICAL:
515 ensure_stdout_valid ();
517 #ifdef G_ENABLE_MSG_PREFIX
518 write (fd, prg_name, strlen (prg_name));
519 write (fd, prg_pid, strlen (prg_pid));
520 #endif /* G_ENABLE_MSG_PREFIX */
523 write (fd, log_domain, strlen (log_domain));
527 write (fd, "** ", 3);
529 write (fd, "CRITICAL (recursed) **: ", 24);
531 write (fd, "CRITICAL **: ", 13);
532 write (fd, message, strlen (message));
534 write (fd, "\naborting...\n", 13);
538 case G_LOG_LEVEL_WARNING:
539 if (!log_domain && local_glib_warning_func)
541 /* compatibility code */
542 local_glib_warning_func (message);
545 ensure_stdout_valid ();
547 #ifdef G_ENABLE_MSG_PREFIX
548 write (fd, prg_name, strlen (prg_name));
549 write (fd, prg_pid, strlen (prg_pid));
550 #endif /* G_ENABLE_MSG_PREFIX */
553 write (fd, log_domain, strlen (log_domain));
557 write (fd, "** ", 3);
559 write (fd, "WARNING (recursed) **: ", 23);
561 write (fd, "WARNING **: ", 12);
562 write (fd, message, strlen (message));
564 write (fd, "\naborting...\n", 13);
568 case G_LOG_LEVEL_MESSAGE:
569 if (!log_domain && local_glib_message_func)
571 /* compatibility code */
572 local_glib_message_func (message);
575 ensure_stdout_valid ();
576 #ifdef G_ENABLE_MSG_PREFIX
577 write (fd, prg_name, strlen (prg_name));
578 write (fd, prg_pid, strlen (prg_pid));
579 #endif /* G_ENABLE_MSG_PREFIX */
582 write (fd, log_domain, strlen (log_domain));
586 write (fd, "Message (recursed): ", 20);
588 write (fd, "Message: ", 9);
589 write (fd, message, strlen (message));
591 write (fd, "\naborting...\n", 13);
595 case G_LOG_LEVEL_INFO:
596 ensure_stdout_valid ();
597 #ifdef G_ENABLE_MSG_PREFIX
598 write (fd, prg_name, strlen (prg_name));
599 write (fd, prg_pid, strlen (prg_pid));
600 #endif /* G_ENABLE_MSG_PREFIX */
603 write (fd, log_domain, strlen (log_domain));
607 write (fd, "INFO (recursed): ", 17);
609 write (fd, "INFO: ", 6);
610 write (fd, message, strlen (message));
612 write (fd, "\naborting...\n", 13);
616 case G_LOG_LEVEL_DEBUG:
617 ensure_stdout_valid ();
618 #ifdef G_ENABLE_MSG_PREFIX
619 write (fd, prg_name, strlen (prg_name));
620 write (fd, prg_pid, strlen (prg_pid));
621 #endif /* G_ENABLE_MSG_PREFIX */
624 write (fd, log_domain, strlen (log_domain));
628 write (fd, "DEBUG (recursed): ", 18);
630 write (fd, "DEBUG: ", 7);
631 write (fd, message, strlen (message));
633 write (fd, "\naborting...\n", 13);
638 /* we are used for a log level that is not defined by GLib itself,
639 * try to make the best out of it.
641 ensure_stdout_valid ();
642 #ifdef G_ENABLE_MSG_PREFIX
643 write (fd, prg_name, strlen (prg_name));
644 write (fd, prg_pid, strlen (prg_pid));
645 #endif /* G_ENABLE_MSG_PREFIX */
648 write (fd, log_domain, strlen (log_domain));
650 write (fd, "-LOG (recursed:", 15);
652 write (fd, "-LOG (", 6);
654 else if (in_recursion)
655 write (fd, "LOG (recursed:", 14);
657 write (fd, "LOG (", 5);
660 gchar string[] = "0x00): ";
661 gchar *p = string + 2;
664 i = g_bit_nth_msf (log_level, -1);
667 *p = '0' + (i & 0xf);
671 write (fd, string, 7);
674 write (fd, "): ", 3);
675 write (fd, message, strlen (message));
677 write (fd, "\naborting...\n", 13);
685 g_set_print_handler (GPrintFunc func)
687 GPrintFunc old_print_func;
689 g_mutex_lock (g_messages_lock);
690 old_print_func = glib_print_func;
691 glib_print_func = func;
692 g_mutex_unlock (g_messages_lock);
694 return old_print_func;
698 g_print (const gchar *format,
703 GPrintFunc local_glib_print_func;
705 g_return_if_fail (format != NULL);
707 va_start (args, format);
708 string = g_strdup_vprintf (format, args);
711 g_mutex_lock (g_messages_lock);
712 local_glib_print_func = glib_print_func;
713 g_mutex_unlock (g_messages_lock);
715 if (local_glib_print_func)
716 local_glib_print_func (string);
719 ensure_stdout_valid ();
720 fputs (string, stdout);
727 g_set_printerr_handler (GPrintFunc func)
729 GPrintFunc old_printerr_func;
731 g_mutex_lock (g_messages_lock);
732 old_printerr_func = glib_printerr_func;
733 glib_printerr_func = func;
734 g_mutex_unlock (g_messages_lock);
736 return old_printerr_func;
740 g_printerr (const gchar *format,
745 GPrintFunc local_glib_printerr_func;
747 g_return_if_fail (format != NULL);
749 va_start (args, format);
750 string = g_strdup_vprintf (format, args);
753 g_mutex_lock (g_messages_lock);
754 local_glib_printerr_func = glib_printerr_func;
755 g_mutex_unlock (g_messages_lock);
757 if (local_glib_printerr_func)
758 local_glib_printerr_func (string);
761 fputs (string, stderr);
767 /* compatibility code */
769 g_set_error_handler (GErrorFunc func)
771 GErrorFunc old_error_func;
773 g_mutex_lock (g_messages_lock);
774 old_error_func = glib_error_func;
775 glib_error_func = func;
776 g_mutex_unlock (g_messages_lock);
778 return old_error_func;
781 /* compatibility code */
783 g_set_warning_handler (GWarningFunc func)
785 GWarningFunc old_warning_func;
787 g_mutex_lock (g_messages_lock);
788 old_warning_func = glib_warning_func;
789 glib_warning_func = func;
790 g_mutex_unlock (g_messages_lock);
792 return old_warning_func;
795 /* compatibility code */
797 g_set_message_handler (GPrintFunc func)
799 GPrintFunc old_message_func;
801 g_mutex_lock (g_messages_lock);
802 old_message_func = glib_message_func;
803 glib_message_func = func;
804 g_mutex_unlock (g_messages_lock);
806 return old_message_func;
810 # define MB_LEN_MAX 8
817 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
818 gboolean add_space, add_sign, possible_sign, seen_precision;
819 gboolean mod_half, mod_long, mod_extra_long;
823 printf_string_upper_bound (const gchar *format,
827 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
835 register gchar c = *format++;
839 else /* (c == '%') */
841 PrintfArgSpec spec = { 0, };
842 gboolean seen_l = FALSE, conv_done = FALSE;
844 const gchar *spec_start = format;
851 GDoubleIEEE754 u_double;
856 /* beware of positional parameters
860 g_warning (G_GNUC_PRETTY_FUNCTION
861 "(): unable to handle positional parameters (%%n$)");
862 len += 1024; /* try adding some safety padding */
868 spec.alternate_format = TRUE;
871 spec.zero_padding = TRUE;
874 spec.adjust_left = TRUE;
877 spec.add_space = TRUE;
880 spec.add_sign = TRUE;
883 spec.locale_grouping = TRUE;
886 /* parse output size specifications
889 spec.seen_precision = TRUE;
902 while (c >= '0' && c <= '9')
905 v_uint = v_uint * 10 + c - '0';
908 if (spec.seen_precision)
909 spec.precision = MAX (spec.precision, v_uint);
911 spec.min_width = MAX (spec.min_width, v_uint);
914 v_int = va_arg (args, int);
915 if (spec.seen_precision)
917 /* forget about negative precision */
919 spec.precision = MAX (spec.precision, v_int);
926 spec.adjust_left = TRUE;
928 spec.min_width = MAX (spec.min_width, v_int);
932 /* parse type modifiers
935 spec.mod_half = TRUE;
940 spec.mod_long = TRUE;
944 /* else, fall through */
947 spec.mod_long = TRUE;
948 spec.mod_extra_long = TRUE;
952 #if GLIB_SIZEOF_SIZE_T > 4
953 spec.mod_long = TRUE;
954 spec.mod_extra_long = TRUE;
955 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
958 #if GLIB_SIZEOF_PTRDIFF_T > 4
959 spec.mod_long = TRUE;
960 spec.mod_extra_long = TRUE;
961 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
964 #if GLIB_SIZEOF_INTMAX_T > 4
965 spec.mod_long = TRUE;
966 spec.mod_extra_long = TRUE;
967 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
970 /* parse output conversions
979 /* some C libraries feature long variants for these as well? */
980 spec.mod_long = TRUE;
987 conv_len += 1; /* sign */
994 spec.possible_sign = TRUE;
996 if (spec.mod_long && honour_longs)
998 if (spec.mod_extra_long)
1000 if (spec.mod_extra_long)
1002 #ifdef G_HAVE_GINT64
1003 (void) va_arg (args, gint64);
1004 #else /* !G_HAVE_GINT64 */
1005 (void) va_arg (args, long);
1006 #endif /* !G_HAVE_GINT64 */
1008 else if (spec.mod_long)
1009 (void) va_arg (args, long);
1011 (void) va_arg (args, int);
1023 spec.possible_sign = TRUE;
1024 /* n . dddddddddddddddddddddddd E +- eeee */
1025 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1026 if (may_warn && spec.mod_extra_long)
1027 g_warning (G_GNUC_PRETTY_FUNCTION
1028 "(): unable to handle long double, collecting double only");
1029 #ifdef HAVE_LONG_DOUBLE
1030 #error need to implement special handling for long double
1032 u_double.v_double = va_arg (args, double);
1033 /* %f can expand up to all significant digits before '.' (308) */
1035 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1037 gint exp = u_double.mpn.biased_exponent;
1039 exp -= G_IEEE754_DOUBLE_BIAS;
1040 exp = exp * G_LOG_2_BASE_10 + 1;
1043 /* some printf() implementations require extra padding for rounding */
1045 /* we can't really handle locale specific grouping here */
1046 if (spec.locale_grouping)
1050 spec.mod_long = TRUE;
1053 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1054 (void) va_arg (args, int);
1057 spec.mod_long = TRUE;
1060 v_string = va_arg (args, char*);
1062 conv_len += 8; /* hold "(null)" */
1063 else if (spec.seen_precision)
1064 conv_len += spec.precision;
1066 conv_len += strlen (v_string);
1071 g_warning (G_GNUC_PRETTY_FUNCTION
1072 "(): unable to handle wide char strings");
1073 len += 1024; /* try adding some safety padding */
1076 case 'P': /* do we actually need this? */
1079 spec.alternate_format = TRUE;
1086 (void) va_arg (args, void*);
1089 /* there's not much we can do to be clever */
1090 v_string = g_strerror (errno);
1091 v_uint = v_string ? strlen (v_string) : 0;
1092 conv_len += MAX (256, v_uint);
1095 /* handle invalid cases
1098 /* no conversion specification, bad bad */
1099 conv_len += format - spec_start;
1103 g_warning (G_GNUC_PRETTY_FUNCTION
1104 "(): unable to handle `%c' while parsing format",
1108 conv_done |= conv_len > 0;
1111 /* handle width specifications */
1112 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1114 conv_len += spec.alternate_format ? 2 : 0;
1115 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1118 } /* else (c == '%') */
1119 } /* while (*format) */
1125 g_printf_string_upper_bound (const gchar *format,
1128 return printf_string_upper_bound (format, TRUE, args);
1132 g_messages_init (void)
1134 g_messages_lock = g_mutex_new();
1135 g_log_depth = g_private_new(NULL);