Re: [libvirt] [PATCH 4/5] Consolidate code for parsing the logging environment variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jul 31, 2009 at 05:57:42PM -0400, Amy Griffis wrote:
> Add two new functions to the internal API,
> virLogParseDefaultPriority() and virLogSetFromEnv(),
> as was suggested earlier by Cole Robinson.
> ---
> 
>  qemud/qemud.c            |   50 ++++++++++++++++++----------------------------
>  src/libvirt.c            |   22 +-------------------
>  src/libvirt_private.syms |    2 ++
>  src/logging.c            |   50 ++++++++++++++++++++++++++++++++++++++++++++++
>  src/logging.h            |    2 ++
>  tests/eventtest.c        |   16 +++------------
>  6 files changed, 78 insertions(+), 64 deletions(-)

ACK, good to remove this duplicated code

Daniel

> 
> diff --git a/qemud/qemud.c b/qemud/qemud.c
> index 65c07d9..e6662e4 100644
> --- a/qemud/qemud.c
> +++ b/qemud/qemud.c
> @@ -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:
> diff --git a/src/libvirt.c b/src/libvirt.c
> index 9e96410..19bffb1 100644
> --- a/src/libvirt.c
> +++ b/src/libvirt.c
> @@ -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");
>  
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 487585c..f20e5ce 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -217,8 +217,10 @@ virLogGetNbFilters;
>  virLogGetNbOutputs;
>  virLogGetDefaultPriority;
>  virLogSetDefaultPriority;
> +virLogSetFromEnv;
>  virLogDefineFilter;
>  virLogDefineOutput;
> +virLogParseDefaultPriority;
>  virLogParseFilters;
>  virLogParseOutputs;
>  virLogStartup;
> diff --git a/src/logging.c b/src/logging.c
> index aad4b50..b2cf0bf 100644
> --- a/src/logging.c
> +++ b/src/logging.c
> @@ -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)); 
> +}
> diff --git a/src/logging.h b/src/logging.h
> index c8698e5..8b2b84c 100644
> --- a/src/logging.h
> +++ b/src/logging.h
> @@ -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,
> diff --git a/tests/eventtest.c b/tests/eventtest.c
> index 05fe3f3..d25381d 100644
> --- a/tests/eventtest.c
> +++ b/tests/eventtest.c
> @@ -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();
> 
> --
> Libvir-list mailing list
> Libvir-list@xxxxxxxxxx
> https://www.redhat.com/mailman/listinfo/libvir-list

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]