📄 gmessages.c
字号:
case G_LOG_LEVEL_ERROR: strcpy (level_prefix, "ERROR"); to_stdout = FALSE; break; case G_LOG_LEVEL_CRITICAL: strcpy (level_prefix, "CRITICAL"); to_stdout = FALSE; break; case G_LOG_LEVEL_WARNING: strcpy (level_prefix, "WARNING"); to_stdout = FALSE; break; case G_LOG_LEVEL_MESSAGE: strcpy (level_prefix, "Message"); to_stdout = FALSE; break; case G_LOG_LEVEL_INFO: strcpy (level_prefix, "INFO"); break; case G_LOG_LEVEL_DEBUG: strcpy (level_prefix, "DEBUG"); break; default: if (log_level) { strcpy (level_prefix, "LOG-"); format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16); } else strcpy (level_prefix, "LOG"); break; } if (log_level & G_LOG_FLAG_RECURSION) strcat (level_prefix, " (recursed)"); if (log_level & ALERT_LEVELS) strcat (level_prefix, " **"); ensure_stdout_valid ();#ifdef G_OS_WIN32 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0; /* Use just stdout as stderr is hard to get redirected from the DOS prompt. */ return stdout;#else return to_stdout ? 1 : 2;#endif}void_g_log_fallback_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data){ gchar level_prefix[STRING_BUFFER_SIZE], pid_string[FORMAT_UNSIGNED_BUFSIZE]; gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0; GFileDescriptor fd; /* we can not call _any_ GLib functions in this fallback handler, * which is why we skip UTF-8 conversion, etc. * since we either recursed or ran out of memory, we're in a pretty * pathologic situation anyways, what we can do is giving the * the process ID unconditionally however. */ fd = mklevel_prefix (level_prefix, log_level); if (!message) message = "(NULL) message"; format_unsigned (pid_string, getpid (), 10); if (log_domain) write_string (fd, "\n"); else write_string (fd, "\n** "); write_string (fd, "(process:"); write_string (fd, pid_string); write_string (fd, "): "); if (log_domain) { write_string (fd, log_domain); write_string (fd, "-"); } write_string (fd, level_prefix); write_string (fd, ": "); write_string (fd, message); if (is_fatal) write_string (fd, "\naborting...\n"); else write_string (fd, "\n");}voidg_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data){ gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0; gchar level_prefix[STRING_BUFFER_SIZE], *string; GString *gstring; GFileDescriptor fd; /* we can be called externally with recursion for whatever reason */ if (log_level & G_LOG_FLAG_RECURSION) { _g_log_fallback_handler (log_domain, log_level, message, unused_data); return; } g_messages_prefixed_init (); fd = mklevel_prefix (level_prefix, log_level); gstring = g_string_new (""); if (log_level & ALERT_LEVELS) g_string_append (gstring, "\n"); if (!log_domain) g_string_append (gstring, "** "); if ((g_log_msg_prefix & log_level) == log_level) { const gchar *prg_name = g_get_prgname (); if (!prg_name) g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ()); else g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ()); } if (log_domain) { g_string_append (gstring, log_domain); g_string_append_c (gstring, '-'); } g_string_append (gstring, level_prefix); g_string_append (gstring, ": "); if (!message) g_string_append (gstring, "(NULL) message"); else {#if 0 const gchar *charset; if (g_get_charset (&charset)) g_string_append (gstring, message); /* charset is UTF-8 already */ else { string = strdup_convert (message, charset); g_string_append (gstring, string); g_free (string); }#else g_string_append (gstring, message);#endif } if (is_fatal) g_string_append (gstring, "\naborting...\n"); else g_string_append (gstring, "\n"); string = g_string_free (gstring, FALSE); write_string (fd, string); g_free (string);}GPrintFuncg_set_print_handler (GPrintFunc func){ GPrintFunc old_print_func; g_mutex_lock (g_messages_lock); old_print_func = glib_print_func; glib_print_func = func; g_mutex_unlock (g_messages_lock); return old_print_func;}voidg_print (const gchar *format, ...){ va_list args; gchar *string; GPrintFunc local_glib_print_func; g_return_if_fail (format != NULL); va_start (args, format); string = g_strdup_vprintf (format, args); va_end (args); g_mutex_lock (g_messages_lock); local_glib_print_func = glib_print_func; g_mutex_unlock (g_messages_lock); if (local_glib_print_func) local_glib_print_func (string); else { ensure_stdout_valid ();#if 0 const gchar *charset; if (g_get_charset (&charset)) fputs (string, stdout); /* charset is UTF-8 already */ else { gchar *lstring = strdup_convert (string, charset); fputs (lstring, stdout); g_free (lstring); }#else fputs (string, stdout);#endif fflush (stdout); } g_free (string);}GPrintFuncg_set_printerr_handler (GPrintFunc func){ GPrintFunc old_printerr_func; g_mutex_lock (g_messages_lock); old_printerr_func = glib_printerr_func; glib_printerr_func = func; g_mutex_unlock (g_messages_lock); return old_printerr_func;}voidg_printerr (const gchar *format, ...){ va_list args; gchar *string; GPrintFunc local_glib_printerr_func; g_return_if_fail (format != NULL); va_start (args, format); string = g_strdup_vprintf (format, args); va_end (args); g_mutex_lock (g_messages_lock); local_glib_printerr_func = glib_printerr_func; g_mutex_unlock (g_messages_lock); if (local_glib_printerr_func) local_glib_printerr_func (string); else {#if 0 const gchar *charset; if (g_get_charset (&charset)) fputs (string, stderr); /* charset is UTF-8 already */ else { gchar *lstring = strdup_convert (string, charset); fputs (lstring, stderr); g_free (lstring); }#else fputs (string, stderr); /* charset is UTF-8 already */#endif fflush (stderr); } g_free (string);}#ifndef MB_LEN_MAX# define MB_LEN_MAX 8#endif#ifndef HAVE_C99_VSNPRINTFtypedef struct{ guint min_width; guint precision; gboolean alternate_format, zero_padding, adjust_left, locale_grouping; gboolean add_space, add_sign, possible_sign, seen_precision; gboolean mod_half, mod_long, mod_extra_long;} PrintfArgSpec;static gsizeprintf_string_upper_bound (const gchar *format, gboolean may_warn, va_list args){ static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4; gsize len = 1; if (!format) return len; while (*format) { register gchar c = *format++; if (c != '%') len += 1; else /* (c == '%') */ { PrintfArgSpec spec = { 0, }; gboolean seen_l = FALSE, conv_done = FALSE; gsize conv_len = 0; const gchar *spec_start = format; do { c = *format++; switch (c) { GDoubleIEEE754 u_double; guint v_uint; gint v_int; const gchar *v_string; /* beware of positional parameters */ case '$': if (may_warn) g_warning (G_GNUC_PRETTY_FUNCTION "(): unable to handle positional parameters (%%n$)"); len += 1024; /* try adding some safety padding */ conv_done = TRUE; break; /* parse flags */ case '#': spec.alternate_format = TRUE; break; case '0': spec.zero_padding = TRUE; break; case '-': spec.adjust_left = TRUE; break; case ' ': spec.add_space = TRUE; break; case '+': spec.add_sign = TRUE; break; case '\'': spec.locale_grouping = TRUE; break; /* parse output size specifications */ case '.': spec.seen_precision = TRUE; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': v_uint = c - '0'; c = *format; while (c >= '0' && c <= '9') { format++; v_uint = v_uint * 10 + c - '0'; c = *format; } if (spec.seen_precision) spec.precision = MAX (spec.precision, v_uint); else spec.min_width = MAX (spec.min_width, v_uint); break; case '*': v_int = va_arg (args, int); if (spec.seen_precision) { /* forget about negative precision */ if (v_int >= 0) spec.precision = MAX (spec.precision, v_int); } else { if (v_int < 0) { v_int = - v_int; spec.adjust_left = TRUE; } spec.min_width = MAX (spec.min_width, v_int); } break; /* parse type modifiers */ case 'h': spec.mod_half = TRUE; break; case 'l': if (!seen_l) { spec.mod_long = TRUE; seen_l = TRUE; break; } /* else, fall through */ case 'L': case 'q': spec.mod_long = TRUE; spec.mod_extra_long = TRUE; break; case 'z': case 'Z':#if GLIB_SIZEOF_SIZE_T > 4 spec.mod_long = TRUE; spec.mod_extra_long = TRUE;#endif /* GLIB_SIZEOF_SIZE_T > 4 */ break; case 't':#if GLIB_SIZEOF_PTRDIFF_T > 4 spec.mod_long = TRUE; spec.mod_extra_long = TRUE;#endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */ break; case 'j':#if GLIB_SIZEOF_INTMAX_T > 4 spec.mod_long = TRUE; spec.mod_extra_long = TRUE;#endif /* GLIB_SIZEOF_INTMAX_T > 4 */ break; /* parse output conversions */ case '%': conv_len += 1; break; case 'O': case 'D': case 'I': case 'U': /* some C libraries feature long variants for these as well? */ spec.mod_long = TRUE; /* fall through */ case 'o': conv_len += 2; /* fall through */ case 'd': case 'i': conv_len += 1; /* sign */ /* fall through */ case 'u': conv_len += 4; /* fall through */ case 'x': case 'X': spec.possible_sign = TRUE; conv_len += 10; if (spec.mod_long && honour_longs) conv_len *= 2; if (spec.mod_extra_long) conv_len *= 2; if (spec.mod_extra_long) { (void) va_arg (args, gint64); } else if (spec.mod_long) (void) va_arg (args, long); else (void) va_arg (args, int); break; case 'A': case 'a': /* 0x */ conv_len += 2; /* fall through */ case 'g': case 'G': case 'e': case 'E': case 'f': spec.possible_sign = TRUE; /* n . dddddddddddddddddddddddd E +- eeee */ conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4; if (may_warn && spec.mod_extra_long) g_warning (G_GNUC_PRETTY_FUNCTION "(): unable to handle long double, collecting double only");#ifdef HAVE_LONG_DOUBLE#error need to implement special handling for long double#endif u_double.v_double = va_arg (args, double); /* %f can expand up to all significant digits before '.' (308) */ if (c == 'f' && u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047) { gint exp = u_double.mpn.biased_exponent; exp -= G_IEEE754_DOUBLE_BIAS; exp = exp * G_LOG_2_BASE_10 + 1; conv_len += ABS (exp); /* exp can be <0 */ } /* some printf() implementations require extra padding for rounding */ conv_len += 2; /* we can't really handle locale specific grouping here */ if (spec.locale_grouping) conv_len *= 2; break; case 'C': spec.mod_long = TRUE; /* fall through */ case 'c': conv_len += spec.mod_long ? MB_LEN_MAX : 1; (void) va_arg (args, int); break; case 'S': spec.mod_long = TRUE; /* fall through */ case 's': v_string = va_arg (args, char*); if (!v_string) conv_len += 8; /* hold "(null)" */ else if (spec.seen_precision) conv_len += spec.precision; else conv_len += strlen (v_string); conv_done = TRUE; if (spec.mod_long) { if (may_warn) g_warning (G_GNUC_PRETTY_FUNCTION "(): unable to handle wide char strings"); len += 1024; /* try adding some safety padding */ } break; case 'P': /* do we actually need this? */ /* fall through */ case 'p': spec.alternate_format = TRUE; conv_len += 10; if (honour_longs) conv_len *= 2; /* fall through */ case 'n': conv_done = TRUE; (void) va_arg (args, void*); break; case 'm': /* there's not much we can do to be clever */ v_string = g_strerror (errno); v_uint = v_string ? strlen (v_string) : 0; conv_len += MAX (256, v_uint); break; /* handle invalid cases */ case '\000': /* no conversion specification, bad bad */ conv_len += format - spec_start; break; default: if (may_warn) g_warning (G_GNUC_PRETTY_FUNCTION "(): unable to handle `%c' while parsing format", c); break; } conv_done |= conv_len > 0; } while (!conv_done); /* handle width specifications */ conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width)); /* handle flags */ conv_len += spec.alternate_format ? 2 : 0; conv_len += (spec.add_space || spec.add_sign || spec.possible_sign); /* finally done */ len += conv_len; } /* else (c == '%') */ } /* while (*format) */ return len;}#endif /* !HAVE_C99_VSNPRINTF */gsizeg_printf_string_upper_bound (const gchar *format, va_list args){#if HAVE_C99_VSNPRINTF gchar c; return vsnprintf (&c, 1, format, args) + 1;#else return printf_string_upper_bound (format, TRUE, args);#endif}voidg_messages_init (void){ g_messages_lock = g_mutex_new (); g_log_depth = g_private_new (NULL); g_messages_prefixed_init (); _g_debug_init ();}gboolean _g_debug_initialized = FALSE;guint _g_debug_flags = 0;void_g_debug_init (void) { const gchar *val; _g_debug_initialized = TRUE; val = g_getenv ("G_DEBUG"); if (val != NULL) { static const GDebugKey keys[] = { {"fatal_warnings", G_DEBUG_FATAL_WARNINGS} }; _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys)); } if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS) { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; g_log_set_always_fatal (fatal_mask); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -