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);
196 g_mutex_unlock (g_messages_lock);
200 static inline GLogFunc
201 g_log_domain_get_handler (GLogDomain *domain,
202 GLogLevelFlags log_level,
205 if (domain && log_level)
207 register GLogHandler *handler;
209 handler = domain->handlers;
212 if ((handler->log_level & log_level) == log_level)
214 *data = handler->data;
215 return handler->log_func;
217 handler = handler->next;
220 return g_log_default_handler;
224 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
226 GLogLevelFlags old_mask;
228 /* restrict the global mask to levels that are known to glib */
229 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
230 /* force errors to be fatal */
231 fatal_mask |= G_LOG_LEVEL_ERROR;
232 /* remove bogus flag */
233 fatal_mask &= ~G_LOG_FLAG_FATAL;
235 g_mutex_lock (g_messages_lock);
236 old_mask = g_log_always_fatal;
237 g_log_always_fatal = fatal_mask;
238 g_mutex_unlock (g_messages_lock);
244 g_log_set_fatal_mask (const gchar *log_domain,
245 GLogLevelFlags fatal_mask)
247 GLogLevelFlags old_flags;
248 register GLogDomain *domain;
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 domain = g_log_find_domain (log_domain);
260 domain = g_log_domain_new (log_domain);
261 old_flags = domain->fatal_mask;
263 domain->fatal_mask = fatal_mask;
264 g_log_domain_check_free (domain);
270 g_log_set_handler (const gchar *log_domain,
271 GLogLevelFlags log_levels,
275 register GLogDomain *domain;
276 register GLogHandler *handler;
277 static guint handler_id = 0;
279 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
280 g_return_val_if_fail (log_func != NULL, 0);
285 domain = g_log_find_domain (log_domain);
287 domain = g_log_domain_new (log_domain);
289 handler = g_new (GLogHandler, 1);
290 g_mutex_lock (g_messages_lock);
291 handler->id = ++handler_id;
292 g_mutex_unlock (g_messages_lock);
293 handler->log_level = log_levels;
294 handler->log_func = log_func;
295 handler->data = user_data;
296 handler->next = domain->handlers;
297 domain->handlers = handler;
303 g_log_remove_handler (const gchar *log_domain,
306 register GLogDomain *domain;
308 g_return_if_fail (handler_id > 0);
313 domain = g_log_find_domain (log_domain);
316 register GLogHandler *work, *last;
319 work = domain->handlers;
322 if (work->id == handler_id)
325 last->next = work->next;
327 domain->handlers = work->next;
329 g_log_domain_check_free (domain);
336 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
342 g_logv (const gchar *log_domain,
343 GLogLevelFlags log_level,
351 log_level &= G_LOG_LEVEL_MASK;
355 /* we use a stack buffer of fixed size, because we might get called
358 G_VA_COPY (args2, args1);
359 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
360 vsprintf (buffer, format, args2);
363 /* since we might be out of memory, we can't use g_vsnprintf(). */
364 #ifdef HAVE_VSNPRINTF
365 vsnprintf (buffer, 1024, format, args2);
366 #else /* !HAVE_VSNPRINTF */
367 /* we are out of luck here */
368 strncpy (buffer, format, 1024);
369 #endif /* !HAVE_VSNPRINTF */
374 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
376 register GLogLevelFlags test_level;
379 if (log_level & test_level)
381 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
384 gpointer data = NULL;
386 domain = g_log_find_domain (log_domain ? log_domain : "");
389 test_level |= G_LOG_FLAG_RECURSION;
392 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
394 g_mutex_lock (g_messages_lock);
395 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
396 g_log_always_fatal) & test_level) != 0)
397 test_level |= G_LOG_FLAG_FATAL;
398 g_mutex_unlock (g_messages_lock);
400 log_func = g_log_domain_get_handler (domain, test_level, &data);
401 log_func (log_domain, test_level, buffer, data);
403 /* *domain can be cluttered now */
405 if (test_level & G_LOG_FLAG_FATAL)
407 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
408 if (!(test_level & G_LOG_FLAG_RECURSION))
412 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
414 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
418 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
424 g_log (const gchar *log_domain,
425 GLogLevelFlags log_level,
431 va_start (args, format);
432 g_logv (log_domain, log_level, format, args);
437 g_log_default_handler (const gchar *log_domain,
438 GLogLevelFlags log_level,
439 const gchar *message,
440 gpointer unused_data)
447 gboolean in_recursion;
449 GErrorFunc local_glib_error_func;
450 GWarningFunc local_glib_warning_func;
451 GPrintFunc local_glib_message_func;
452 gchar prg_pid[64], *prg_name = g_get_prgname ();
454 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
455 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
456 log_level &= G_LOG_LEVEL_MASK;
459 message = "g_log_default_handler(): (NULL) message";
462 prg_name = "(process";
463 sprintf (prg_pid, ":%u): ", getpid ());
466 sprintf (prg_pid, " (pid:%u): ", getpid ());
469 /* Use just stdout as stderr is hard to get redirected from the
474 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
477 g_mutex_lock (g_messages_lock);
478 local_glib_error_func = glib_error_func;
479 local_glib_warning_func = glib_warning_func;
480 local_glib_message_func = glib_message_func;
481 g_mutex_unlock (g_messages_lock);
485 case G_LOG_LEVEL_ERROR:
486 if (!log_domain && local_glib_error_func)
488 /* compatibility code */
489 local_glib_error_func (message);
492 /* use write(2) for output, in case we are out of memeory */
493 ensure_stdout_valid ();
495 #ifdef G_ENABLE_MSG_PREFIX
496 write (fd, prg_name, strlen (prg_name));
497 write (fd, prg_pid, strlen (prg_pid));
498 #endif /* G_ENABLE_MSG_PREFIX */
501 write (fd, log_domain, strlen (log_domain));
505 write (fd, "** ", 3);
507 write (fd, "ERROR (recursed) **: ", 21);
509 write (fd, "ERROR **: ", 10);
510 write (fd, message, strlen (message));
512 write (fd, "\naborting...\n", 13);
516 case G_LOG_LEVEL_CRITICAL:
517 ensure_stdout_valid ();
519 #ifdef G_ENABLE_MSG_PREFIX
520 write (fd, prg_name, strlen (prg_name));
521 write (fd, prg_pid, strlen (prg_pid));
522 #endif /* G_ENABLE_MSG_PREFIX */
525 write (fd, log_domain, strlen (log_domain));
529 write (fd, "** ", 3);
531 write (fd, "CRITICAL (recursed) **: ", 24);
533 write (fd, "CRITICAL **: ", 13);
534 write (fd, message, strlen (message));
536 write (fd, "\naborting...\n", 13);
540 case G_LOG_LEVEL_WARNING:
541 if (!log_domain && local_glib_warning_func)
543 /* compatibility code */
544 local_glib_warning_func (message);
547 ensure_stdout_valid ();
549 #ifdef G_ENABLE_MSG_PREFIX
550 write (fd, prg_name, strlen (prg_name));
551 write (fd, prg_pid, strlen (prg_pid));
552 #endif /* G_ENABLE_MSG_PREFIX */
555 write (fd, log_domain, strlen (log_domain));
559 write (fd, "** ", 3);
561 write (fd, "WARNING (recursed) **: ", 23);
563 write (fd, "WARNING **: ", 12);
564 write (fd, message, strlen (message));
566 write (fd, "\naborting...\n", 13);
570 case G_LOG_LEVEL_MESSAGE:
571 if (!log_domain && local_glib_message_func)
573 /* compatibility code */
574 local_glib_message_func (message);
577 ensure_stdout_valid ();
578 #ifdef G_ENABLE_MSG_PREFIX
579 write (fd, prg_name, strlen (prg_name));
580 write (fd, prg_pid, strlen (prg_pid));
581 #endif /* G_ENABLE_MSG_PREFIX */
584 write (fd, log_domain, strlen (log_domain));
588 write (fd, "Message (recursed): ", 20);
590 write (fd, "Message: ", 9);
591 write (fd, message, strlen (message));
593 write (fd, "\naborting...\n", 13);
597 case G_LOG_LEVEL_INFO:
598 ensure_stdout_valid ();
599 #ifdef G_ENABLE_MSG_PREFIX
600 write (fd, prg_name, strlen (prg_name));
601 write (fd, prg_pid, strlen (prg_pid));
602 #endif /* G_ENABLE_MSG_PREFIX */
605 write (fd, log_domain, strlen (log_domain));
609 write (fd, "INFO (recursed): ", 17);
611 write (fd, "INFO: ", 6);
612 write (fd, message, strlen (message));
614 write (fd, "\naborting...\n", 13);
618 case G_LOG_LEVEL_DEBUG:
619 ensure_stdout_valid ();
620 #ifdef G_ENABLE_MSG_PREFIX
621 write (fd, prg_name, strlen (prg_name));
622 write (fd, prg_pid, strlen (prg_pid));
623 #endif /* G_ENABLE_MSG_PREFIX */
626 write (fd, log_domain, strlen (log_domain));
630 write (fd, "DEBUG (recursed): ", 18);
632 write (fd, "DEBUG: ", 7);
633 write (fd, message, strlen (message));
635 write (fd, "\naborting...\n", 13);
640 /* we are used for a log level that is not defined by GLib itself,
641 * try to make the best out of it.
643 ensure_stdout_valid ();
644 #ifdef G_ENABLE_MSG_PREFIX
645 write (fd, prg_name, strlen (prg_name));
646 write (fd, prg_pid, strlen (prg_pid));
647 #endif /* G_ENABLE_MSG_PREFIX */
650 write (fd, log_domain, strlen (log_domain));
652 write (fd, "-LOG (recursed:", 15);
654 write (fd, "-LOG (", 6);
656 else if (in_recursion)
657 write (fd, "LOG (recursed:", 14);
659 write (fd, "LOG (", 5);
662 gchar string[] = "0x00): ";
663 gchar *p = string + 2;
666 i = g_bit_nth_msf (log_level, -1);
669 *p = '0' + (i & 0xf);
673 write (fd, string, 7);
676 write (fd, "): ", 3);
677 write (fd, message, strlen (message));
679 write (fd, "\naborting...\n", 13);
687 g_set_print_handler (GPrintFunc func)
689 GPrintFunc old_print_func;
691 g_mutex_lock (g_messages_lock);
692 old_print_func = glib_print_func;
693 glib_print_func = func;
694 g_mutex_unlock (g_messages_lock);
696 return old_print_func;
700 g_print (const gchar *format,
705 GPrintFunc local_glib_print_func;
707 g_return_if_fail (format != NULL);
709 va_start (args, format);
710 string = g_strdup_vprintf (format, args);
713 g_mutex_lock (g_messages_lock);
714 local_glib_print_func = glib_print_func;
715 g_mutex_unlock (g_messages_lock);
717 if (local_glib_print_func)
718 local_glib_print_func (string);
721 ensure_stdout_valid ();
722 fputs (string, stdout);
729 g_set_printerr_handler (GPrintFunc func)
731 GPrintFunc old_printerr_func;
733 g_mutex_lock (g_messages_lock);
734 old_printerr_func = glib_printerr_func;
735 glib_printerr_func = func;
736 g_mutex_unlock (g_messages_lock);
738 return old_printerr_func;
742 g_printerr (const gchar *format,
747 GPrintFunc local_glib_printerr_func;
749 g_return_if_fail (format != NULL);
751 va_start (args, format);
752 string = g_strdup_vprintf (format, args);
755 g_mutex_lock (g_messages_lock);
756 local_glib_printerr_func = glib_printerr_func;
757 g_mutex_unlock (g_messages_lock);
759 if (local_glib_printerr_func)
760 local_glib_printerr_func (string);
763 fputs (string, stderr);
769 /* compatibility code */
771 g_set_error_handler (GErrorFunc func)
773 GErrorFunc old_error_func;
775 g_mutex_lock (g_messages_lock);
776 old_error_func = glib_error_func;
777 glib_error_func = func;
778 g_mutex_unlock (g_messages_lock);
780 return old_error_func;
783 /* compatibility code */
785 g_set_warning_handler (GWarningFunc func)
787 GWarningFunc old_warning_func;
789 g_mutex_lock (g_messages_lock);
790 old_warning_func = glib_warning_func;
791 glib_warning_func = func;
792 g_mutex_unlock (g_messages_lock);
794 return old_warning_func;
797 /* compatibility code */
799 g_set_message_handler (GPrintFunc func)
801 GPrintFunc old_message_func;
803 g_mutex_lock (g_messages_lock);
804 old_message_func = glib_message_func;
805 glib_message_func = func;
806 g_mutex_unlock (g_messages_lock);
808 return old_message_func;
812 # define MB_LEN_MAX 8
819 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
820 gboolean add_space, add_sign, possible_sign, seen_precision;
821 gboolean mod_half, mod_long, mod_extra_long;
825 printf_string_upper_bound (const gchar *format,
829 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
837 register gchar c = *format++;
841 else /* (c == '%') */
843 PrintfArgSpec spec = { 0, };
844 gboolean seen_l = FALSE, conv_done = FALSE;
846 const gchar *spec_start = format;
853 GDoubleIEEE754 u_double;
858 /* beware of positional parameters
862 g_warning (G_GNUC_PRETTY_FUNCTION
863 "(): unable to handle positional parameters (%%n$)");
864 len += 1024; /* try adding some safety padding */
870 spec.alternate_format = TRUE;
873 spec.zero_padding = TRUE;
876 spec.adjust_left = TRUE;
879 spec.add_space = TRUE;
882 spec.add_sign = TRUE;
885 spec.locale_grouping = TRUE;
888 /* parse output size specifications
891 spec.seen_precision = TRUE;
904 while (c >= '0' && c <= '9')
907 v_uint = v_uint * 10 + c - '0';
910 if (spec.seen_precision)
911 spec.precision = MAX (spec.precision, v_uint);
913 spec.min_width = MAX (spec.min_width, v_uint);
916 v_int = va_arg (args, int);
917 if (spec.seen_precision)
919 /* forget about negative precision */
921 spec.precision = MAX (spec.precision, v_int);
928 spec.adjust_left = TRUE;
930 spec.min_width = MAX (spec.min_width, v_int);
934 /* parse type modifiers
937 spec.mod_half = TRUE;
942 spec.mod_long = TRUE;
946 /* else, fall through */
949 spec.mod_long = TRUE;
950 spec.mod_extra_long = TRUE;
954 #if GLIB_SIZEOF_SIZE_T > 4
955 spec.mod_long = TRUE;
956 spec.mod_extra_long = TRUE;
957 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
960 #if GLIB_SIZEOF_PTRDIFF_T > 4
961 spec.mod_long = TRUE;
962 spec.mod_extra_long = TRUE;
963 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
966 #if GLIB_SIZEOF_INTMAX_T > 4
967 spec.mod_long = TRUE;
968 spec.mod_extra_long = TRUE;
969 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
972 /* parse output conversions
981 /* some C libraries feature long variants for these as well? */
982 spec.mod_long = TRUE;
989 conv_len += 1; /* sign */
996 spec.possible_sign = TRUE;
998 if (spec.mod_long && honour_longs)
1000 if (spec.mod_extra_long)
1002 if (spec.mod_extra_long)
1004 #ifdef G_HAVE_GINT64
1005 (void) va_arg (args, gint64);
1006 #else /* !G_HAVE_GINT64 */
1007 (void) va_arg (args, long);
1008 #endif /* !G_HAVE_GINT64 */
1010 else if (spec.mod_long)
1011 (void) va_arg (args, long);
1013 (void) va_arg (args, int);
1025 spec.possible_sign = TRUE;
1026 /* n . dddddddddddddddddddddddd E +- eeee */
1027 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1028 if (may_warn && spec.mod_extra_long)
1029 g_warning (G_GNUC_PRETTY_FUNCTION
1030 "(): unable to handle long double, collecting double only");
1031 #ifdef HAVE_LONG_DOUBLE
1032 #error need to implement special handling for long double
1034 u_double.v_double = va_arg (args, double);
1035 /* %f can expand up to all significant digits before '.' (308) */
1037 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1039 gint exp = u_double.mpn.biased_exponent;
1041 exp -= G_IEEE754_DOUBLE_BIAS;
1042 exp = exp * G_LOG_2_BASE_10 + 1;
1045 /* some printf() implementations require extra padding for rounding */
1047 /* we can't really handle locale specific grouping here */
1048 if (spec.locale_grouping)
1052 spec.mod_long = TRUE;
1055 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1056 (void) va_arg (args, int);
1059 spec.mod_long = TRUE;
1062 v_string = va_arg (args, char*);
1064 conv_len += 8; /* hold "(null)" */
1065 else if (spec.seen_precision)
1066 conv_len += spec.precision;
1068 conv_len += strlen (v_string);
1073 g_warning (G_GNUC_PRETTY_FUNCTION
1074 "(): unable to handle wide char strings");
1075 len += 1024; /* try adding some safety padding */
1078 case 'P': /* do we actually need this? */
1081 spec.alternate_format = TRUE;
1088 (void) va_arg (args, void*);
1091 /* there's not much we can do to be clever */
1092 v_string = g_strerror (errno);
1093 v_uint = v_string ? strlen (v_string) : 0;
1094 conv_len += MAX (256, v_uint);
1097 /* handle invalid cases
1100 /* no conversion specification, bad bad */
1101 conv_len += format - spec_start;
1105 g_warning (G_GNUC_PRETTY_FUNCTION
1106 "(): unable to handle `%c' while parsing format",
1110 conv_done |= conv_len > 0;
1113 /* handle width specifications */
1114 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1116 conv_len += spec.alternate_format ? 2 : 0;
1117 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1120 } /* else (c == '%') */
1121 } /* while (*format) */
1127 g_printf_string_upper_bound (const gchar *format,
1130 return printf_string_upper_bound (format, TRUE, args);
1134 g_messages_init (void)
1136 g_messages_lock = g_mutex_new();
1137 g_log_depth = g_private_new(NULL);