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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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() */
95 /* Just use stdio. If we're out of memory, we're hosed anyway. */
102 fwrite (buf, len, 1, fd);
107 ensure_stdout_valid (void)
111 handle = GetStdHandle (STD_OUTPUT_HANDLE);
113 if (handle == INVALID_HANDLE_VALUE)
116 freopen ("CONOUT$", "w", stdout);
120 #define ensure_stdout_valid() /* Define as empty */
123 static inline GLogDomain*
124 g_log_find_domain (const gchar *log_domain)
126 register GLogDomain *domain;
128 g_mutex_lock (g_messages_lock);
129 domain = g_log_domains;
132 if (strcmp (domain->log_domain, log_domain) == 0)
134 g_mutex_unlock (g_messages_lock);
137 domain = domain->next;
139 g_mutex_unlock (g_messages_lock);
143 static inline GLogDomain*
144 g_log_domain_new (const gchar *log_domain)
146 register GLogDomain *domain;
148 domain = g_new (GLogDomain, 1);
149 domain->log_domain = g_strdup (log_domain);
150 domain->fatal_mask = G_LOG_FATAL_MASK;
151 domain->handlers = NULL;
153 g_mutex_lock (g_messages_lock);
154 domain->next = g_log_domains;
155 g_log_domains = domain;
156 g_mutex_unlock (g_messages_lock);
162 g_log_domain_check_free (GLogDomain *domain)
164 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
165 domain->handlers == NULL)
167 register GLogDomain *last, *work;
171 g_mutex_lock (g_messages_lock);
172 work = g_log_domains;
178 last->next = domain->next;
180 g_log_domains = domain->next;
181 g_free (domain->log_domain);
187 g_mutex_unlock (g_messages_lock);
191 static inline GLogFunc
192 g_log_domain_get_handler (GLogDomain *domain,
193 GLogLevelFlags log_level,
196 if (domain && log_level)
198 register GLogHandler *handler;
200 handler = domain->handlers;
203 if ((handler->log_level & log_level) == log_level)
205 *data = handler->data;
206 return handler->log_func;
208 handler = handler->next;
211 return g_log_default_handler;
215 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
217 GLogLevelFlags old_mask;
219 /* restrict the global mask to levels that are known to glib */
220 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
221 /* force errors to be fatal */
222 fatal_mask |= G_LOG_LEVEL_ERROR;
223 /* remove bogus flag */
224 fatal_mask &= ~G_LOG_FLAG_FATAL;
226 g_mutex_lock (g_messages_lock);
227 old_mask = g_log_always_fatal;
228 g_log_always_fatal = fatal_mask;
229 g_mutex_unlock (g_messages_lock);
235 g_log_set_fatal_mask (const gchar *log_domain,
236 GLogLevelFlags fatal_mask)
238 GLogLevelFlags old_flags;
239 register GLogDomain *domain;
244 /* force errors to be fatal */
245 fatal_mask |= G_LOG_LEVEL_ERROR;
246 /* remove bogus flag */
247 fatal_mask &= ~G_LOG_FLAG_FATAL;
249 domain = g_log_find_domain (log_domain);
251 domain = g_log_domain_new (log_domain);
252 old_flags = domain->fatal_mask;
254 domain->fatal_mask = fatal_mask;
255 g_log_domain_check_free (domain);
261 g_log_set_handler (const gchar *log_domain,
262 GLogLevelFlags log_levels,
266 register GLogDomain *domain;
267 register GLogHandler *handler;
268 static guint handler_id = 0;
270 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
271 g_return_val_if_fail (log_func != NULL, 0);
276 domain = g_log_find_domain (log_domain);
278 domain = g_log_domain_new (log_domain);
280 handler = g_new (GLogHandler, 1);
281 g_mutex_lock (g_messages_lock);
282 handler->id = ++handler_id;
283 g_mutex_unlock (g_messages_lock);
284 handler->log_level = log_levels;
285 handler->log_func = log_func;
286 handler->data = user_data;
287 handler->next = domain->handlers;
288 domain->handlers = handler;
294 g_log_remove_handler (const gchar *log_domain,
297 register GLogDomain *domain;
299 g_return_if_fail (handler_id > 0);
304 domain = g_log_find_domain (log_domain);
307 register GLogHandler *work, *last;
310 work = domain->handlers;
313 if (work->id == handler_id)
316 last->next = work->next;
318 domain->handlers = work->next;
320 g_log_domain_check_free (domain);
326 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
332 g_logv (const gchar *log_domain,
333 GLogLevelFlags log_level,
341 log_level &= G_LOG_LEVEL_MASK;
345 /* we use a stack buffer of fixed size, because we might get called
348 G_VA_COPY (args2, args1);
349 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
350 vsprintf (buffer, format, args2);
353 /* since we might be out of memory, we can't use g_vsnprintf(). */
354 #ifdef HAVE_VSNPRINTF
355 vsnprintf (buffer, 1024, format, args2);
356 #else /* !HAVE_VSNPRINTF */
357 /* we are out of luck here */
358 strncpy (buffer, format, 1024);
359 #endif /* !HAVE_VSNPRINTF */
364 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
366 register GLogLevelFlags test_level;
369 if (log_level & test_level)
371 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
374 gpointer data = NULL;
376 domain = g_log_find_domain (log_domain ? log_domain : "");
379 test_level |= G_LOG_FLAG_RECURSION;
382 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
384 g_mutex_lock (g_messages_lock);
385 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
386 g_log_always_fatal) & test_level) != 0)
387 test_level |= G_LOG_FLAG_FATAL;
388 g_mutex_unlock (g_messages_lock);
390 log_func = g_log_domain_get_handler (domain, test_level, &data);
391 log_func (log_domain, test_level, buffer, data);
393 /* *domain can be cluttered now */
395 if (test_level & G_LOG_FLAG_FATAL)
397 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
398 if (!(test_level & G_LOG_FLAG_RECURSION))
402 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
404 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
408 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
414 g_log (const gchar *log_domain,
415 GLogLevelFlags log_level,
421 va_start (args, format);
422 g_logv (log_domain, log_level, format, args);
427 g_log_default_handler (const gchar *log_domain,
428 GLogLevelFlags log_level,
429 const gchar *message,
430 gpointer unused_data)
437 gboolean in_recursion;
439 GErrorFunc local_glib_error_func;
440 GWarningFunc local_glib_warning_func;
441 GPrintFunc local_glib_message_func;
442 gchar prg_pid[64], *prg_name = g_get_prgname ();
444 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
445 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
446 log_level &= G_LOG_LEVEL_MASK;
449 message = "g_log_default_handler(): (NULL) message";
452 prg_name = "(process";
453 sprintf (prg_pid, ":%u): ", getpid ());
456 sprintf (prg_pid, " (pid:%u): ", getpid ());
459 /* Use just stdout as stderr is hard to get redirected from the
464 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
467 g_mutex_lock (g_messages_lock);
468 local_glib_error_func = glib_error_func;
469 local_glib_warning_func = glib_warning_func;
470 local_glib_message_func = glib_message_func;
471 g_mutex_unlock (g_messages_lock);
475 case G_LOG_LEVEL_ERROR:
476 if (!log_domain && local_glib_error_func)
478 /* compatibility code */
479 local_glib_error_func (message);
482 /* use write(2) for output, in case we are out of memeory */
483 ensure_stdout_valid ();
485 #ifdef G_ENABLE_MSG_PREFIX
486 write (fd, prg_name, strlen (prg_name));
487 write (fd, prg_pid, strlen (prg_pid));
488 #endif /* G_ENABLE_MSG_PREFIX */
491 write (fd, log_domain, strlen (log_domain));
495 write (fd, "** ", 3);
497 write (fd, "ERROR (recursed) **: ", 21);
499 write (fd, "ERROR **: ", 10);
500 write (fd, message, strlen (message));
502 write (fd, "\naborting...\n", 13);
506 case G_LOG_LEVEL_CRITICAL:
507 ensure_stdout_valid ();
509 #ifdef G_ENABLE_MSG_PREFIX
510 write (fd, prg_name, strlen (prg_name));
511 write (fd, prg_pid, strlen (prg_pid));
512 #endif /* G_ENABLE_MSG_PREFIX */
515 write (fd, log_domain, strlen (log_domain));
519 write (fd, "** ", 3);
521 write (fd, "CRITICAL (recursed) **: ", 24);
523 write (fd, "CRITICAL **: ", 13);
524 write (fd, message, strlen (message));
526 write (fd, "\naborting...\n", 13);
530 case G_LOG_LEVEL_WARNING:
531 if (!log_domain && local_glib_warning_func)
533 /* compatibility code */
534 local_glib_warning_func (message);
537 ensure_stdout_valid ();
539 #ifdef G_ENABLE_MSG_PREFIX
540 write (fd, prg_name, strlen (prg_name));
541 write (fd, prg_pid, strlen (prg_pid));
542 #endif /* G_ENABLE_MSG_PREFIX */
545 write (fd, log_domain, strlen (log_domain));
549 write (fd, "** ", 3);
551 write (fd, "WARNING (recursed) **: ", 23);
553 write (fd, "WARNING **: ", 12);
554 write (fd, message, strlen (message));
556 write (fd, "\naborting...\n", 13);
560 case G_LOG_LEVEL_MESSAGE:
561 if (!log_domain && local_glib_message_func)
563 /* compatibility code */
564 local_glib_message_func (message);
567 ensure_stdout_valid ();
568 #ifdef G_ENABLE_MSG_PREFIX
569 write (fd, prg_name, strlen (prg_name));
570 write (fd, prg_pid, strlen (prg_pid));
571 #endif /* G_ENABLE_MSG_PREFIX */
574 write (fd, log_domain, strlen (log_domain));
578 write (fd, "Message (recursed): ", 20);
580 write (fd, "Message: ", 9);
581 write (fd, message, strlen (message));
583 write (fd, "\naborting...\n", 13);
587 case G_LOG_LEVEL_INFO:
588 ensure_stdout_valid ();
589 #ifdef G_ENABLE_MSG_PREFIX
590 write (fd, prg_name, strlen (prg_name));
591 write (fd, prg_pid, strlen (prg_pid));
592 #endif /* G_ENABLE_MSG_PREFIX */
595 write (fd, log_domain, strlen (log_domain));
599 write (fd, "INFO (recursed): ", 17);
601 write (fd, "INFO: ", 6);
602 write (fd, message, strlen (message));
604 write (fd, "\naborting...\n", 13);
608 case G_LOG_LEVEL_DEBUG:
609 ensure_stdout_valid ();
610 #ifdef G_ENABLE_MSG_PREFIX
611 write (fd, prg_name, strlen (prg_name));
612 write (fd, prg_pid, strlen (prg_pid));
613 #endif /* G_ENABLE_MSG_PREFIX */
616 write (fd, log_domain, strlen (log_domain));
620 write (fd, "DEBUG (recursed): ", 18);
622 write (fd, "DEBUG: ", 7);
623 write (fd, message, strlen (message));
625 write (fd, "\naborting...\n", 13);
630 /* we are used for a log level that is not defined by GLib itself,
631 * try to make the best out of it.
633 ensure_stdout_valid ();
634 #ifdef G_ENABLE_MSG_PREFIX
635 write (fd, prg_name, strlen (prg_name));
636 write (fd, prg_pid, strlen (prg_pid));
637 #endif /* G_ENABLE_MSG_PREFIX */
640 write (fd, log_domain, strlen (log_domain));
642 write (fd, "-LOG (recursed:", 15);
644 write (fd, "-LOG (", 6);
646 else if (in_recursion)
647 write (fd, "LOG (recursed:", 14);
649 write (fd, "LOG (", 5);
652 gchar string[] = "0x00): ";
653 gchar *p = string + 2;
656 i = g_bit_nth_msf (log_level, -1);
659 *p = '0' + (i & 0xf);
663 write (fd, string, 7);
666 write (fd, "): ", 3);
667 write (fd, message, strlen (message));
669 write (fd, "\naborting...\n", 13);
677 g_set_print_handler (GPrintFunc func)
679 GPrintFunc old_print_func;
681 g_mutex_lock (g_messages_lock);
682 old_print_func = glib_print_func;
683 glib_print_func = func;
684 g_mutex_unlock (g_messages_lock);
686 return old_print_func;
690 g_print (const gchar *format,
695 GPrintFunc local_glib_print_func;
697 g_return_if_fail (format != NULL);
699 va_start (args, format);
700 string = g_strdup_vprintf (format, args);
703 g_mutex_lock (g_messages_lock);
704 local_glib_print_func = glib_print_func;
705 g_mutex_unlock (g_messages_lock);
707 if (local_glib_print_func)
708 local_glib_print_func (string);
711 ensure_stdout_valid ();
712 fputs (string, stdout);
719 g_set_printerr_handler (GPrintFunc func)
721 GPrintFunc old_printerr_func;
723 g_mutex_lock (g_messages_lock);
724 old_printerr_func = glib_printerr_func;
725 glib_printerr_func = func;
726 g_mutex_unlock (g_messages_lock);
728 return old_printerr_func;
732 g_printerr (const gchar *format,
737 GPrintFunc local_glib_printerr_func;
739 g_return_if_fail (format != NULL);
741 va_start (args, format);
742 string = g_strdup_vprintf (format, args);
745 g_mutex_lock (g_messages_lock);
746 local_glib_printerr_func = glib_printerr_func;
747 g_mutex_unlock (g_messages_lock);
749 if (local_glib_printerr_func)
750 local_glib_printerr_func (string);
753 fputs (string, stderr);
759 /* compatibility code */
761 g_set_error_handler (GErrorFunc func)
763 GErrorFunc old_error_func;
765 g_mutex_lock (g_messages_lock);
766 old_error_func = glib_error_func;
767 glib_error_func = func;
768 g_mutex_unlock (g_messages_lock);
770 return old_error_func;
773 /* compatibility code */
775 g_set_warning_handler (GWarningFunc func)
777 GWarningFunc old_warning_func;
779 g_mutex_lock (g_messages_lock);
780 old_warning_func = glib_warning_func;
781 glib_warning_func = func;
782 g_mutex_unlock (g_messages_lock);
784 return old_warning_func;
787 /* compatibility code */
789 g_set_message_handler (GPrintFunc func)
791 GPrintFunc old_message_func;
793 g_mutex_lock (g_messages_lock);
794 old_message_func = glib_message_func;
795 glib_message_func = func;
796 g_mutex_unlock (g_messages_lock);
798 return old_message_func;
802 # define MB_LEN_MAX 8
809 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
810 gboolean add_space, add_sign, possible_sign, seen_precision;
811 gboolean mod_half, mod_long, mod_extra_long;
815 printf_string_upper_bound (const gchar *format,
819 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
827 register gchar c = *format++;
831 else /* (c == '%') */
833 PrintfArgSpec spec = { 0, };
834 gboolean seen_l = FALSE, conv_done = FALSE;
836 const gchar *spec_start = format;
843 GDoubleIEEE754 u_double;
848 /* beware of positional parameters
852 g_warning (G_GNUC_PRETTY_FUNCTION
853 "(): unable to handle positional parameters (%%n$)");
854 len += 1024; /* try adding some safety padding */
860 spec.alternate_format = TRUE;
863 spec.zero_padding = TRUE;
866 spec.adjust_left = TRUE;
869 spec.add_space = TRUE;
872 spec.add_sign = TRUE;
875 spec.locale_grouping = TRUE;
878 /* parse output size specifications
881 spec.seen_precision = TRUE;
894 while (c >= '0' && c <= '9')
897 v_uint = v_uint * 10 + c - '0';
900 if (spec.seen_precision)
901 spec.precision = MAX (spec.precision, v_uint);
903 spec.min_width = MAX (spec.min_width, v_uint);
906 v_int = va_arg (args, int);
907 if (spec.seen_precision)
909 /* forget about negative precision */
911 spec.precision = MAX (spec.precision, v_int);
918 spec.adjust_left = TRUE;
920 spec.min_width = MAX (spec.min_width, v_int);
924 /* parse type modifiers
927 spec.mod_half = TRUE;
932 spec.mod_long = TRUE;
936 /* else, fall through */
939 spec.mod_long = TRUE;
940 spec.mod_extra_long = TRUE;
944 #if GLIB_SIZEOF_SIZE_T > 4
945 spec.mod_long = TRUE;
946 spec.mod_extra_long = TRUE;
947 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
950 #if GLIB_SIZEOF_PTRDIFF_T > 4
951 spec.mod_long = TRUE;
952 spec.mod_extra_long = TRUE;
953 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
956 #if GLIB_SIZEOF_INTMAX_T > 4
957 spec.mod_long = TRUE;
958 spec.mod_extra_long = TRUE;
959 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
962 /* parse output conversions
971 /* some C libraries feature long variants for these as well? */
972 spec.mod_long = TRUE;
979 conv_len += 1; /* sign */
986 spec.possible_sign = TRUE;
988 if (spec.mod_long && honour_longs)
990 if (spec.mod_extra_long)
992 if (spec.mod_extra_long)
995 (void) va_arg (args, gint64);
996 #else /* !G_HAVE_GINT64 */
997 (void) va_arg (args, long);
998 #endif /* !G_HAVE_GINT64 */
1000 else if (spec.mod_long)
1001 (void) va_arg (args, long);
1003 (void) va_arg (args, int);
1015 spec.possible_sign = TRUE;
1016 /* n . dddddddddddddddddddddddd E +- eeee */
1017 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1018 if (may_warn && spec.mod_extra_long)
1019 g_warning (G_GNUC_PRETTY_FUNCTION
1020 "(): unable to handle long double, collecting double only");
1021 #ifdef HAVE_LONG_DOUBLE
1022 #error need to implement special handling for long double
1024 u_double.v_double = va_arg (args, double);
1025 /* %f can expand up to all significant digits before '.' (308) */
1027 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1029 gint exp = u_double.mpn.biased_exponent;
1031 exp -= G_IEEE754_DOUBLE_BIAS;
1032 exp = exp * G_LOG_2_BASE_10 + 1;
1035 /* some printf() implementations require extra padding for rounding */
1037 /* we can't really handle locale specific grouping here */
1038 if (spec.locale_grouping)
1042 spec.mod_long = TRUE;
1045 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1046 (void) va_arg (args, int);
1049 spec.mod_long = TRUE;
1052 v_string = va_arg (args, char*);
1054 conv_len += 8; /* hold "(null)" */
1055 else if (spec.seen_precision)
1056 conv_len += spec.precision;
1058 conv_len += strlen (v_string);
1063 g_warning (G_GNUC_PRETTY_FUNCTION
1064 "(): unable to handle wide char strings");
1065 len += 1024; /* try adding some safety padding */
1068 case 'P': /* do we actually need this? */
1071 spec.alternate_format = TRUE;
1078 (void) va_arg (args, void*);
1081 /* there's not much we can do to be clever */
1082 v_string = g_strerror (errno);
1083 v_uint = v_string ? strlen (v_string) : 0;
1084 conv_len += MAX (256, v_uint);
1087 /* handle invalid cases
1090 /* no conversion specification, bad bad */
1091 conv_len += format - spec_start;
1095 g_warning (G_GNUC_PRETTY_FUNCTION
1096 "(): unable to handle `%c' while parsing format",
1100 conv_done |= conv_len > 0;
1103 /* handle width specifications */
1104 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1106 conv_len += spec.alternate_format ? 2 : 0;
1107 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1110 } /* else (c == '%') */
1111 } /* while (*format) */
1117 g_printf_string_upper_bound (const gchar *format,
1120 return printf_string_upper_bound (format, TRUE, args);
1124 g_messages_init (void)
1126 g_messages_lock = g_mutex_new();
1127 g_log_depth = g_private_new(NULL);