[libvirt] [PATCH v2 09/10] admin: Add an example demonstrating how to use the logging APIs

John Ferlan jferlan at redhat.com
Fri Dec 9 12:36:28 UTC 2016



On 11/25/2016 08:12 AM, Erik Skultety wrote:
> Provide a simple C example demonstrating the use of both query APIs as well as
> setter APIs.
> 
> Signed-off-by: Erik Skultety <eskultet at redhat.com>
> ---
>  .gitignore               |   1 +
>  examples/Makefile.am     |   3 +-
>  examples/admin/logging.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 105 insertions(+), 1 deletion(-)
>  create mode 100644 examples/admin/logging.c
> 
> diff --git a/.gitignore b/.gitignore
> index 879ec24..984ad07 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -79,6 +79,7 @@
>  /examples/admin/client_limits
>  /examples/admin/list_clients
>  /examples/admin/list_servers
> +/examples/admin/logging
>  /examples/admin/threadpool_params
>  /examples/object-events/event-test
>  /examples/dominfo/info1
> diff --git a/examples/Makefile.am b/examples/Makefile.am
> index 7cb8258..2956e14 100644
> --- a/examples/Makefile.am
> +++ b/examples/Makefile.am
> @@ -43,7 +43,7 @@ noinst_PROGRAMS=dominfo/info1 dommigrate/dommigrate domsuspend/suspend \
>  	domtop/domtop hellolibvirt/hellolibvirt object-events/event-test \
>  	openauth/openauth rename/rename admin/list_servers admin/list_clients \
>  	admin/threadpool_params admin/client_limits admin/client_info \
> -	admin/client_close
> +	admin/client_close admin/logging
>  
>  dominfo_info1_SOURCES = dominfo/info1.c
>  dommigrate_dommigrate_SOURCES = dommigrate/dommigrate.c
> @@ -65,6 +65,7 @@ admin_threadpool_params_SOURCES = admin/threadpool_params.c
>  admin_client_limits_SOURCES = admin/client_limits.c
>  admin_client_info_SOURCES = admin/client_info.c
>  admin_client_close_SOURCES = admin/client_close.c
> +admin_logging_SOURCES = admin/logging.c
>  
>  if WITH_APPARMOR_PROFILES
>  apparmordir = $(sysconfdir)/apparmor.d/
> diff --git a/examples/admin/logging.c b/examples/admin/logging.c
> new file mode 100644
> index 0000000..e28c7d5
> --- /dev/null
> +++ b/examples/admin/logging.c
> @@ -0,0 +1,102 @@
> +#include<stdio.h>
> +#include<stdlib.h>
> +#include<stdbool.h>
> +
> +#include "config.h"
> +#include<unistd.h>
> +#include<libvirt/libvirt-admin.h>
> +#include<libvirt/virterror.h>
> +
> +static void printHelp(const char *argv0)
> +{
> +    fprintf(stderr,
> +            ("Usage:\n"
> +              "  %s [options]\n"
> +              "\n"
> +              "Options:\n"
> +              "  -h          Print this message.\n"
> +              "  -o [string] Specify new log outputs.\n"
> +              "  -f [string] Specify new log filters.\n"
> +              "\n"),
> +            argv0);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    int ret, c;
> +    virAdmConnectPtr conn = NULL;
> +    char *get_outputs = NULL;
> +    char *get_filters = NULL;
> +    const char *set_outputs = NULL;
> +    const char *set_filters = NULL;
> +
> +    ret = c = -1;
> +    opterr = 0;
> +
> +    while ((c = getopt(argc, argv, ":hpo:f:")) > 0) {
> +        switch (c) {
> +        case 'h':
> +            printHelp(argv[0]);
> +            exit(EXIT_SUCCESS);
> +        case 'o':
> +            set_outputs = optarg;
> +            break;
> +        case 'f':
> +            set_filters = optarg;
> +            break;
> +        case ':':
> +            fprintf(stderr, "Missing argument for option -%c\n", optopt);
> +            exit(EXIT_FAILURE);
> +        case '?':
> +            fprintf(stderr, "Unrecognized option '-%c'\n", optopt);
> +            exit(EXIT_FAILURE);
> +        }
> +    }
> +
> +    /* first, open a connection to the daemon */
> +    if (!(conn = virAdmConnectOpen(NULL, 0)))
> +        goto cleanup;
> +
> +    /* get the currently defined log outputs and filters */
> +    if (virAdmConnectGetLoggingOutputs(conn, &get_outputs, 0) < 0 ||
> +        virAdmConnectGetLoggingFilters(conn, &get_filters, 0) < 0)
> +        goto cleanup;
> +
> +    fprintf(stdout,
> +            "Current settings:\n"
> +            " outputs: %s\n"
> +            " filters: %s\n"
> +            "\n",
> +            get_outputs, get_filters ? get_filters : "None");
> +
> +    free(get_outputs);
> +    free(get_filters);

If run without any arguments, we should just stop here.

e.g. if set_outputs and set_filters are both NULL, just jump to the ret = 0;

ACK -

John


> +
> +    /* now, try to change the redefine the current log output and filters */
> +    if (virAdmConnectSetLoggingOutputs(conn, set_outputs, 0) < 0)
> +        goto cleanup;
> +
> +    if (virAdmConnectSetLoggingFilters(conn, set_filters, 0) < 0)
> +        goto cleanup;
> +
> +    /* get the currently defined log outputs and filters */
> +    if (virAdmConnectGetLoggingOutputs(conn, &get_outputs, 0) < 0 ||
> +        virAdmConnectGetLoggingFilters(conn, &get_filters, 0) < 0)
> +        goto cleanup;
> +
> +    fprintf(stdout,
> +            "New settings:\n"
> +            " outputs: %s\n"
> +            " filters: %s\n"
> +            "\n",
> +            get_outputs ? get_outputs : "Default",
> +            get_filters ? get_filters : "None");
> +
> +    free(get_outputs);
> +    free(get_filters);
> +
> +    ret = 0;
> + cleanup:
> +    virAdmConnectClose(conn);
> +    return ret;
> +}
> 




More information about the libvir-list mailing list