virerror.h 8.1 KB
Newer Older
1
/*
2
 * virerror.h: error handling and reporting code for libvirt
3
 *
4
 * Copyright (C) 2006-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21
 *
 */

22 23
#ifndef LIBVIRT_VIRERROR_H
# define LIBVIRT_VIRERROR_H
24

25
# include "internal.h"
26
# include "virautoclean.h"
27

28 29
# define VIR_ERROR_MAX_LENGTH 1024

30 31 32
extern virErrorFunc virErrorHandler;
extern void *virUserData;

33
int virErrorInitialize(void);
34
void virRaiseErrorFull(const char *filename,
D
Daniel P. Berrange 已提交
35 36 37 38 39 40 41 42 43 44 45
                       const char *funcname,
                       size_t linenr,
                       int domain,
                       int code,
                       virErrorLevel level,
                       const char *str1,
                       const char *str2,
                       const char *str3,
                       int int1,
                       int int2,
                       const char *fmt, ...)
46
    ATTRIBUTE_FMT_PRINTF(12, 13);
D
Daniel P. Berrange 已提交
47

48 49 50 51 52
void virRaiseErrorObject(const char *filename,
                         const char *funcname,
                         size_t linenr,
                         virErrorPtr err);

53
void virReportErrorHelper(int domcode, int errcode,
54 55 56
                          const char *filename,
                          const char *funcname,
                          size_t linenr,
57
                          const char *fmt, ...)
58
  ATTRIBUTE_FMT_PRINTF(6, 7);
59

60
void virReportSystemErrorFull(int domcode,
61 62 63 64 65
                              int theerrno,
                              const char *filename,
                              const char *funcname,
                              size_t linenr,
                              const char *fmt, ...)
66
    ATTRIBUTE_FMT_PRINTF(6, 7);
67

68 69 70 71
# define virReportSystemError(theerrno, fmt,...) \
    virReportSystemErrorFull(VIR_FROM_THIS, \
                             (theerrno), \
                             __FILE__, __FUNCTION__, __LINE__, \
72 73
                             (fmt), __VA_ARGS__)

74 75 76 77 78 79 80 81 82 83
# define virReportInvalidNullArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must be NULL"), \
84
                      #argname, __FUNCTION__)
85 86 87 88 89 90 91 92 93 94
# define virReportInvalidNonNullArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must not be NULL"), \
95
                      #argname, __FUNCTION__)
96 97 98 99 100 101 102 103 104 105
# define virReportInvalidEmptyStringArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("string %s in %s must not be empty"), \
106
                      #argname, __FUNCTION__)
107 108 109 110 111 112 113 114 115 116
# define virReportInvalidPositiveArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must be greater than zero"), \
117
                      #argname, __FUNCTION__)
118 119 120 121 122 123 124 125 126 127
# define virReportInvalidNonZeroArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must not be zero"), \
128
                      #argname, __FUNCTION__)
129 130 131 132 133 134 135 136 137 138
# define virReportInvalidZeroArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must be zero"), \
139
                      #argname, __FUNCTION__)
140 141 142 143 144 145 146 147 148 149
# define virReportInvalidNonNegativeArg(argname) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
                      _("%s in %s must be zero or greater"), \
150
                      #argname, __FUNCTION__)
151 152 153 154 155 156 157 158 159
# define virReportInvalidArg(argname, fmt, ...) \
    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__, \
                      VIR_FROM_THIS, \
                      VIR_ERR_INVALID_ARG, \
                      VIR_ERR_ERROR, \
                      __FUNCTION__, \
                      #argname, \
                      NULL, \
                      0, 0, \
160 161
                      (fmt), __VA_ARGS__)

162 163
# define virReportUnsupportedError() \
    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_NO_SUPPORT, \
164
                         __FILE__, __FUNCTION__, __LINE__, __FUNCTION__)
165 166
# define virReportRestrictedError(...) \
    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_OPERATION_DENIED, \
167
                         __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
168 169 170 171 172 173 174 175 176
/* The sizeof(...) comparison here is a hack to catch typos
 * in the name of the enum by triggering a compile error, as well
 * as detecting if you passed a typename that refers to a function
 * or struct type, instead of an enum. It should get optimized away
 * since sizeof() is known at compile time  */
# define virReportEnumRangeError(typname, value) \
    virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INTERNAL_ERROR, \
                         __FILE__, __FUNCTION__, __LINE__, \
                         "Unexpected enum value %d for %s", \
177
                         value, sizeof((typname)1) != 0 ? #typname : #typname);
178

179
void virReportOOMErrorFull(int domcode,
180 181 182 183
                           const char *filename,
                           const char *funcname,
                           size_t linenr);

184
# define virReportOOMError() \
185
    virReportOOMErrorFull(VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
186

187 188
# define virReportError(code, ...) \
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
189
                         __FUNCTION__, __LINE__, __VA_ARGS__)
190

191
# define virReportErrorObject(obj) \
192 193
    virRaiseErrorObject(__FILE__, __FUNCTION__, __LINE__, obj)

C
Cole Robinson 已提交
194
int virSetError(virErrorPtr newerr);
J
Jiri Denemark 已提交
195
virErrorPtr virErrorCopyNew(virErrorPtr err);
196
void virDispatchError(virConnectPtr conn);
J
Jim Meyering 已提交
197
const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen);
198

199 200 201
typedef int (*virErrorLogPriorityFunc)(virErrorPtr, int);
void virSetErrorLogPriorityFunc(virErrorLogPriorityFunc func);

202 203 204 205
void virErrorSetErrnoFromLastError(void);

bool virLastErrorIsSystemErrno(int errnum);

206 207 208
void virErrorPreserveLast(virErrorPtr *saveerr);
void virErrorRestore(virErrorPtr *savederr);

209
VIR_DEFINE_AUTOPTR_FUNC(virError, virFreeError);
210

211
#endif /* LIBVIRT_VIRERROR_H */