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:
Amy Griffis 2009-08-06 15:55:07 +02:00 committed by Daniel Veillard
parent e542d52a7c
commit 63fbcc6927
6 changed files with 78 additions and 64 deletions

View File

@ -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:

View File

@ -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");

View File

@ -217,8 +217,10 @@ virLogGetNbFilters;
virLogGetNbOutputs;
virLogGetDefaultPriority;
virLogSetDefaultPriority;
virLogSetFromEnv;
virLogDefineFilter;
virLogDefineOutput;
virLogParseDefaultPriority;
virLogParseFilters;
virLogParseOutputs;
virLogStartup;

View File

@ -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));
}

View File

@ -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,

View File

@ -272,19 +272,9 @@ 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 {
fprintf(stderr, "Invalid log level setting.\n");
return EXIT_FAILURE;
}
if (debugEnv && *debugEnv && (virLogParseDefaultPriority(debugEnv) == -1)) {
fprintf(stderr, "Invalid log level setting.\n");
return EXIT_FAILURE;
}
virEventInit();