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;
89 /* --- 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 */
154 g_log_write_prefix (gint fd,
157 static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
158 static gboolean initted = FALSE;
160 g_mutex_lock (g_messages_lock);
167 val = g_getenv ("G_PREFIX_MESSAGES");
171 static const GDebugKey keys[] = {
172 { "error", G_LOG_LEVEL_ERROR },
173 { "critical", G_LOG_LEVEL_CRITICAL },
174 { "warning", G_LOG_LEVEL_WARNING },
175 { "message", G_LOG_LEVEL_MESSAGE },
176 { "info", G_LOG_LEVEL_INFO },
177 { "debug", G_LOG_LEVEL_DEBUG }
180 g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
184 g_mutex_unlock (g_messages_lock);
186 if ((g_log_msg_prefix & mask) == mask)
188 gchar prg_pid[64], *prg_name;
190 prg_name = g_get_prgname ();
194 prg_name = "(process";
195 sprintf (prg_pid, ":%u): ", getpid ());
198 sprintf (prg_pid, " (pid:%u): ", getpid ());
200 write (fd, prg_name, strlen (prg_name));
201 write (fd, prg_pid, strlen (prg_pid));
205 static inline GLogDomain*
206 g_log_find_domain (const gchar *log_domain)
208 register GLogDomain *domain;
210 g_mutex_lock (g_messages_lock);
211 domain = g_log_domains;
214 if (strcmp (domain->log_domain, log_domain) == 0)
216 g_mutex_unlock (g_messages_lock);
219 domain = domain->next;
221 g_mutex_unlock (g_messages_lock);
225 static inline GLogDomain*
226 g_log_domain_new (const gchar *log_domain)
228 register GLogDomain *domain;
230 domain = g_new (GLogDomain, 1);
231 domain->log_domain = g_strdup (log_domain);
232 domain->fatal_mask = G_LOG_FATAL_MASK;
233 domain->handlers = NULL;
235 g_mutex_lock (g_messages_lock);
236 domain->next = g_log_domains;
237 g_log_domains = domain;
238 g_mutex_unlock (g_messages_lock);
244 g_log_domain_check_free (GLogDomain *domain)
246 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
247 domain->handlers == NULL)
249 register GLogDomain *last, *work;
253 g_mutex_lock (g_messages_lock);
254 work = g_log_domains;
260 last->next = domain->next;
262 g_log_domains = domain->next;
263 g_free (domain->log_domain);
270 g_mutex_unlock (g_messages_lock);
274 static inline GLogFunc
275 g_log_domain_get_handler (GLogDomain *domain,
276 GLogLevelFlags log_level,
279 if (domain && log_level)
281 register GLogHandler *handler;
283 handler = domain->handlers;
286 if ((handler->log_level & log_level) == log_level)
288 *data = handler->data;
289 return handler->log_func;
291 handler = handler->next;
294 return g_log_default_handler;
298 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
300 GLogLevelFlags old_mask;
302 /* restrict the global mask to levels that are known to glib */
303 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
304 /* force errors to be fatal */
305 fatal_mask |= G_LOG_LEVEL_ERROR;
306 /* remove bogus flag */
307 fatal_mask &= ~G_LOG_FLAG_FATAL;
309 g_mutex_lock (g_messages_lock);
310 old_mask = g_log_always_fatal;
311 g_log_always_fatal = fatal_mask;
312 g_mutex_unlock (g_messages_lock);
318 g_log_set_fatal_mask (const gchar *log_domain,
319 GLogLevelFlags fatal_mask)
321 GLogLevelFlags old_flags;
322 register GLogDomain *domain;
327 /* force errors to be fatal */
328 fatal_mask |= G_LOG_LEVEL_ERROR;
329 /* remove bogus flag */
330 fatal_mask &= ~G_LOG_FLAG_FATAL;
332 domain = g_log_find_domain (log_domain);
334 domain = g_log_domain_new (log_domain);
335 old_flags = domain->fatal_mask;
337 domain->fatal_mask = fatal_mask;
338 g_log_domain_check_free (domain);
344 g_log_set_handler (const gchar *log_domain,
345 GLogLevelFlags log_levels,
349 register GLogDomain *domain;
350 register GLogHandler *handler;
351 static guint handler_id = 0;
353 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
354 g_return_val_if_fail (log_func != NULL, 0);
359 domain = g_log_find_domain (log_domain);
361 domain = g_log_domain_new (log_domain);
363 handler = g_new (GLogHandler, 1);
364 g_mutex_lock (g_messages_lock);
365 handler->id = ++handler_id;
366 g_mutex_unlock (g_messages_lock);
367 handler->log_level = log_levels;
368 handler->log_func = log_func;
369 handler->data = user_data;
370 handler->next = domain->handlers;
371 domain->handlers = handler;
377 g_log_remove_handler (const gchar *log_domain,
380 register GLogDomain *domain;
382 g_return_if_fail (handler_id > 0);
387 domain = g_log_find_domain (log_domain);
390 register GLogHandler *work, *last;
393 work = domain->handlers;
396 if (work->id == handler_id)
399 last->next = work->next;
401 domain->handlers = work->next;
403 g_log_domain_check_free (domain);
410 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
416 g_logv (const gchar *log_domain,
417 GLogLevelFlags log_level,
425 log_level &= G_LOG_LEVEL_MASK;
429 /* we use a stack buffer of fixed size, because we might get called
432 G_VA_COPY (args2, args1);
433 if (printf_string_upper_bound (format, FALSE, args1) < 1024)
434 vsprintf (buffer, format, args2);
437 /* since we might be out of memory, we can't use g_vsnprintf(). */
438 #ifdef HAVE_VSNPRINTF
439 vsnprintf (buffer, 1024, format, args2);
440 #else /* !HAVE_VSNPRINTF */
441 /* we are out of luck here */
442 strncpy (buffer, format, 1024);
443 #endif /* !HAVE_VSNPRINTF */
448 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
450 register GLogLevelFlags test_level;
453 if (log_level & test_level)
455 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
458 gpointer data = NULL;
460 domain = g_log_find_domain (log_domain ? log_domain : "");
463 test_level |= G_LOG_FLAG_RECURSION;
466 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
468 g_mutex_lock (g_messages_lock);
469 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
470 g_log_always_fatal) & test_level) != 0)
471 test_level |= G_LOG_FLAG_FATAL;
472 g_mutex_unlock (g_messages_lock);
474 log_func = g_log_domain_get_handler (domain, test_level, &data);
475 log_func (log_domain, test_level, buffer, data);
477 /* *domain can be cluttered now */
479 if (test_level & G_LOG_FLAG_FATAL)
481 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
482 if (!(test_level & G_LOG_FLAG_RECURSION))
486 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
488 MessageBox (NULL, fatal_msg_buf, NULL, MB_OK);
490 # if defined (_MSC_VER) && defined (_DEBUG)
491 /* let's see the call stack ... */
495 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
499 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
505 g_log (const gchar *log_domain,
506 GLogLevelFlags log_level,
512 va_start (args, format);
513 g_logv (log_domain, log_level, format, args);
518 g_log_default_handler (const gchar *log_domain,
519 GLogLevelFlags log_level,
520 const gchar *message,
521 gpointer unused_data)
528 gboolean in_recursion;
530 GErrorFunc local_glib_error_func;
531 GWarningFunc local_glib_warning_func;
532 GPrintFunc local_glib_message_func;
533 gchar prg_pid[64], *prg_name = g_get_prgname ();
535 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
536 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
537 log_level &= G_LOG_LEVEL_MASK;
540 message = "g_log_default_handler(): (NULL) message";
543 prg_name = "(process";
544 sprintf (prg_pid, ":%u): ", getpid ());
547 sprintf (prg_pid, " (pid:%u): ", getpid ());
550 /* Use just stdout as stderr is hard to get redirected from the
554 gonna_abort = is_fatal;
556 fd = (log_level > G_LOG_LEVEL_MESSAGE) ? 1 : 2;
559 g_mutex_lock (g_messages_lock);
560 local_glib_error_func = glib_error_func;
561 local_glib_warning_func = glib_warning_func;
562 local_glib_message_func = glib_message_func;
563 g_mutex_unlock (g_messages_lock);
567 case G_LOG_LEVEL_ERROR:
568 if (!log_domain && local_glib_error_func)
570 /* compatibility code */
571 local_glib_error_func (message);
574 /* use write(2) for output, in case we are out of memeory */
575 ensure_stdout_valid ();
577 g_log_write_prefix (fd, log_level);
581 write (fd, log_domain, strlen (log_domain));
585 write (fd, "** ", 3);
587 write (fd, "ERROR (recursed) **: ", 21);
589 write (fd, "ERROR **: ", 10);
590 write (fd, message, strlen (message));
592 write (fd, "\naborting...\n", 13);
596 case G_LOG_LEVEL_CRITICAL:
597 ensure_stdout_valid ();
599 g_log_write_prefix (fd, log_level);
603 write (fd, log_domain, strlen (log_domain));
607 write (fd, "** ", 3);
609 write (fd, "CRITICAL (recursed) **: ", 24);
611 write (fd, "CRITICAL **: ", 13);
612 write (fd, message, strlen (message));
614 write (fd, "\naborting...\n", 13);
618 case G_LOG_LEVEL_WARNING:
619 if (!log_domain && local_glib_warning_func)
621 /* compatibility code */
622 local_glib_warning_func (message);
625 ensure_stdout_valid ();
627 g_log_write_prefix (fd, log_level);
631 write (fd, log_domain, strlen (log_domain));
635 write (fd, "** ", 3);
637 write (fd, "WARNING (recursed) **: ", 23);
639 write (fd, "WARNING **: ", 12);
640 write (fd, message, strlen (message));
642 write (fd, "\naborting...\n", 13);
646 case G_LOG_LEVEL_MESSAGE:
647 if (!log_domain && local_glib_message_func)
649 /* compatibility code */
650 local_glib_message_func (message);
653 ensure_stdout_valid ();
655 g_log_write_prefix (fd, log_level);
659 write (fd, log_domain, strlen (log_domain));
663 write (fd, "Message (recursed): ", 20);
665 write (fd, "Message: ", 9);
666 write (fd, message, strlen (message));
668 write (fd, "\naborting...\n", 13);
672 case G_LOG_LEVEL_INFO:
673 ensure_stdout_valid ();
675 g_log_write_prefix (fd, log_level);
679 write (fd, log_domain, strlen (log_domain));
683 write (fd, "INFO (recursed): ", 17);
685 write (fd, "INFO: ", 6);
686 write (fd, message, strlen (message));
688 write (fd, "\naborting...\n", 13);
692 case G_LOG_LEVEL_DEBUG:
693 ensure_stdout_valid ();
695 g_log_write_prefix (fd, log_level);
699 write (fd, log_domain, strlen (log_domain));
703 write (fd, "DEBUG (recursed): ", 18);
705 write (fd, "DEBUG: ", 7);
706 write (fd, message, strlen (message));
708 write (fd, "\naborting...\n", 13);
713 /* we are used for a log level that is not defined by GLib itself,
714 * try to make the best out of it.
716 ensure_stdout_valid ();
718 g_log_write_prefix (fd, log_level);
722 write (fd, log_domain, strlen (log_domain));
724 write (fd, "-LOG (recursed:", 15);
726 write (fd, "-LOG (", 6);
728 else if (in_recursion)
729 write (fd, "LOG (recursed:", 14);
731 write (fd, "LOG (", 5);
734 gchar string[] = "0x00): ";
735 gchar *p = string + 2;
738 i = g_bit_nth_msf (log_level, -1);
741 *p = '0' + (i & 0xf);
745 write (fd, string, 7);
748 write (fd, "): ", 3);
749 write (fd, message, strlen (message));
751 write (fd, "\naborting...\n", 13);
759 g_set_print_handler (GPrintFunc func)
761 GPrintFunc old_print_func;
763 g_mutex_lock (g_messages_lock);
764 old_print_func = glib_print_func;
765 glib_print_func = func;
766 g_mutex_unlock (g_messages_lock);
768 return old_print_func;
772 g_print (const gchar *format,
777 GPrintFunc local_glib_print_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_print_func = glib_print_func;
787 g_mutex_unlock (g_messages_lock);
789 if (local_glib_print_func)
790 local_glib_print_func (string);
793 ensure_stdout_valid ();
794 fputs (string, stdout);
801 g_set_printerr_handler (GPrintFunc func)
803 GPrintFunc old_printerr_func;
805 g_mutex_lock (g_messages_lock);
806 old_printerr_func = glib_printerr_func;
807 glib_printerr_func = func;
808 g_mutex_unlock (g_messages_lock);
810 return old_printerr_func;
814 g_printerr (const gchar *format,
819 GPrintFunc local_glib_printerr_func;
821 g_return_if_fail (format != NULL);
823 va_start (args, format);
824 string = g_strdup_vprintf (format, args);
827 g_mutex_lock (g_messages_lock);
828 local_glib_printerr_func = glib_printerr_func;
829 g_mutex_unlock (g_messages_lock);
831 if (local_glib_printerr_func)
832 local_glib_printerr_func (string);
835 fputs (string, stderr);
841 /* compatibility code */
843 g_set_error_handler (GErrorFunc func)
845 GErrorFunc old_error_func;
847 g_mutex_lock (g_messages_lock);
848 old_error_func = glib_error_func;
849 glib_error_func = func;
850 g_mutex_unlock (g_messages_lock);
852 return old_error_func;
855 /* compatibility code */
857 g_set_warning_handler (GWarningFunc func)
859 GWarningFunc old_warning_func;
861 g_mutex_lock (g_messages_lock);
862 old_warning_func = glib_warning_func;
863 glib_warning_func = func;
864 g_mutex_unlock (g_messages_lock);
866 return old_warning_func;
869 /* compatibility code */
871 g_set_message_handler (GPrintFunc func)
873 GPrintFunc old_message_func;
875 g_mutex_lock (g_messages_lock);
876 old_message_func = glib_message_func;
877 glib_message_func = func;
878 g_mutex_unlock (g_messages_lock);
880 return old_message_func;
884 # define MB_LEN_MAX 8
891 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
892 gboolean add_space, add_sign, possible_sign, seen_precision;
893 gboolean mod_half, mod_long, mod_extra_long;
897 printf_string_upper_bound (const gchar *format,
901 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
909 register gchar c = *format++;
913 else /* (c == '%') */
915 PrintfArgSpec spec = { 0, };
916 gboolean seen_l = FALSE, conv_done = FALSE;
918 const gchar *spec_start = format;
925 GDoubleIEEE754 u_double;
928 const gchar *v_string;
930 /* beware of positional parameters
934 g_warning (G_GNUC_PRETTY_FUNCTION
935 "(): unable to handle positional parameters (%%n$)");
936 len += 1024; /* try adding some safety padding */
942 spec.alternate_format = TRUE;
945 spec.zero_padding = TRUE;
948 spec.adjust_left = TRUE;
951 spec.add_space = TRUE;
954 spec.add_sign = TRUE;
957 spec.locale_grouping = TRUE;
960 /* parse output size specifications
963 spec.seen_precision = TRUE;
976 while (c >= '0' && c <= '9')
979 v_uint = v_uint * 10 + c - '0';
982 if (spec.seen_precision)
983 spec.precision = MAX (spec.precision, v_uint);
985 spec.min_width = MAX (spec.min_width, v_uint);
988 v_int = va_arg (args, int);
989 if (spec.seen_precision)
991 /* forget about negative precision */
993 spec.precision = MAX (spec.precision, v_int);
1000 spec.adjust_left = TRUE;
1002 spec.min_width = MAX (spec.min_width, v_int);
1006 /* parse type modifiers
1009 spec.mod_half = TRUE;
1014 spec.mod_long = TRUE;
1018 /* else, fall through */
1021 spec.mod_long = TRUE;
1022 spec.mod_extra_long = TRUE;
1026 #if GLIB_SIZEOF_SIZE_T > 4
1027 spec.mod_long = TRUE;
1028 spec.mod_extra_long = TRUE;
1029 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
1032 #if GLIB_SIZEOF_PTRDIFF_T > 4
1033 spec.mod_long = TRUE;
1034 spec.mod_extra_long = TRUE;
1035 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
1038 #if GLIB_SIZEOF_INTMAX_T > 4
1039 spec.mod_long = TRUE;
1040 spec.mod_extra_long = TRUE;
1041 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
1044 /* parse output conversions
1053 /* some C libraries feature long variants for these as well? */
1054 spec.mod_long = TRUE;
1061 conv_len += 1; /* sign */
1068 spec.possible_sign = TRUE;
1070 if (spec.mod_long && honour_longs)
1072 if (spec.mod_extra_long)
1074 if (spec.mod_extra_long)
1076 #ifdef G_HAVE_GINT64
1077 (void) va_arg (args, gint64);
1078 #else /* !G_HAVE_GINT64 */
1079 (void) va_arg (args, long);
1080 #endif /* !G_HAVE_GINT64 */
1082 else if (spec.mod_long)
1083 (void) va_arg (args, long);
1085 (void) va_arg (args, int);
1097 spec.possible_sign = TRUE;
1098 /* n . dddddddddddddddddddddddd E +- eeee */
1099 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1100 if (may_warn && spec.mod_extra_long)
1101 g_warning (G_GNUC_PRETTY_FUNCTION
1102 "(): unable to handle long double, collecting double only");
1103 #ifdef HAVE_LONG_DOUBLE
1104 #error need to implement special handling for long double
1106 u_double.v_double = va_arg (args, double);
1107 /* %f can expand up to all significant digits before '.' (308) */
1109 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1111 gint exp = u_double.mpn.biased_exponent;
1113 exp -= G_IEEE754_DOUBLE_BIAS;
1114 exp = exp * G_LOG_2_BASE_10 + 1;
1117 /* some printf() implementations require extra padding for rounding */
1119 /* we can't really handle locale specific grouping here */
1120 if (spec.locale_grouping)
1124 spec.mod_long = TRUE;
1127 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1128 (void) va_arg (args, int);
1131 spec.mod_long = TRUE;
1134 v_string = va_arg (args, char*);
1136 conv_len += 8; /* hold "(null)" */
1137 else if (spec.seen_precision)
1138 conv_len += spec.precision;
1140 conv_len += strlen (v_string);
1145 g_warning (G_GNUC_PRETTY_FUNCTION
1146 "(): unable to handle wide char strings");
1147 len += 1024; /* try adding some safety padding */
1150 case 'P': /* do we actually need this? */
1153 spec.alternate_format = TRUE;
1160 (void) va_arg (args, void*);
1163 /* there's not much we can do to be clever */
1164 v_string = g_strerror (errno);
1165 v_uint = v_string ? strlen (v_string) : 0;
1166 conv_len += MAX (256, v_uint);
1169 /* handle invalid cases
1172 /* no conversion specification, bad bad */
1173 conv_len += format - spec_start;
1177 g_warning (G_GNUC_PRETTY_FUNCTION
1178 "(): unable to handle `%c' while parsing format",
1182 conv_done |= conv_len > 0;
1185 /* handle width specifications */
1186 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1188 conv_len += spec.alternate_format ? 2 : 0;
1189 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1192 } /* else (c == '%') */
1193 } /* while (*format) */
1199 g_printf_string_upper_bound (const gchar *format,
1202 return printf_string_upper_bound (format, TRUE, args);
1206 g_messages_init (void)
1208 g_messages_lock = g_mutex_new();
1209 g_log_depth = g_private_new(NULL);