Exemplo n.º 1
0
    /* 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 (NULL);
    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
    {
        GString *msg;
        const gchar *charset;

        msg = g_string_new (message);
        escape_string (msg);

        if (g_get_charset (&charset))
            g_string_append (gstring, msg->str);	/* charset is UTF-8 already */
        else
        {
            string = strdup_convert (msg->str, charset);
            g_string_append (gstring, string);
            g_free (string);
        }

        g_string_free (msg, TRUE);
    }
    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);
}

GPrintFunc
g_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;
}

void
g_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
    {
        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);
        }
        fflush (stdout);
    }
    g_free (string);
}

GPrintFunc
g_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;
}

void
g_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
    {
        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);
        }
        fflush (stderr);
    }
    g_free (string);
}

#endif /* NOT_NEEDED_FOR_NAVIT */
gsize
g_printf_string_upper_bound (const gchar *format,
                             va_list      args)
{
#ifdef HAVE_API_WIN32_BASE
    gchar c[16384];
    return _g_vsnprintf (c, 16384, format, args) + 1;
#else
    gchar c;
    return _g_vsnprintf (&c, 1, format, args) + 1;
#endif
}
Exemplo n.º 2
0
gsize
g_printf_string_upper_bound (const gchar *format,
			     va_list      args)
{
  gchar c;
  return _g_vsnprintf (&c, 1, format, args) + 1;
}
Exemplo n.º 3
0
/** 
 * g_vsnprintf:
 * @string: the buffer to hold the output.
 * @n: the maximum number of bytes to produce (including the 
 *     terminating nul character).
 * @format: a standard printf() format string, but notice 
 *          <link linkend="string-precision">string precision pitfalls</link>.
 * @args: the list of arguments to insert in the output.
 *
 * A safer form of the standard vsprintf() function. The output is guaranteed
 * to not exceed @n characters (including the terminating nul character), so 
 * it is easy to ensure that a buffer overflow cannot occur.
 *
 * See also g_strdup_vprintf().
 *
 * In versions of GLib prior to 1.2.3, this function may return -1 if the 
 * output was truncated, and the truncated string may not be nul-terminated.
 * In versions prior to 1.3.12, this function returns the length of the output 
 * string.
 *
 * The return value of g_vsnprintf() conforms to the vsnprintf() function 
 * as standardized in ISO C99. Note that this is different from traditional 
 * vsnprintf(), which returns the length of the output string.
 *
 * The format string may contain positional parameters, as specified in 
 * the Single Unix Specification.
 *
 * Returns: the number of bytes which would be produced if the buffer 
 *  was large enough.
 */
gint
g_vsnprintf (gchar	 *string,
	     gulong	  n,
	     gchar const *format,
	     va_list      args)
{
  g_return_val_if_fail (n == 0 || string != NULL, -1);
  g_return_val_if_fail (format != NULL, -1);

  return _g_vsnprintf (string, n, format, args);
}
Exemplo n.º 4
0
gsize
g_printf_string_upper_bound (const gchar *format,
			     va_list      args)
{
  gchar c;

#ifdef G_PLATFORM_WIN32_CE
  return _vsnprintf (&c, 1, format, args) + 1;
#else
  return _g_vsnprintf (&c, 1, format, args) + 1;
#endif
}
Exemplo n.º 5
0
void
g_logv (const gchar   *log_domain,
	GLogLevelFlags log_level,
	const gchar   *format,
	va_list	       args1)
{
  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
  gint i;

  log_level &= G_LOG_LEVEL_MASK;
  if (!log_level)
    return;

  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
    {
      register GLogLevelFlags test_level;

      test_level = 1 << i;
      if (log_level & test_level)
	{
	  guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
	  GLogDomain *domain;
	  GLogFunc log_func;
	  GLogLevelFlags domain_fatal_mask;
	  gpointer data = NULL;
          gboolean masquerade_fatal = FALSE;

	  if (was_fatal)
	    test_level |= G_LOG_FLAG_FATAL;
	  if (was_recursion)
	    test_level |= G_LOG_FLAG_RECURSION;

	  /* check recursion and lookup handler */
	  g_mutex_lock (g_messages_lock);
	  domain = g_log_find_domain_L (log_domain ? log_domain : "");
	  if (depth)
	    test_level |= G_LOG_FLAG_RECURSION;
	  depth++;
	  domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
	  if ((domain_fatal_mask | g_log_always_fatal) & test_level)
	    test_level |= G_LOG_FLAG_FATAL;
	  if (test_level & G_LOG_FLAG_RECURSION)
	    log_func = _g_log_fallback_handler;
	  else
	    log_func = g_log_domain_get_handler_L (domain, test_level, &data);
	  domain = NULL;
	  g_mutex_unlock (g_messages_lock);

	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));

	  /* had to defer debug initialization until we can keep track of recursion */
	  if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
	    {
	      GLogLevelFlags orig_test_level = test_level;

	      _g_debug_init ();
	      if ((domain_fatal_mask | g_log_always_fatal) & test_level)
		test_level |= G_LOG_FLAG_FATAL;
	      if (test_level != orig_test_level)
		{
		  /* need a relookup, not nice, but not too bad either */
		  g_mutex_lock (g_messages_lock);
		  domain = g_log_find_domain_L (log_domain ? log_domain : "");
		  log_func = g_log_domain_get_handler_L (domain, test_level, &data);
		  domain = NULL;
		  g_mutex_unlock (g_messages_lock);
		}
	    }

	  if (test_level & G_LOG_FLAG_RECURSION)
	    {
	      /* we use a stack buffer of fixed size, since we're likely
	       * in an out-of-memory situation
	       */
	      gchar buffer[1025];
              gsize size;
              va_list args2;

              G_VA_COPY (args2, args1);
	      size = _g_vsnprintf (buffer, 1024, format, args2);
              va_end (args2);

	      log_func (log_domain, test_level, buffer, data);
	    }
	  else
	    {
	      gchar *msg;
              va_list args2;

              G_VA_COPY (args2, args1);
              msg = g_strdup_vprintf (format, args2);
              va_end (args2);

	      log_func (log_domain, test_level, msg, data);

              if ((test_level & G_LOG_FLAG_FATAL)
                && !(test_level & G_LOG_LEVEL_ERROR))
                {
                  masquerade_fatal = fatal_log_func
                    && !fatal_log_func (log_domain, test_level, msg, data);
                }

	      g_free (msg);
	    }

	 gchar *locale_msg2 = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);

              MessageBox (NULL, locale_msg2, L"G_LOGV",
                          MB_ICONERROR|MB_SETFOREGROUND);

	  if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
            {
#ifdef G_OS_WIN32
	      gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
	      
	      MessageBox (NULL, locale_msg, NULL,
			  MB_ICONERROR|MB_SETFOREGROUND);
#ifndef G_PLATFORM_WIN32_CE
	      if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
		G_BREAKPOINT ();
	      else
		abort ();
#else
		abort ();
#endif


#else
#if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
	      if (!(test_level & G_LOG_FLAG_RECURSION))
		G_BREAKPOINT ();
	      else
		abort ();
#else /* !G_ENABLE_DEBUG || !SIGTRAP */
	      abort ();
#endif /* !G_ENABLE_DEBUG || !SIGTRAP */
#endif /* !G_OS_WIN32 */
	    }
	  
	  depth--;
	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
	}
    }
}