virterror.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/*
 * virterror.c: implements error handling and reporting code for libvirt
 *
 * Copy:  Copyright (C) 2006 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libvirt.h"
#include "virterror.h"
#include "internal.h"

static virError     lastErr = 		/* the last error */
{ 0, 0, NULL, VIR_ERR_NONE, NULL, NULL, NULL, NULL, NULL, 0, 0};
static virErrorFunc virErrorHandler = NULL;/* global error handlet */
static void        *virUserData = NULL;	/* associated data */

/*
 * virGetLastError:
 *
 * Provide a pointer to the last error caught at the library level
 * Simpler but may not be suitable for multithreaded accesses, in which
 * case use virCopyLastError()
 *
 * Returns a pointer to the last error or NULL if none occured.
 */
virErrorPtr
virGetLastError(void) {
    if (lastErr.code == VIR_ERR_OK)
        return(NULL);
    return(&lastErr);
}

/*
 * virCopyLastError:
 * @to: target to receive the copy
 *
 * Copy the content of the last error caught at the library level
 * One will need to free the result with virResetError()
 *
 * Returns 0 if no error was found and the error code otherwise and -1 in case
 *         of parameter error.
 */
int
virCopyLastError(virErrorPtr to) {
    if (to == NULL)
        return(-1);
    if (lastErr.code == VIR_ERR_OK)
        return(0);
    memcpy(to, &lastErr, sizeof(virError));
    return(lastErr.code);
}

/**
 * virResetError:
 * @err: pointer to the virError to clean up
 * 
 * Reset the error being pointed to
 */
void
virResetError(virErrorPtr err) {
    if (err == NULL)
        return;
    if (err->message != NULL)
        free(err->message);
    if (err->str1 != NULL)
        free(err->str1);
    if (err->str2 != NULL)
        free(err->str2);
    if (err->str3 != NULL)
        free(err->str3);
    memset(err, 0, sizeof(virError));
}

/**
 * virResetLastError:
 * 
 * Reset the last error caught at the library level.
 */
void
virResetLastError(void) {
    virResetError(&lastErr);
}

/**
 * virConnGetLastError:
 * @conn: pointer to the hypervisor connection
 *
 * Provide a pointer to the last error caught on that connection
 * Simpler but may not be suitable for multithreaded accesses, in which
 * case use virConnCopyLastError()
 *
 * Returns a pointer to the last error or NULL if none occured.
 */
virErrorPtr
virConnGetLastError(virConnectPtr conn) {
    if (conn == NULL)
	return(NULL);
    return(&conn->err);
}

/**
 * virConnCopyLastError:
 * @conn: pointer to the hypervisor connection
 * @to: target to receive the copy
 *
 * Copy the content of the last error caught on that connection
 * One will need to free the result with virResetError()
 *
 * Returns 0 if no error was found and the error code otherwise and -1 in case
 *         of parameter error.
 */
int
virConnCopyLastError(virConnectPtr conn, virErrorPtr to) {
    if (conn == NULL)
	return(-1);
    if (to == NULL)
        return(-1);
    if (conn->err.code == VIR_ERR_OK)
        return(0);
    memcpy(to, &conn->err, sizeof(virError));
    return(conn->err.code);
}

/**
 * virConnResetLastError:
 * @conn: pointer to the hypervisor connection
 *
 * Reset the last error caught on that connection
 */
void
virConnResetLastError(virConnectPtr conn) {
    if (conn == NULL)
        return;
    virResetError(&conn->err);
}

/**
 * virSetErrorFunc:
 * @userData: pointer to the user data provided in the handler callback
 * @handler: the function to get called in case of error or NULL
 *
 * Set a library global error handling function, if @handler is NULL,
 * it will reset to default printing on stderr. The error raised there
 * are those for which no handler at the connection level could caught.
 */
void
virSetErrorFunc(void *userData, virErrorFunc handler) {
    virErrorHandler = handler;
    virUserData = userData;
}

/**
 * virConnSetErrorFunc:
 * @conn: pointer to the hypervisor connection
 * @userData: pointer to the user data provided in the handler callback
 * @handler: the function to get called in case of error or NULL
 *
 * Set a connection error handling function, if @handler is NULL
 * it will reset to default which is to pass error back to the global
 * library handler.
 */
void
virConnSetErrorFunc(virConnectPtr conn, void *userData, virErrorFunc handler) {
    if (conn == NULL)
        return;
    conn->handler = handler;
    conn->userData = userData;
}