spice-gtk注册glib错误信息

首先生成spice统一的错误域
#define SPICE_CLIENT_ERROR spice_client_error_quark()

GQuark spice_client_error_quark(void)
{
    return g_quark_from_static_string("spice-client-error-quark");
}

方法1:通过g_set_error()函数

g_set_error(err, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED, "Error attaching device: (no error information)"); 

void
g_set_error (GError      **err,
             GQuark        domain,//错误域
             gint          code,//错误码
             const gchar  *format,//错误描述
             ...)
{
  GError *new;
  va_list args;
  if (err == NULL)
    return;
  va_start (args, format);
  new = g_error_new_valist (domain, code, format, args);
  va_end (args);
  if (*err == NULL)
    *err = new;
  else{
      g_warning (ERROR_OVERWRITTEN_WARNING, new->message);
      g_error_free (new);
    }
}
最终函数g_error_new_steal 
GError*
g_error_new_valist (GQuark       domain,
                    gint         code,
                    const gchar *format,
                    va_list      args)
{
  g_return_val_if_fail (format != NULL, NULL);
  /* Historically, GError allowed this (although it was never meant to work),
   * and it has significant use in the wild, which g_return_val_if_fail
   * would break. It should maybe g_return_val_if_fail in GLib 4.
   * (GNOME#660371, GNOME#560482)
   */
  g_warn_if_fail (domain != 0);
  return g_error_new_steal (domain, code, g_strdup_vprintf (format, args), NULL);
}

方法2:通过g_error_new_literal()函数 

error = g_error_new_literal(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,_("File transfer is disabled."));

GError*
g_error_new_literal (GQuark         domain,
                     gint           code,
                     const gchar   *message)
{
  g_return_val_if_fail (message != NULL, NULL);
  g_return_val_if_fail (domain != 0, NULL);
  return g_error_new_steal (domain, code, g_strdup (message), NULL);
}

static GError *
g_error_new_steal (GQuark           domain,
                   gint             code,
                   gchar           *message,
                   ErrorDomainInfo *out_info)
{
  ErrorDomainInfo info;
  GError *error = g_error_allocate (domain, &info);
  error->domain = domain;
  error->code = code;
  error->message = message;
  if (info.init != NULL)
    info.init (error);
  if (out_info != NULL)
    *out_info = info;
  return error;
}

方法3:通过g_error_new()函数 

error = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,_("File transfer failed due to lack of free space on remote machine"));

GError*
g_error_new (GQuark       domain,
             gint         code,
             const gchar *format,
             ...)
{
  GError* error;
  va_list args;

  g_return_val_if_fail (format != NULL, NULL);
  g_return_val_if_fail (domain != 0, NULL);
  va_start (args, format);
  error = g_error_new_valist (domain, code, format, args);
  va_end (args);
  return error;
}

你可能感兴趣的:(spice,spice)