mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-25 01:34:11 +03:00
Consolidate code for parsing the logging env
* src/logging.c src/logging.h src/libvirt_private.syms: define new functions virLogSetFromEnv and virLogParseDefaultPriority * qemud/qemud.c src/libvirt.c tests/eventtest.c: cleanup to use the unified functions
This commit is contained in:
parent
e542d52a7c
commit
63fbcc6927
@ -2488,7 +2488,6 @@ remoteReadSaslAllowedUsernameList (virConfPtr conf ATTRIBUTE_UNUSED,
|
||||
*/
|
||||
static int
|
||||
qemudSetLogging(virConfPtr conf, const char *filename) {
|
||||
char *debugEnv;
|
||||
int log_level;
|
||||
char *log_filters = NULL;
|
||||
char *log_outputs = NULL;
|
||||
@ -2497,9 +2496,18 @@ qemudSetLogging(virConfPtr conf, const char *filename) {
|
||||
virLogReset();
|
||||
|
||||
/*
|
||||
* look for default logging level first from config file,
|
||||
* then from environment variable and finally from command
|
||||
* line options
|
||||
* Libvirtd's order of precedence is:
|
||||
* cmdline > environment > config
|
||||
*
|
||||
* In order to achieve this, we must process configuration in
|
||||
* different order for the log level versus the filters and
|
||||
* outputs. Because filters and outputs append, we have to look at
|
||||
* the environment first and then only check the config file if
|
||||
* there was no result from the environment. The default output is
|
||||
* then applied only if there was no setting from either of the
|
||||
* first two. Because we don't have a way to determine if the log
|
||||
* level has been set, we must process variables in the opposite
|
||||
* order, each one overriding the previous.
|
||||
*/
|
||||
/*
|
||||
* GET_CONF_INT returns 0 when there is no log_level setting in
|
||||
@ -2511,38 +2519,13 @@ qemudSetLogging(virConfPtr conf, const char *filename) {
|
||||
if (log_level != 0)
|
||||
virLogSetDefaultPriority(log_level);
|
||||
|
||||
debugEnv = getenv("LIBVIRT_DEBUG");
|
||||
if (debugEnv && *debugEnv) {
|
||||
if (STREQ(debugEnv, "1") || STREQ(debugEnv, "debug"))
|
||||
virLogSetDefaultPriority(VIR_LOG_DEBUG);
|
||||
else if (STREQ(debugEnv, "2") || STREQ(debugEnv, "info"))
|
||||
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
else if (STREQ(debugEnv, "3") || STREQ(debugEnv, "warning"))
|
||||
virLogSetDefaultPriority(VIR_LOG_WARN);
|
||||
else if (STREQ(debugEnv, "4") || STREQ(debugEnv, "error"))
|
||||
virLogSetDefaultPriority(VIR_LOG_ERROR);
|
||||
else
|
||||
VIR_WARN0(_("Ignoring invalid log level setting."));
|
||||
}
|
||||
|
||||
if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
|
||||
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
|
||||
debugEnv = getenv("LIBVIRT_LOG_FILTERS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseFilters(strdup(debugEnv));
|
||||
virLogSetFromEnv();
|
||||
|
||||
if (virLogGetNbFilters() == 0) {
|
||||
GET_CONF_STR (conf, filename, log_filters);
|
||||
virLogParseFilters(log_filters);
|
||||
}
|
||||
|
||||
/* there is no default filters */
|
||||
|
||||
debugEnv = getenv("LIBVIRT_LOG_OUTPUTS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseOutputs(strdup(debugEnv));
|
||||
|
||||
if (virLogGetNbOutputs() == 0) {
|
||||
GET_CONF_STR (conf, filename, log_outputs);
|
||||
virLogParseOutputs(log_outputs);
|
||||
@ -2566,6 +2549,13 @@ qemudSetLogging(virConfPtr conf, const char *filename) {
|
||||
virLogParseOutputs(tmp);
|
||||
VIR_FREE(tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Command line override for --verbose
|
||||
*/
|
||||
if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
|
||||
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
|
||||
ret = 0;
|
||||
|
||||
free_and_fail:
|
||||
|
@ -262,7 +262,6 @@ winsock_init (void)
|
||||
int
|
||||
virInitialize(void)
|
||||
{
|
||||
char *debugEnv;
|
||||
if (initialized)
|
||||
return(0);
|
||||
|
||||
@ -273,26 +272,7 @@ virInitialize(void)
|
||||
virRandomInitialize(time(NULL) ^ getpid()))
|
||||
return -1;
|
||||
|
||||
debugEnv = getenv("LIBVIRT_DEBUG");
|
||||
if (debugEnv && *debugEnv) {
|
||||
if (STREQ(debugEnv, "1") || STREQ(debugEnv, "debug"))
|
||||
virLogSetDefaultPriority(VIR_LOG_DEBUG);
|
||||
else if (STREQ(debugEnv, "2") || STREQ(debugEnv, "info"))
|
||||
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
else if (STREQ(debugEnv, "3") || STREQ(debugEnv, "warning"))
|
||||
virLogSetDefaultPriority(VIR_LOG_WARN);
|
||||
else if (STREQ(debugEnv, "4") || STREQ(debugEnv, "error"))
|
||||
virLogSetDefaultPriority(VIR_LOG_ERROR);
|
||||
else
|
||||
VIR_WARN0(_("Ignoring invalid log level setting."));
|
||||
}
|
||||
debugEnv = getenv("LIBVIRT_LOG_FILTERS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseFilters(debugEnv);
|
||||
|
||||
debugEnv = getenv("LIBVIRT_LOG_OUTPUTS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseOutputs(debugEnv);
|
||||
virLogSetFromEnv();
|
||||
|
||||
DEBUG0("register drivers");
|
||||
|
||||
|
@ -217,8 +217,10 @@ virLogGetNbFilters;
|
||||
virLogGetNbOutputs;
|
||||
virLogGetDefaultPriority;
|
||||
virLogSetDefaultPriority;
|
||||
virLogSetFromEnv;
|
||||
virLogDefineFilter;
|
||||
virLogDefineOutput;
|
||||
virLogParseDefaultPriority;
|
||||
virLogParseFilters;
|
||||
virLogParseOutputs;
|
||||
virLogStartup;
|
||||
|
@ -829,3 +829,53 @@ int virLogGetNbFilters(void) {
|
||||
int virLogGetNbOutputs(void) {
|
||||
return (virLogNbOutputs);
|
||||
}
|
||||
|
||||
/**
|
||||
* virLogParseDefaultPriority:
|
||||
* @priority: string defining the desired logging level
|
||||
*
|
||||
* Parses and sets the default log priority level. It can take a string or
|
||||
* number corresponding to the following levels:
|
||||
* 1: DEBUG
|
||||
* 2: INFO
|
||||
* 3: WARNING
|
||||
* 4: ERROR
|
||||
*
|
||||
* Returns the parsed log level or -1 on error.
|
||||
*/
|
||||
int virLogParseDefaultPriority(const char *priority) {
|
||||
int ret = -1;
|
||||
|
||||
if (STREQ(priority, "1") || STREQ(priority, "debug"))
|
||||
ret = virLogSetDefaultPriority(VIR_LOG_DEBUG);
|
||||
else if (STREQ(priority, "2") || STREQ(priority, "info"))
|
||||
ret = virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
else if (STREQ(priority, "3") || STREQ(priority, "warning"))
|
||||
ret = virLogSetDefaultPriority(VIR_LOG_WARN);
|
||||
else if (STREQ(priority, "4") || STREQ(priority, "error"))
|
||||
ret = virLogSetDefaultPriority(VIR_LOG_ERROR);
|
||||
else
|
||||
VIR_WARN0(_("Ignoring invalid log level setting"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* virLogSetFromEnv:
|
||||
*
|
||||
* Sets virLogDefaultPriority, virLogFilters and virLogOutputs based on
|
||||
* environment variables.
|
||||
*/
|
||||
void virLogSetFromEnv(void) {
|
||||
char *debugEnv;
|
||||
|
||||
debugEnv = getenv("LIBVIRT_DEBUG");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseDefaultPriority(debugEnv);
|
||||
debugEnv = getenv("LIBVIRT_LOG_FILTERS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseFilters(strdup(debugEnv));
|
||||
debugEnv = getenv("LIBVIRT_LOG_OUTPUTS");
|
||||
if (debugEnv && *debugEnv)
|
||||
virLogParseOutputs(strdup(debugEnv));
|
||||
}
|
||||
|
@ -109,6 +109,7 @@ extern int virLogGetNbFilters(void);
|
||||
extern int virLogGetNbOutputs(void);
|
||||
extern int virLogGetDefaultPriority(void);
|
||||
extern int virLogSetDefaultPriority(int priority);
|
||||
extern void virLogSetFromEnv(void);
|
||||
extern int virLogDefineFilter(const char *match, int priority, int flags);
|
||||
extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c,
|
||||
void *data, int priority, int flags);
|
||||
@ -120,6 +121,7 @@ extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c,
|
||||
extern int virLogStartup(void);
|
||||
extern int virLogReset(void);
|
||||
extern void virLogShutdown(void);
|
||||
extern int virLogParseDefaultPriority(const char *priority);
|
||||
extern int virLogParseFilters(const char *filters);
|
||||
extern int virLogParseOutputs(const char *output);
|
||||
extern void virLogMessage(const char *category, int priority,
|
||||
|
@ -272,20 +272,10 @@ mymain(int argc, char **argv)
|
||||
if (virThreadInitialize() < 0)
|
||||
return EXIT_FAILURE;
|
||||
char *debugEnv = getenv("LIBVIRT_DEBUG");
|
||||
if (debugEnv && *debugEnv) {
|
||||
if (STREQ(debugEnv, "1") || STREQ(debugEnv, "debug"))
|
||||
virLogSetDefaultPriority(VIR_LOG_DEBUG);
|
||||
else if (STREQ(debugEnv, "2") || STREQ(debugEnv, "info"))
|
||||
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||
else if (STREQ(debugEnv, "3") || STREQ(debugEnv, "warning"))
|
||||
virLogSetDefaultPriority(VIR_LOG_WARN);
|
||||
else if (STREQ(debugEnv, "4") || STREQ(debugEnv, "error"))
|
||||
virLogSetDefaultPriority(VIR_LOG_ERROR);
|
||||
else {
|
||||
if (debugEnv && *debugEnv && (virLogParseDefaultPriority(debugEnv) == -1)) {
|
||||
fprintf(stderr, "Invalid log level setting.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
virEventInit();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user