virlog: Introduce Type{To,From}String for virLogDestination

In order to refactor the ugly virLogParseOutputs method, this is a neat way of
finding out whether the destination type (in the form of a string) user
provided is a valid one. As a bonus, if it turns out it is valid, we get the
actual enum which will later be passed to any of virLogAddOutput methods right
away.
This commit is contained in:
Erik Skultety 2016-03-15 21:35:17 +01:00
parent 6bd9758e58
commit 034337fb85
2 changed files with 8 additions and 20 deletions

View File

@ -73,6 +73,10 @@ static regex_t *virLogRegex;
VIR_LOG_DATE_REGEX " " VIR_LOG_TIME_REGEX ": " \
VIR_LOG_PID_REGEX ": " VIR_LOG_LEVEL_REGEX " : "
VIR_ENUM_DECL(virLogDestination);
VIR_ENUM_IMPL(virLogDestination, VIR_LOG_TO_OUTPUT_LAST,
"stderr", "syslog", "file", "journald");
/*
* Filters are used to refine the rules on what to keep or drop
* based on a matching pattern (currently a substring)
@ -147,23 +151,6 @@ virLogUnlock(void)
}
static const char *
virLogOutputString(virLogDestination ldest)
{
switch (ldest) {
case VIR_LOG_TO_STDERR:
return "stderr";
case VIR_LOG_TO_SYSLOG:
return "syslog";
case VIR_LOG_TO_FILE:
return "file";
case VIR_LOG_TO_JOURNALD:
return "journald";
}
return "unknown";
}
static const char *
virLogPriorityString(virLogPriority lvl)
{
@ -1340,13 +1327,13 @@ virLogGetOutputs(void)
case VIR_LOG_TO_FILE:
virBufferAsprintf(&outputbuf, "%d:%s:%s",
virLogOutputs[i].priority,
virLogOutputString(dest),
virLogDestinationTypeToString(dest),
virLogOutputs[i].name);
break;
default:
virBufferAsprintf(&outputbuf, "%d:%s",
virLogOutputs[i].priority,
virLogOutputString(dest));
virLogDestinationTypeToString(dest));
}
}
virLogUnlock();

View File

@ -51,10 +51,11 @@ typedef enum {
# define VIR_LOG_DEFAULT VIR_LOG_WARN
typedef enum {
VIR_LOG_TO_STDERR = 1,
VIR_LOG_TO_STDERR = 0,
VIR_LOG_TO_SYSLOG,
VIR_LOG_TO_FILE,
VIR_LOG_TO_JOURNALD,
VIR_LOG_TO_OUTPUT_LAST,
} virLogDestination;
typedef struct _virLogSource virLogSource;