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 static gboolean gonna_abort = FALSE;
100 /* This default message will usually be overwritten. */
101 /* Yes, a fixed size buffer is bad. So sue me. But g_error is never
102 * with huge strings, is it? */
103 static char fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
104 static char *fatal_msg_ptr = fatal_msg_buf;
106 /* Just use stdio. If we're out of memory, we're hosed anyway. */
115 memcpy (fatal_msg_ptr, buf, len);
116 fatal_msg_ptr += len;
121 fwrite (buf, len, 1, fd);
127 #define write(fd, buf, len) dowrite(fd, buf, len)
130 ensure_stdout_valid (void)
137 if (!alloc_console_called)
139 handle = GetStdHandle (STD_OUTPUT_HANDLE);
141 if (handle == INVALID_HANDLE_VALUE)
144 alloc_console_called = TRUE;
145 freopen ("CONOUT$", "w", stdout);
150 #define ensure_stdout_valid() /* Define as empty */
153 static inline GLogDomain*
154 g_log_find_domain (const gchar *log_domain)
156 register GLogDomain *domain;
158 g_mutex_lock (g_messages_lock);
159 domain = g_log_domains;
162 if (strcmp (domain->log_domain, log_domain) == 0)
164 g_mutex_unlock (g_messages_lock);
167 domain = domain->next;
169 g_mutex_unlock (g_messages_lock);
173 static inline GLogDomain*
174 g_log_domain_new (const gchar *log_domain)
176 register GLogDomain *domain;
178 domain = g_new (GLogDomain, 1);
179 domain->log_domain = g_strdup (log_domain);
180 domain->fatal_mask = G_LOG_FATAL_MASK;
181 domain->handlers = NULL;
183 g_mutex_lock (g_messages_lock);
184 domain->next = g_log_domains;
185 g_log_domains = domain;
186 g_mutex_unlock (g_messages_lock);
192 g_log_domain_check_free (GLogDomain *domain)
194 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
195 domain->handlers == NULL)
197 register GLogDomain *last, *work;
201 g_mutex_lock (g_messages_lock);
202 work = g_log_domains;
208 last->next = domain->next;
210 g_log_domains = domain->next;
211 g_free (domain->log_domain);
218 g_mutex_unlock (g_messages_lock);
222 static inline GLogFunc
223 g_log_domain_get_handler (GLogDomain *domain,
224 GLogLevelFlags log_level,
227 if (domain && log_level)
229 register GLogHandler *handler;
231 handler = domain->handlers;
234 if ((handler->log_level & log_level) == log_level)
236 *data = handler->data;
237 return handler->log_func;
239 handler = handler->next;
242 return g_log_default_handler;
246 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
248 GLogLevelFlags old_mask;
250 /* restrict the global mask to levels that are known to glib */
251 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
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 g_mutex_lock (g_messages_lock);
258 old_mask = g_log_always_fatal;
259 g_log_always_fatal = fatal_mask;
260 g_mutex_unlock (g_messages_lock);
266 g_log_set_fatal_mask (const gchar *log_domain,
267 GLogLevelFlags fatal_mask)
269 GLogLevelFlags old_flags;
270 register GLogDomain *domain;
275 /* force errors to be fatal */
276 fatal_mask |= G_LOG_LEVEL_ERROR;
277 /* remove bogus flag */
278 fatal_mask &= ~G_LOG_FLAG_FATAL;
280 domain = g_log_find_domain (log_domain);
282 domain = g_log_domain_new (log_domain);
283 old_flags = domain->fatal_mask;
285 domain->fatal_mask = fatal_mask;
286 g_log_domain_check_free (domain);
292 g_log_set_handler (const gchar *log_domain,
293 GLogLevelFlags log_levels,
297 register GLogDomain *domain;
298 register GLogHandler *handler;
299 static guint handler_id = 0;
301 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
302 g_return_val_if_fail (log_func != NULL, 0);
307 domain = g_log_find_domain (log_domain);
309 domain = g_log_domain_new (log_domain);
311 handler = g_new (GLogHandler, 1);
312 g_mutex_lock (g_messages_lock);
313 handler->id = ++handler_id;
314 g_mutex_unlock (g_messages_lock);
315 handler->log_level = log_levels;
316 handler->log_func = log_func;
317 handler->data = user_data;
318 handler->next = domain->handlers;
319 domain->handlers = handler;
325 g_log_remove_handler (const gchar *log_domain,
328 register GLogDomain *domain;
330 g_return_if_fail (handler_id > 0);
335 domain = g_log_find_domain (log_domain);
338 register GLogHandler *work, *last;
341 work = domain->handlers;
344 if (work->id == handler_id)
347 last->next = work->next;
349 domain->handlers = work->next;
351 g_log_domain_check_free (domain);
358 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
364 g_logv (const gchar *log_domain,
365 GLogLevelFlags log_level,
373 log_level &= G_LOG_LEVEL_MASK;
377 /* we use a stack buffer of fixed size, because we might get called
380 G_VA_COPY (args2, args1);
381 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
382 vsprintf (buffer, format, args2);
385 /* since we might be out of memory, we can't use g_vsnprintf(). */
386 #ifdef HAVE_VSNPRINTF
387 vsnprintf (buffer, 1024, format, args2);
388 #else /* !HAVE_VSNPRINTF */
389 /* we are out of luck here */
390 strncpy (buffer, format, 1024);
391 #endif /* !HAVE_VSNPRINTF */
396 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
398 register GLogLevelFlags test_level;
401 if (log_level & test_level)
403 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
406 gpointer data = NULL;
408 domain = g_log_find_domain (log_domain ? log_domain : "");
411 test_level |= G_LOG_FLAG_RECURSION;
414 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
416 g_mutex_lock (g_messages_lock);
417 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
418 g_log_always_fatal) & test_level) != 0)
419 test_level |= G_LOG_FLAG_FATAL;
420 g_mutex_unlock (g_messages_lock);
422 log_func = g_log_domain_get_handler (domain, test_level, &data);
423 log_func (log_domain, test_level, buffer, data);
425 /* *domain can be cluttered now */
427 if (test_level & G_LOG_FLAG_FATAL)
429 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
430 if (!(test_level & G_LOG_FLAG_RECURSION))
434 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
436 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
438 # if defined (_MSC_VER) && defined (_DEBUG)
439 /* let's see the call stack ... */
443 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
447 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
453 g_log (const gchar *log_domain,
454 GLogLevelFlags log_level,
460 va_start (args, format);
461 g_logv (log_domain, log_level, format, args);
466 g_log_default_handler (const gchar *log_domain,
467 GLogLevelFlags log_level,
468 const gchar *message,
469 gpointer unused_data)
476 gboolean in_recursion;
478 GErrorFunc local_glib_error_func;
479 GWarningFunc local_glib_warning_func;
480 GPrintFunc local_glib_message_func;
481 gchar prg_pid[64], *prg_name = g_get_prgname ();
483 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
484 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
485 log_level &= G_LOG_LEVEL_MASK;
488 message = "g_log_default_handler(): (NULL) message";
491 prg_name = "(process";
492 sprintf (prg_pid, ":%u): ", getpid ());
495 sprintf (prg_pid, " (pid:%u): ", getpid ());
498 /* Use just stdout as stderr is hard to get redirected from the
502 gonna_abort = is_fatal;
504 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
507 g_mutex_lock (g_messages_lock);
508 local_glib_error_func = glib_error_func;
509 local_glib_warning_func = glib_warning_func;
510 local_glib_message_func = glib_message_func;
511 g_mutex_unlock (g_messages_lock);
515 case G_LOG_LEVEL_ERROR:
516 if (!log_domain && local_glib_error_func)
518 /* compatibility code */
519 local_glib_error_func (message);
522 /* use write(2) for output, in case we are out of memeory */
523 ensure_stdout_valid ();
525 #ifdef G_ENABLE_MSG_PREFIX
526 write (fd, prg_name, strlen (prg_name));
527 write (fd, prg_pid, strlen (prg_pid));
528 #endif /* G_ENABLE_MSG_PREFIX */
531 write (fd, log_domain, strlen (log_domain));
535 write (fd, "** ", 3);
537 write (fd, "ERROR (recursed) **: ", 21);
539 write (fd, "ERROR **: ", 10);
540 write (fd, message, strlen (message));
542 write (fd, "\naborting...\n", 13);
546 case G_LOG_LEVEL_CRITICAL:
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, "CRITICAL (recursed) **: ", 24);
563 write (fd, "CRITICAL **: ", 13);
564 write (fd, message, strlen (message));
566 write (fd, "\naborting...\n", 13);
570 case G_LOG_LEVEL_WARNING:
571 if (!log_domain && local_glib_warning_func)
573 /* compatibility code */
574 local_glib_warning_func (message);
577 ensure_stdout_valid ();
579 #ifdef G_ENABLE_MSG_PREFIX
580 write (fd, prg_name, strlen (prg_name));
581 write (fd, prg_pid, strlen (prg_pid));
582 #endif /* G_ENABLE_MSG_PREFIX */
585 write (fd, log_domain, strlen (log_domain));
589 write (fd, "** ", 3);
591 write (fd, "WARNING (recursed) **: ", 23);
593 write (fd, "WARNING **: ", 12);
594 write (fd, message, strlen (message));
596 write (fd, "\naborting...\n", 13);
600 case G_LOG_LEVEL_MESSAGE:
601 if (!log_domain && local_glib_message_func)
603 /* compatibility code */
604 local_glib_message_func (message);
607 ensure_stdout_valid ();
608 #ifdef G_ENABLE_MSG_PREFIX
609 write (fd, prg_name, strlen (prg_name));
610 write (fd, prg_pid, strlen (prg_pid));
611 #endif /* G_ENABLE_MSG_PREFIX */
614 write (fd, log_domain, strlen (log_domain));
618 write (fd, "Message (recursed): ", 20);
620 write (fd, "Message: ", 9);
621 write (fd, message, strlen (message));
623 write (fd, "\naborting...\n", 13);
627 case G_LOG_LEVEL_INFO:
628 ensure_stdout_valid ();
629 #ifdef G_ENABLE_MSG_PREFIX
630 write (fd, prg_name, strlen (prg_name));
631 write (fd, prg_pid, strlen (prg_pid));
632 #endif /* G_ENABLE_MSG_PREFIX */
635 write (fd, log_domain, strlen (log_domain));
639 write (fd, "INFO (recursed): ", 17);
641 write (fd, "INFO: ", 6);
642 write (fd, message, strlen (message));
644 write (fd, "\naborting...\n", 13);
648 case G_LOG_LEVEL_DEBUG:
649 ensure_stdout_valid ();
650 #ifdef G_ENABLE_MSG_PREFIX
651 write (fd, prg_name, strlen (prg_name));
652 write (fd, prg_pid, strlen (prg_pid));
653 #endif /* G_ENABLE_MSG_PREFIX */
656 write (fd, log_domain, strlen (log_domain));
660 write (fd, "DEBUG (recursed): ", 18);
662 write (fd, "DEBUG: ", 7);
663 write (fd, message, strlen (message));
665 write (fd, "\naborting...\n", 13);
670 /* we are used for a log level that is not defined by GLib itself,
671 * try to make the best out of it.
673 ensure_stdout_valid ();
674 #ifdef G_ENABLE_MSG_PREFIX
675 write (fd, prg_name, strlen (prg_name));
676 write (fd, prg_pid, strlen (prg_pid));
677 #endif /* G_ENABLE_MSG_PREFIX */
680 write (fd, log_domain, strlen (log_domain));
682 write (fd, "-LOG (recursed:", 15);
684 write (fd, "-LOG (", 6);
686 else if (in_recursion)
687 write (fd, "LOG (recursed:", 14);
689 write (fd, "LOG (", 5);
692 gchar string[] = "0x00): ";
693 gchar *p = string + 2;
696 i = g_bit_nth_msf (log_level, -1);
699 *p = '0' + (i & 0xf);
703 write (fd, string, 7);
706 write (fd, "): ", 3);
707 write (fd, message, strlen (message));
709 write (fd, "\naborting...\n", 13);
717 g_set_print_handler (GPrintFunc func)
719 GPrintFunc old_print_func;
721 g_mutex_lock (g_messages_lock);
722 old_print_func = glib_print_func;
723 glib_print_func = func;
724 g_mutex_unlock (g_messages_lock);
726 return old_print_func;
730 g_print (const gchar *format,
735 GPrintFunc local_glib_print_func;
737 g_return_if_fail (format != NULL);
739 va_start (args, format);
740 string = g_strdup_vprintf (format, args);
743 g_mutex_lock (g_messages_lock);
744 local_glib_print_func = glib_print_func;
745 g_mutex_unlock (g_messages_lock);
747 if (local_glib_print_func)
748 local_glib_print_func (string);
751 ensure_stdout_valid ();
752 fputs (string, stdout);
759 g_set_printerr_handler (GPrintFunc func)
761 GPrintFunc old_printerr_func;
763 g_mutex_lock (g_messages_lock);
764 old_printerr_func = glib_printerr_func;
765 glib_printerr_func = func;
766 g_mutex_unlock (g_messages_lock);
768 return old_printerr_func;
772 g_printerr (const gchar *format,
777 GPrintFunc local_glib_printerr_func;
779 g_return_if_fail (format != NULL);
781 va_start (args, format);
782 string = g_strdup_vprintf (format, args);
785 g_mutex_lock (g_messages_lock);
786 local_glib_printerr_func = glib_printerr_func;
787 g_mutex_unlock (g_messages_lock);
789 if (local_glib_printerr_func)
790 local_glib_printerr_func (string);
793 fputs (string, stderr);
799 /* compatibility code */
801 g_set_error_handler (GErrorFunc func)
803 GErrorFunc old_error_func;
805 g_mutex_lock (g_messages_lock);
806 old_error_func = glib_error_func;
807 glib_error_func = func;
808 g_mutex_unlock (g_messages_lock);
810 return old_error_func;
813 /* compatibility code */
815 g_set_warning_handler (GWarningFunc func)
817 GWarningFunc old_warning_func;
819 g_mutex_lock (g_messages_lock);
820 old_warning_func = glib_warning_func;
821 glib_warning_func = func;
822 g_mutex_unlock (g_messages_lock);
824 return old_warning_func;
827 /* compatibility code */
829 g_set_message_handler (GPrintFunc func)
831 GPrintFunc old_message_func;
833 g_mutex_lock (g_messages_lock);
834 old_message_func = glib_message_func;
835 glib_message_func = func;
836 g_mutex_unlock (g_messages_lock);
838 return old_message_func;
842 # define MB_LEN_MAX 8
849 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
850 gboolean add_space, add_sign, possible_sign, seen_precision;
851 gboolean mod_half, mod_long, mod_extra_long;
855 printf_string_upper_bound (const gchar *format,
859 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
867 register gchar c = *format++;
871 else /* (c == '%') */
873 PrintfArgSpec spec = { 0, };
874 gboolean seen_l = FALSE, conv_done = FALSE;
876 const gchar *spec_start = format;
883 GDoubleIEEE754 u_double;
886 const gchar *v_string;
888 /* beware of positional parameters
892 g_warning (G_GNUC_PRETTY_FUNCTION
893 "(): unable to handle positional parameters (%%n$)");
894 len += 1024; /* try adding some safety padding */
900 spec.alternate_format = TRUE;
903 spec.zero_padding = TRUE;
906 spec.adjust_left = TRUE;
909 spec.add_space = TRUE;
912 spec.add_sign = TRUE;
915 spec.locale_grouping = TRUE;
918 /* parse output size specifications
921 spec.seen_precision = TRUE;
934 while (c >= '0' && c <= '9')
937 v_uint = v_uint * 10 + c - '0';
940 if (spec.seen_precision)
941 spec.precision = MAX (spec.precision, v_uint);
943 spec.min_width = MAX (spec.min_width, v_uint);
946 v_int = va_arg (args, int);
947 if (spec.seen_precision)
949 /* forget about negative precision */
951 spec.precision = MAX (spec.precision, v_int);
958 spec.adjust_left = TRUE;
960 spec.min_width = MAX (spec.min_width, v_int);
964 /* parse type modifiers
967 spec.mod_half = TRUE;
972 spec.mod_long = TRUE;
976 /* else, fall through */
979 spec.mod_long = TRUE;
980 spec.mod_extra_long = TRUE;
984 #if GLIB_SIZEOF_SIZE_T > 4
985 spec.mod_long = TRUE;
986 spec.mod_extra_long = TRUE;
987 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
990 #if GLIB_SIZEOF_PTRDIFF_T > 4
991 spec.mod_long = TRUE;
992 spec.mod_extra_long = TRUE;
993 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
996 #if GLIB_SIZEOF_INTMAX_T > 4
997 spec.mod_long = TRUE;
998 spec.mod_extra_long = TRUE;
999 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1002 /* parse output conversions
1011 /* some C libraries feature long variants for these as well? */
1012 spec.mod_long = TRUE;
1019 conv_len += 1; /* sign */
1026 spec.possible_sign = TRUE;
1028 if (spec.mod_long && honour_longs)
1030 if (spec.mod_extra_long)
1032 if (spec.mod_extra_long)
1034 #ifdef G_HAVE_GINT64
1035 (void) va_arg (args, gint64);
1036 #else /* !G_HAVE_GINT64 */
1037 (void) va_arg (args, long);
1038 #endif /* !G_HAVE_GINT64 */
1040 else if (spec.mod_long)
1041 (void) va_arg (args, long);
1043 (void) va_arg (args, int);
1055 spec.possible_sign = TRUE;
1056 /* n . dddddddddddddddddddddddd E +- eeee */
1057 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1058 if (may_warn && spec.mod_extra_long)
1059 g_warning (G_GNUC_PRETTY_FUNCTION
1060 "(): unable to handle long double, collecting double only");
1061 #ifdef HAVE_LONG_DOUBLE
1062 #error need to implement special handling for long double
1064 u_double.v_double = va_arg (args, double);
1065 /* %f can expand up to all significant digits before '.' (308) */
1067 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1069 gint exp = u_double.mpn.biased_exponent;
1071 exp -= G_IEEE754_DOUBLE_BIAS;
1072 exp = exp * G_LOG_2_BASE_10 + 1;
1075 /* some printf() implementations require extra padding for rounding */
1077 /* we can't really handle locale specific grouping here */
1078 if (spec.locale_grouping)
1082 spec.mod_long = TRUE;
1085 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1086 (void) va_arg (args, int);
1089 spec.mod_long = TRUE;
1092 v_string = va_arg (args, char*);
1094 conv_len += 8; /* hold "(null)" */
1095 else if (spec.seen_precision)
1096 conv_len += spec.precision;
1098 conv_len += strlen (v_string);
1103 g_warning (G_GNUC_PRETTY_FUNCTION
1104 "(): unable to handle wide char strings");
1105 len += 1024; /* try adding some safety padding */
1108 case 'P': /* do we actually need this? */
1111 spec.alternate_format = TRUE;
1118 (void) va_arg (args, void*);
1121 /* there's not much we can do to be clever */
1122 v_string = g_strerror (errno);
1123 v_uint = v_string ? strlen (v_string) : 0;
1124 conv_len += MAX (256, v_uint);
1127 /* handle invalid cases
1130 /* no conversion specification, bad bad */
1131 conv_len += format - spec_start;
1135 g_warning (G_GNUC_PRETTY_FUNCTION
1136 "(): unable to handle `%c' while parsing format",
1140 conv_done |= conv_len > 0;
1143 /* handle width specifications */
1144 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1146 conv_len += spec.alternate_format ? 2 : 0;
1147 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1150 } /* else (c == '%') */
1151 } /* while (*format) */
1157 g_printf_string_upper_bound (const gchar *format,
1160 return printf_string_upper_bound (format, TRUE, args);
1164 g_messages_init (void)
1166 g_messages_lock = g_mutex_new();
1167 g_log_depth = g_private_new(NULL);