mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 17:34:18 +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
|
static int
|
||||||
qemudSetLogging(virConfPtr conf, const char *filename) {
|
qemudSetLogging(virConfPtr conf, const char *filename) {
|
||||||
char *debugEnv;
|
|
||||||
int log_level;
|
int log_level;
|
||||||
char *log_filters = NULL;
|
char *log_filters = NULL;
|
||||||
char *log_outputs = NULL;
|
char *log_outputs = NULL;
|
||||||
@ -2497,9 +2496,18 @@ qemudSetLogging(virConfPtr conf, const char *filename) {
|
|||||||
virLogReset();
|
virLogReset();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* look for default logging level first from config file,
|
* Libvirtd's order of precedence is:
|
||||||
* then from environment variable and finally from command
|
* cmdline > environment > config
|
||||||
* line options
|
*
|
||||||
|
* 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
|
* 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)
|
if (log_level != 0)
|
||||||
virLogSetDefaultPriority(log_level);
|
virLogSetDefaultPriority(log_level);
|
||||||
|
|
||||||
debugEnv = getenv("LIBVIRT_DEBUG");
|
virLogSetFromEnv();
|
||||||
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));
|
|
||||||
|
|
||||||
if (virLogGetNbFilters() == 0) {
|
if (virLogGetNbFilters() == 0) {
|
||||||
GET_CONF_STR (conf, filename, log_filters);
|
GET_CONF_STR (conf, filename, log_filters);
|
||||||
virLogParseFilters(log_filters);
|
virLogParseFilters(log_filters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* there is no default filters */
|
|
||||||
|
|
||||||
debugEnv = getenv("LIBVIRT_LOG_OUTPUTS");
|
|
||||||
if (debugEnv && *debugEnv)
|
|
||||||
virLogParseOutputs(strdup(debugEnv));
|
|
||||||
|
|
||||||
if (virLogGetNbOutputs() == 0) {
|
if (virLogGetNbOutputs() == 0) {
|
||||||
GET_CONF_STR (conf, filename, log_outputs);
|
GET_CONF_STR (conf, filename, log_outputs);
|
||||||
virLogParseOutputs(log_outputs);
|
virLogParseOutputs(log_outputs);
|
||||||
@ -2566,6 +2549,13 @@ qemudSetLogging(virConfPtr conf, const char *filename) {
|
|||||||
virLogParseOutputs(tmp);
|
virLogParseOutputs(tmp);
|
||||||
VIR_FREE(tmp);
|
VIR_FREE(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Command line override for --verbose
|
||||||
|
*/
|
||||||
|
if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
|
||||||
|
virLogSetDefaultPriority(VIR_LOG_INFO);
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
free_and_fail:
|
free_and_fail:
|
||||||
|
@ -262,7 +262,6 @@ winsock_init (void)
|
|||||||
int
|
int
|
||||||
virInitialize(void)
|
virInitialize(void)
|
||||||
{
|
{
|
||||||
char *debugEnv;
|
|
||||||
if (initialized)
|
if (initialized)
|
||||||
return(0);
|
return(0);
|
||||||
|
|
||||||
@ -273,26 +272,7 @@ virInitialize(void)
|
|||||||
virRandomInitialize(time(NULL) ^ getpid()))
|
virRandomInitialize(time(NULL) ^ getpid()))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
debugEnv = getenv("LIBVIRT_DEBUG");
|
virLogSetFromEnv();
|
||||||
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);
|
|
||||||
|
|
||||||
DEBUG0("register drivers");
|
DEBUG0("register drivers");
|
||||||
|
|
||||||
|
@ -217,8 +217,10 @@ virLogGetNbFilters;
|
|||||||
virLogGetNbOutputs;
|
virLogGetNbOutputs;
|
||||||
virLogGetDefaultPriority;
|
virLogGetDefaultPriority;
|
||||||
virLogSetDefaultPriority;
|
virLogSetDefaultPriority;
|
||||||
|
virLogSetFromEnv;
|
||||||
virLogDefineFilter;
|
virLogDefineFilter;
|
||||||
virLogDefineOutput;
|
virLogDefineOutput;
|
||||||
|
virLogParseDefaultPriority;
|
||||||
virLogParseFilters;
|
virLogParseFilters;
|
||||||
virLogParseOutputs;
|
virLogParseOutputs;
|
||||||
virLogStartup;
|
virLogStartup;
|
||||||
|
@ -829,3 +829,53 @@ int virLogGetNbFilters(void) {
|
|||||||
int virLogGetNbOutputs(void) {
|
int virLogGetNbOutputs(void) {
|
||||||
return (virLogNbOutputs);
|
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 virLogGetNbOutputs(void);
|
||||||
extern int virLogGetDefaultPriority(void);
|
extern int virLogGetDefaultPriority(void);
|
||||||
extern int virLogSetDefaultPriority(int priority);
|
extern int virLogSetDefaultPriority(int priority);
|
||||||
|
extern void virLogSetFromEnv(void);
|
||||||
extern int virLogDefineFilter(const char *match, int priority, int flags);
|
extern int virLogDefineFilter(const char *match, int priority, int flags);
|
||||||
extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c,
|
extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c,
|
||||||
void *data, int priority, int flags);
|
void *data, int priority, int flags);
|
||||||
@ -120,6 +121,7 @@ extern int virLogDefineOutput(virLogOutputFunc f, virLogCloseFunc c,
|
|||||||
extern int virLogStartup(void);
|
extern int virLogStartup(void);
|
||||||
extern int virLogReset(void);
|
extern int virLogReset(void);
|
||||||
extern void virLogShutdown(void);
|
extern void virLogShutdown(void);
|
||||||
|
extern int virLogParseDefaultPriority(const char *priority);
|
||||||
extern int virLogParseFilters(const char *filters);
|
extern int virLogParseFilters(const char *filters);
|
||||||
extern int virLogParseOutputs(const char *output);
|
extern int virLogParseOutputs(const char *output);
|
||||||
extern void virLogMessage(const char *category, int priority,
|
extern void virLogMessage(const char *category, int priority,
|
||||||
|
@ -272,19 +272,9 @@ mymain(int argc, char **argv)
|
|||||||
if (virThreadInitialize() < 0)
|
if (virThreadInitialize() < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
char *debugEnv = getenv("LIBVIRT_DEBUG");
|
char *debugEnv = getenv("LIBVIRT_DEBUG");
|
||||||
if (debugEnv && *debugEnv) {
|
if (debugEnv && *debugEnv && (virLogParseDefaultPriority(debugEnv) == -1)) {
|
||||||
if (STREQ(debugEnv, "1") || STREQ(debugEnv, "debug"))
|
fprintf(stderr, "Invalid log level setting.\n");
|
||||||
virLogSetDefaultPriority(VIR_LOG_DEBUG);
|
return EXIT_FAILURE;
|
||||||
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 {
|
|
||||||
fprintf(stderr, "Invalid log level setting.\n");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virEventInit();
|
virEventInit();
|
||||||
|
Loading…
Reference in New Issue
Block a user