@@ -444,16 +444,22 @@ virParseVersionString(const char *str, unsigned long *version,
int virEnumFromString(const char *const*types,
unsigned int ntypes,
- const char *type)
+ const char *type,
+ const char * const label)
{
size_t i;
if (!type)
- return -1;
+ goto error;
for (i = 0; i < ntypes; i++)
if (STREQ(types[i], type))
return i;
+ error:
+ if (label) {
+ virReportError(VIR_ERR_INVALID_ARG,
+ _("Unknown '%s' value '%s'"), label, type);
+ }
return -1;
}
@@ -540,10 +546,16 @@ virFormatIntPretty(unsigned long long val,
const char *virEnumToString(const char *const*types,
unsigned int ntypes,
- int type)
+ int type,
+ const char * const label)
{
- if (type < 0 || type >= ntypes)
+ if (type < 0 || type >= ntypes) {
+ if (label) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unknown '%s' internal value %d"), label, type);
+ }
return NULL;
+ }
return types[type];
}
@@ -76,26 +76,31 @@ char *virIndexToDiskName(int idx, const char *prefix);
int virEnumFromString(const char *const*types,
unsigned int ntypes,
- const char *type);
+ const char *type,
+ const char *errmsg);
const char *virEnumToString(const char *const*types,
unsigned int ntypes,
- int type);
+ int type,
+ const char *errmsg);
-# define VIR_ENUM_IMPL(name, lastVal, ...) \
+# define VIR_ENUM_IMPL_LABEL(name, label, lastVal, ...) \
static const char *const name ## TypeList[] = { __VA_ARGS__ }; \
verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal); \
const char *name ## TypeToString(int type) { \
return virEnumToString(name ## TypeList, \
ARRAY_CARDINALITY(name ## TypeList), \
- type); \
+ type, label); \
} \
int name ## TypeFromString(const char *type) { \
return virEnumFromString(name ## TypeList, \
ARRAY_CARDINALITY(name ## TypeList), \
- type); \
+ type, label); \
}
+# define VIR_ENUM_IMPL(name, lastVal, ...) \
+ VIR_ENUM_IMPL_LABEL(name, NULL, lastVal, __VA_ARGS__)
+
# define VIR_ENUM_DECL(name) \
const char *name ## TypeToString(int type); \
int name ## TypeFromString(const char*type);
This allows passing in a string label describing the enum, which can be used to autogenerate error messages Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/util/virutil.c | 20 ++++++++++++++++---- src/util/virutil.h | 15 ++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) -- 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list