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);
439 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
443 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
449 g_log (const gchar *log_domain,
450 GLogLevelFlags log_level,
456 va_start (args, format);
457 g_logv (log_domain, log_level, format, args);
462 g_log_default_handler (const gchar *log_domain,
463 GLogLevelFlags log_level,
464 const gchar *message,
465 gpointer unused_data)
472 gboolean in_recursion;
474 GErrorFunc local_glib_error_func;
475 GWarningFunc local_glib_warning_func;
476 GPrintFunc local_glib_message_func;
477 gchar prg_pid[64], *prg_name = g_get_prgname ();
479 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
480 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
481 log_level &= G_LOG_LEVEL_MASK;
484 message = "g_log_default_handler(): (NULL) message";
487 prg_name = "(process";
488 sprintf (prg_pid, ":%u): ", getpid ());
491 sprintf (prg_pid, " (pid:%u): ", getpid ());
494 /* Use just stdout as stderr is hard to get redirected from the
498 gonna_abort = is_fatal;
500 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
503 g_mutex_lock (g_messages_lock);
504 local_glib_error_func = glib_error_func;
505 local_glib_warning_func = glib_warning_func;
506 local_glib_message_func = glib_message_func;
507 g_mutex_unlock (g_messages_lock);
511 case G_LOG_LEVEL_ERROR:
512 if (!log_domain && local_glib_error_func)
514 /* compatibility code */
515 local_glib_error_func (message);
518 /* use write(2) for output, in case we are out of memeory */
519 ensure_stdout_valid ();
521 #ifdef G_ENABLE_MSG_PREFIX
522 write (fd, prg_name, strlen (prg_name));
523 write (fd, prg_pid, strlen (prg_pid));
524 #endif /* G_ENABLE_MSG_PREFIX */
527 write (fd, log_domain, strlen (log_domain));
531 write (fd, "** ", 3);
533 write (fd, "ERROR (recursed) **: ", 21);
535 write (fd, "ERROR **: ", 10);
536 write (fd, message, strlen (message));
538 write (fd, "\naborting...\n", 13);
542 case G_LOG_LEVEL_CRITICAL:
543 ensure_stdout_valid ();
545 #ifdef G_ENABLE_MSG_PREFIX
546 write (fd, prg_name, strlen (prg_name));
547 write (fd, prg_pid, strlen (prg_pid));
548 #endif /* G_ENABLE_MSG_PREFIX */
551 write (fd, log_domain, strlen (log_domain));
555 write (fd, "** ", 3);
557 write (fd, "CRITICAL (recursed) **: ", 24);
559 write (fd, "CRITICAL **: ", 13);
560 write (fd, message, strlen (message));
562 write (fd, "\naborting...\n", 13);
566 case G_LOG_LEVEL_WARNING:
567 if (!log_domain && local_glib_warning_func)
569 /* compatibility code */
570 local_glib_warning_func (message);
573 ensure_stdout_valid ();
575 #ifdef G_ENABLE_MSG_PREFIX
576 write (fd, prg_name, strlen (prg_name));
577 write (fd, prg_pid, strlen (prg_pid));
578 #endif /* G_ENABLE_MSG_PREFIX */
581 write (fd, log_domain, strlen (log_domain));
585 write (fd, "** ", 3);
587 write (fd, "WARNING (recursed) **: ", 23);
589 write (fd, "WARNING **: ", 12);
590 write (fd, message, strlen (message));
592 write (fd, "\naborting...\n", 13);
596 case G_LOG_LEVEL_MESSAGE:
597 if (!log_domain && local_glib_message_func)
599 /* compatibility code */
600 local_glib_message_func (message);
603 ensure_stdout_valid ();
604 #ifdef G_ENABLE_MSG_PREFIX
605 write (fd, prg_name, strlen (prg_name));
606 write (fd, prg_pid, strlen (prg_pid));
607 #endif /* G_ENABLE_MSG_PREFIX */
610 write (fd, log_domain, strlen (log_domain));
614 write (fd, "Message (recursed): ", 20);
616 write (fd, "Message: ", 9);
617 write (fd, message, strlen (message));
619 write (fd, "\naborting...\n", 13);
623 case G_LOG_LEVEL_INFO:
624 ensure_stdout_valid ();
625 #ifdef G_ENABLE_MSG_PREFIX
626 write (fd, prg_name, strlen (prg_name));
627 write (fd, prg_pid, strlen (prg_pid));
628 #endif /* G_ENABLE_MSG_PREFIX */
631 write (fd, log_domain, strlen (log_domain));
635 write (fd, "INFO (recursed): ", 17);
637 write (fd, "INFO: ", 6);
638 write (fd, message, strlen (message));
640 write (fd, "\naborting...\n", 13);
644 case G_LOG_LEVEL_DEBUG:
645 ensure_stdout_valid ();
646 #ifdef G_ENABLE_MSG_PREFIX
647 write (fd, prg_name, strlen (prg_name));
648 write (fd, prg_pid, strlen (prg_pid));
649 #endif /* G_ENABLE_MSG_PREFIX */
652 write (fd, log_domain, strlen (log_domain));
656 write (fd, "DEBUG (recursed): ", 18);
658 write (fd, "DEBUG: ", 7);
659 write (fd, message, strlen (message));
661 write (fd, "\naborting...\n", 13);
666 /* we are used for a log level that is not defined by GLib itself,
667 * try to make the best out of it.
669 ensure_stdout_valid ();
670 #ifdef G_ENABLE_MSG_PREFIX
671 write (fd, prg_name, strlen (prg_name));
672 write (fd, prg_pid, strlen (prg_pid));
673 #endif /* G_ENABLE_MSG_PREFIX */
676 write (fd, log_domain, strlen (log_domain));
678 write (fd, "-LOG (recursed:", 15);
680 write (fd, "-LOG (", 6);
682 else if (in_recursion)
683 write (fd, "LOG (recursed:", 14);
685 write (fd, "LOG (", 5);
688 gchar string[] = "0x00): ";
689 gchar *p = string + 2;
692 i = g_bit_nth_msf (log_level, -1);
695 *p = '0' + (i & 0xf);
699 write (fd, string, 7);
702 write (fd, "): ", 3);
703 write (fd, message, strlen (message));
705 write (fd, "\naborting...\n", 13);
713 g_set_print_handler (GPrintFunc func)
715 GPrintFunc old_print_func;
717 g_mutex_lock (g_messages_lock);
718 old_print_func = glib_print_func;
719 glib_print_func = func;
720 g_mutex_unlock (g_messages_lock);
722 return old_print_func;
726 g_print (const gchar *format,
731 GPrintFunc local_glib_print_func;
733 g_return_if_fail (format != NULL);
735 va_start (args, format);
736 string = g_strdup_vprintf (format, args);
739 g_mutex_lock (g_messages_lock);
740 local_glib_print_func = glib_print_func;
741 g_mutex_unlock (g_messages_lock);
743 if (local_glib_print_func)
744 local_glib_print_func (string);
747 ensure_stdout_valid ();
748 fputs (string, stdout);
755 g_set_printerr_handler (GPrintFunc func)
757 GPrintFunc old_printerr_func;
759 g_mutex_lock (g_messages_lock);
760 old_printerr_func = glib_printerr_func;
761 glib_printerr_func = func;
762 g_mutex_unlock (g_messages_lock);
764 return old_printerr_func;
768 g_printerr (const gchar *format,
773 GPrintFunc local_glib_printerr_func;
775 g_return_if_fail (format != NULL);
777 va_start (args, format);
778 string = g_strdup_vprintf (format, args);
781 g_mutex_lock (g_messages_lock);
782 local_glib_printerr_func = glib_printerr_func;
783 g_mutex_unlock (g_messages_lock);
785 if (local_glib_printerr_func)
786 local_glib_printerr_func (string);
789 fputs (string, stderr);
795 /* compatibility code */
797 g_set_error_handler (GErrorFunc func)
799 GErrorFunc old_error_func;
801 g_mutex_lock (g_messages_lock);
802 old_error_func = glib_error_func;
803 glib_error_func = func;
804 g_mutex_unlock (g_messages_lock);
806 return old_error_func;
809 /* compatibility code */
811 g_set_warning_handler (GWarningFunc func)
813 GWarningFunc old_warning_func;
815 g_mutex_lock (g_messages_lock);
816 old_warning_func = glib_warning_func;
817 glib_warning_func = func;
818 g_mutex_unlock (g_messages_lock);
820 return old_warning_func;
823 /* compatibility code */
825 g_set_message_handler (GPrintFunc func)
827 GPrintFunc old_message_func;
829 g_mutex_lock (g_messages_lock);
830 old_message_func = glib_message_func;
831 glib_message_func = func;
832 g_mutex_unlock (g_messages_lock);
834 return old_message_func;
838 # define MB_LEN_MAX 8
845 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
846 gboolean add_space, add_sign, possible_sign, seen_precision;
847 gboolean mod_half, mod_long, mod_extra_long;
851 printf_string_upper_bound (const gchar *format,
855 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
863 register gchar c = *format++;
867 else /* (c == '%') */
869 PrintfArgSpec spec = { 0, };
870 gboolean seen_l = FALSE, conv_done = FALSE;
872 const gchar *spec_start = format;
879 GDoubleIEEE754 u_double;
884 /* beware of positional parameters
888 g_warning (G_GNUC_PRETTY_FUNCTION
889 "(): unable to handle positional parameters (%%n$)");
890 len += 1024; /* try adding some safety padding */
896 spec.alternate_format = TRUE;
899 spec.zero_padding = TRUE;
902 spec.adjust_left = TRUE;
905 spec.add_space = TRUE;
908 spec.add_sign = TRUE;
911 spec.locale_grouping = TRUE;
914 /* parse output size specifications
917 spec.seen_precision = TRUE;
930 while (c >= '0' && c <= '9')
933 v_uint = v_uint * 10 + c - '0';
936 if (spec.seen_precision)
937 spec.precision = MAX (spec.precision, v_uint);
939 spec.min_width = MAX (spec.min_width, v_uint);
942 v_int = va_arg (args, int);
943 if (spec.seen_precision)
945 /* forget about negative precision */
947 spec.precision = MAX (spec.precision, v_int);
954 spec.adjust_left = TRUE;
956 spec.min_width = MAX (spec.min_width, v_int);
960 /* parse type modifiers
963 spec.mod_half = TRUE;
968 spec.mod_long = TRUE;
972 /* else, fall through */
975 spec.mod_long = TRUE;
976 spec.mod_extra_long = TRUE;
980 #if GLIB_SIZEOF_SIZE_T > 4
981 spec.mod_long = TRUE;
982 spec.mod_extra_long = TRUE;
983 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
986 #if GLIB_SIZEOF_PTRDIFF_T > 4
987 spec.mod_long = TRUE;
988 spec.mod_extra_long = TRUE;
989 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
992 #if GLIB_SIZEOF_INTMAX_T > 4
993 spec.mod_long = TRUE;
994 spec.mod_extra_long = TRUE;
995 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
998 /* parse output conversions
1007 /* some C libraries feature long variants for these as well? */
1008 spec.mod_long = TRUE;
1015 conv_len += 1; /* sign */
1022 spec.possible_sign = TRUE;
1024 if (spec.mod_long && honour_longs)
1026 if (spec.mod_extra_long)
1028 if (spec.mod_extra_long)
1030 #ifdef G_HAVE_GINT64
1031 (void) va_arg (args, gint64);
1032 #else /* !G_HAVE_GINT64 */
1033 (void) va_arg (args, long);
1034 #endif /* !G_HAVE_GINT64 */
1036 else if (spec.mod_long)
1037 (void) va_arg (args, long);
1039 (void) va_arg (args, int);
1051 spec.possible_sign = TRUE;
1052 /* n . dddddddddddddddddddddddd E +- eeee */
1053 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1054 if (may_warn && spec.mod_extra_long)
1055 g_warning (G_GNUC_PRETTY_FUNCTION
1056 "(): unable to handle long double, collecting double only");
1057 #ifdef HAVE_LONG_DOUBLE
1058 #error need to implement special handling for long double
1060 u_double.v_double = va_arg (args, double);
1061 /* %f can expand up to all significant digits before '.' (308) */
1063 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1065 gint exp = u_double.mpn.biased_exponent;
1067 exp -= G_IEEE754_DOUBLE_BIAS;
1068 exp = exp * G_LOG_2_BASE_10 + 1;
1071 /* some printf() implementations require extra padding for rounding */
1073 /* we can't really handle locale specific grouping here */
1074 if (spec.locale_grouping)
1078 spec.mod_long = TRUE;
1081 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1082 (void) va_arg (args, int);
1085 spec.mod_long = TRUE;
1088 v_string = va_arg (args, char*);
1090 conv_len += 8; /* hold "(null)" */
1091 else if (spec.seen_precision)
1092 conv_len += spec.precision;
1094 conv_len += strlen (v_string);
1099 g_warning (G_GNUC_PRETTY_FUNCTION
1100 "(): unable to handle wide char strings");
1101 len += 1024; /* try adding some safety padding */
1104 case 'P': /* do we actually need this? */
1107 spec.alternate_format = TRUE;
1114 (void) va_arg (args, void*);
1117 /* there's not much we can do to be clever */
1118 v_string = g_strerror (errno);
1119 v_uint = v_string ? strlen (v_string) : 0;
1120 conv_len += MAX (256, v_uint);
1123 /* handle invalid cases
1126 /* no conversion specification, bad bad */
1127 conv_len += format - spec_start;
1131 g_warning (G_GNUC_PRETTY_FUNCTION
1132 "(): unable to handle `%c' while parsing format",
1136 conv_done |= conv_len > 0;
1139 /* handle width specifications */
1140 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1142 conv_len += spec.alternate_format ? 2 : 0;
1143 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1146 } /* else (c == '%') */
1147 } /* while (*format) */
1153 g_printf_string_upper_bound (const gchar *format,
1156 return printf_string_upper_bound (format, TRUE, args);
1160 g_messages_init (void)
1162 g_messages_lock = g_mutex_new();
1163 g_log_depth = g_private_new(NULL);