From nobody Tue Oct 15 16:15:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1539865866005588.8036974118551; Thu, 18 Oct 2018 05:31:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EEAC8C059B85; Thu, 18 Oct 2018 12:31:03 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AF4E971D66; Thu, 18 Oct 2018 12:31:03 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 486B018005B3; Thu, 18 Oct 2018 12:31:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9ICRJed026307 for ; Thu, 18 Oct 2018 08:27:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4017760922; Thu, 18 Oct 2018 12:27:19 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6D84611BC for ; Thu, 18 Oct 2018 12:27:18 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Oct 2018 14:26:53 +0200 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [snmp PATCH 15/20] libvirtSnmp: s/showError/printLibvirtError/ X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 18 Oct 2018 12:31:04 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We have two function that do merely the same. Merge them into one. Signed-off-by: Michal Privoznik --- src/libvirtSnmp.c | 65 ++++++++---------------------------------- src/libvirtSnmpError.c | 38 ++++++++++++++++++++---- src/libvirtSnmpError.h | 13 ++++++++- 3 files changed, 57 insertions(+), 59 deletions(-) diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c index 7aa83a1..97bb927 100644 --- a/src/libvirtSnmp.c +++ b/src/libvirtSnmp.c @@ -30,6 +30,7 @@ #include =20 #include "libvirtSnmp.h" +#include "libvirtSnmpError.h" #include "libvirtGuestTable.h" /* include our MIB structures*/ #include "libvirtNotifications.h" =20 @@ -43,17 +44,6 @@ #define ATTRIBUTE_UNUSED __attribute__((__unused__)) #endif =20 -# ifndef ATTRIBUTE_FMT_PRINTF -# ifndef __clang__ -# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ - __attribute__((__format__ (__gnu_printf__, fmtpos, argpos))) -# else -# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ - __attribute__((__format__ (__printf__, fmtpos, argpos))) -# endif -# endif - - int verbose =3D 0; virConnectPtr conn; int callbackRet =3D -1; @@ -86,37 +76,6 @@ stop(int sig) run =3D 0; } =20 -static void -ATTRIBUTE_FMT_PRINTF(1,2) -showError(const char *fmt, ...) -{ - const char *libvirtErr =3D virGetLastErrorMessage(); - char ebuf[1024]; - int rc; - int size =3D 0; - va_list ap; - - va_start(ap, fmt); - rc =3D vsnprintf(ebuf, sizeof(ebuf), fmt, ap); - size +=3D rc; - va_end(ap); - - if (rc < 0 || size >=3D sizeof(ebuf)) - return; - - rc =3D snprintf(ebuf + size, sizeof(ebuf) - size, ": %s\n", libvirtErr= ); - size +=3D rc; - - if (rc < 0 || size >=3D sizeof(ebuf)) - return; - - fputs(ebuf, stderr); - snmp_log(LOG_ERR, "%s", ebuf); - - virResetLastError(); -} - - static int insertGuest(netsnmp_container *container, virDomainPtr domain) { @@ -126,7 +85,7 @@ insertGuest(netsnmp_container *container, virDomainPtr d= omain) unsigned char uuid[VIR_UUID_BUFLEN]; =20 if (virDomainGetInfo(domain, &info) < 0) { - showError("Failed to get domain info"); + printLibvirtError("Failed to get domain info"); goto error; } =20 @@ -138,7 +97,7 @@ insertGuest(netsnmp_container *container, virDomainPtr d= omain) =20 /* set the index of the row */ if (virDomainGetUUID(domain, uuid) < 0) { - showError("Failed to get domain UUID"); + printLibvirtError("Failed to get domain UUID"); goto error; } =20 @@ -151,7 +110,7 @@ insertGuest(netsnmp_container *container, virDomainPtr = domain) =20 /* set the data */ if (!(name =3D virDomainGetName(domain))) { - showError("Failed to get domain name"); + printLibvirtError("Failed to get domain name"); goto error; } =20 @@ -195,7 +154,7 @@ libvirtSnmpLoadGuests(netsnmp_container *container) virDomainPtr *domains =3D NULL; =20 if ((ndomains =3D virConnectListAllDomains(conn, &domains, 0)) < 0) { - showError("Failed to list all domains"); + printLibvirtError("Failed to list all domains"); goto cleanup; } =20 @@ -221,7 +180,7 @@ pollingThreadFunc(void *foo) { while (run) { if (virEventRunDefaultImpl() < 0) { - showError("Failed to run event loop"); + printLibvirtError("Failed to run event loop"); pthread_exit(NULL); } } @@ -250,7 +209,7 @@ libvirtRegisterEvents(virConnectPtr conn) { NULL, myFreeFunc); =20 if (callbackRet =3D=3D -1) { - showError("Failed to register libvirt event handler"); + printLibvirtError("Failed to register libvirt event handler"); goto cleanup; } =20 @@ -287,7 +246,7 @@ int libvirtSnmpInit(void) /* TODO: configure the URI */ /* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/ if (!(conn =3D virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0)))= { - showError("No connection to hypervisor"); + printLibvirtError("No connection to hypervisor"); return -1; } =20 @@ -326,7 +285,7 @@ void libvirtSnmpShutdown(void) } =20 if ((rc =3D virConnectClose(conn))) { - showError("Failed to disconnect from hypervisor. " + printLibvirtError("Failed to disconnect from hypervisor. " "Leaked references: %d\n", rc); } } @@ -366,7 +325,7 @@ libvirtSnmpCreate(unsigned char *uuid, int state) } =20 if (virDomainCreateWithFlags(dom, flags) < 0) { - showError("Failed to create domain: %s", virDomainGetName(dom)); + printLibvirtError("Failed to create domain: %s", virDomainGetName(= dom)); goto cleanup; } =20 @@ -388,7 +347,7 @@ libvirtSnmpDestroy(unsigned char *uuid) } =20 if (virDomainDestroy(dom) < 0) { - showError("Failed to destroy domain %s", virDomainGetName(dom)); + printLibvirtError("Failed to destroy domain %s", virDomainGetName(= dom)); goto cleanup; } =20 @@ -421,7 +380,7 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstat= e, int oldstate) } =20 if (ret < 0) - showError("Failed to change state of %s", virDomainGetName(dom)); + printLibvirtError("Failed to change state of %s", virDomainGetName= (dom)); out: virDomainFree(dom); return ret; diff --git a/src/libvirtSnmpError.c b/src/libvirtSnmpError.c index 1678bcb..c356aae 100644 --- a/src/libvirtSnmpError.c +++ b/src/libvirtSnmpError.c @@ -25,15 +25,43 @@ #include #include #include +#include +#include +#include =20 #include "libvirtSnmpError.h" =20 /** - * Print libvirt error - * @msg Error message + * printLibvirtError: + * @fmt: Error message format string + * + * Print and reset Libvirt error. */ -void printLibvirtError(const char *msg) { - virErrorPtr err =3D virGetLastError(); +void +printLibvirtError(const char *fmt, ...) +{ + const char *libvirtErr =3D virGetLastErrorMessage(); + char ebuf[1024]; + int rc; + int size =3D 0; + va_list ap; =20 - fprintf(stderr, "%s: %s", msg, err ? err->message : "Unknown error"); + va_start(ap, fmt); + rc =3D vsnprintf(ebuf, sizeof(ebuf), fmt, ap); + size +=3D rc; + va_end(ap); + + if (rc < 0 || size >=3D sizeof(ebuf)) + return; + + rc =3D snprintf(ebuf + size, sizeof(ebuf) - size, ": %s\n", libvirtErr= ); + size +=3D rc; + + if (rc < 0 || size >=3D sizeof(ebuf)) + return; + + fputs(ebuf, stderr); + snmp_log(LOG_ERR, "%s", ebuf); + + virResetLastError(); } diff --git a/src/libvirtSnmpError.h b/src/libvirtSnmpError.h index 8dae0cb..46c98f0 100644 --- a/src/libvirtSnmpError.h +++ b/src/libvirtSnmpError.h @@ -23,6 +23,17 @@ #ifndef __LIBVIRT_SNMP_ERROR_H__ # define __LIBVIRT_SNMP_ERROR_H__ =20 -void printLibvirtError(const char *msg); +# ifndef ATTRIBUTE_FMT_PRINTF +# ifndef __clang__ +# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ + __attribute__((__format__ (__gnu_printf__, fmtpos, argpos))) +# else +# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \ + __attribute__((__format__ (__printf__, fmtpos, argpos))) +# endif +# endif + +void printLibvirtError(const char *fmt, ...) + ATTRIBUTE_FMT_PRINTF(1, 2); =20 #endif /* __LIBVIRT_SNMP_ERROR_H__ */ --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list