admin: Add an example demonstrating how to use the logging APIs

Provide a simple C example demonstrating the use of both query APIs as
well as setter APIs.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Erik Skultety 2016-11-24 14:04:11 +01:00
parent 06b917856f
commit 0c169930c0
3 changed files with 111 additions and 1 deletions

1
.gitignore vendored
View File

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

View File

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

108
examples/admin/logging.c Normal file
View File

@ -0,0 +1,108 @@
#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);
/* no arguments were provided */
if (argc == 1) {
ret = 0;
goto cleanup;
}
/* 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;
}