From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 3AC4C5A0050 for ; Wed, 19 Jun 2024 04:34:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1718764462; bh=QQrCN8F8qe2iMzA483VaBGvuMYlJkoUocgVbhBZnsMw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=UVc9NnnmO6yNpbeq5IsjAcVZZ2zzk4QI7/AqU1JITzc1/LBZTJgqOm8rjQ1mwsDD1 2v7sfgNXghZdPx0DDbMuALiSnZ9fHmZOAnikL27XOEXfrJ6C9bzUcG0mWMZUvEJmcP Qq8ErJ5r0dmfzaXyQxKz8qJZSj+ouZac2tOOMnaLAlDQBI2U981mFbeCb4NeOxMdrQ CvmrHcrIvjhfUnxcsdhI/b3jJhm+IhZFVpaoM94hw8h5OGhm54V3oyuhMkCuXETbUp nidLhqfCf+U41/7/x9QeKWpIMhFFgbplPoyOBNtqq78j3zDOoS+t33mjSIhV5ctwDc Nj+jOjD447ejw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4W3nkk3q4Wz4wqK; Wed, 19 Jun 2024 12:34:22 +1000 (AEST) Date: Wed, 19 Jun 2024 12:21:56 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2 4/6] log: Add _perror() logging function variants Message-ID: References: <20240618071427.1544869-1-sbrivio@redhat.com> <20240618071427.1544869-5-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="H9FNuquZR6fT76KU" Content-Disposition: inline In-Reply-To: <20240618071427.1544869-5-sbrivio@redhat.com> Message-ID-Hash: BDGR3Y23FLRIF4ZSW6OICLJ2NG6RYBQJ X-Message-ID-Hash: BDGR3Y23FLRIF4ZSW6OICLJ2NG6RYBQJ X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Yalan Zhang X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --H9FNuquZR6fT76KU Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jun 18, 2024 at 09:14:25AM +0200, Stefano Brivio wrote: > In many places, we have direct perror() calls, which completely bypass > logging functions and log files. >=20 > They are definitely convenient: offer similar convenience with > _perror() logging variants, so that we can drop those direct perror() > calls. >=20 > Signed-off-by: Stefano Brivio > --- > log.c | 21 +++++++++++++++++++++ > log.h | 21 +++++++++++++++++---- > 2 files changed, 38 insertions(+), 4 deletions(-) >=20 > diff --git a/log.c b/log.c > index 5853496..9ddc58c 100644 > --- a/log.c > +++ b/log.c > @@ -79,6 +79,11 @@ void vlogmsg(int pri, const char *format, va_list ap) > } > } > =20 > +/** > + * logmsg() - vlogmsg() wrapper for variable argument lists > + * @pri: Facility and level map, same as priority for vsyslog() > + * @format: Message > + */ > void logmsg(int pri, const char *format, ...) > { > va_list ap; > @@ -88,6 +93,22 @@ void logmsg(int pri, const char *format, ...) > va_end(ap); > } > =20 > +/** > + * logmsg_perror() - vlogmsg() wrapper with perror()-like functionality > + * @pri: Facility and level map, same as priority for vsyslog() > + * @format: Message > + */ > +void logmsg_perror(int pri, const char *format, ...) > +{ > + va_list ap; > + > + va_start(ap, format); > + vlogmsg(pri, format, ap); > + va_end(ap); > + > + logmsg(pri, ": %s", strerror(errno)); The vlogmsg() above could invoke syscalls which clobber errno, so you need to save it beforehand. > +} > + > /* Prefixes for log file messages, indexed by priority */ > const char *logfile_prefix[] =3D { > NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ > diff --git a/log.h b/log.h > index 1d6dd1d..bdeffde 100644 > --- a/log.h > +++ b/log.h > @@ -16,11 +16,18 @@ > void vlogmsg(int pri, const char *format, va_list ap); > void logmsg(int pri, const char *format, ...) > __attribute__((format(printf, 2, 3))); > +void logmsg_perror(int pri, const char *format, ...) > + __attribute__((format(printf, 2, 3))); > + > +#define err(...) logmsg( LOG_ERR, __VA_ARGS__) > +#define warn(...) logmsg( LOG_WARNING, __VA_ARGS__) > +#define info(...) logmsg( LOG_INFO, __VA_ARGS__) > +#define debug(...) logmsg( LOG_DEBUG, __VA_ARGS__) > =20 > -#define err(...) logmsg(LOG_ERR, __VA_ARGS__) > -#define warn(...) logmsg(LOG_WARNING, __VA_ARGS__) > -#define info(...) logmsg(LOG_INFO, __VA_ARGS__) > -#define debug(...) logmsg(LOG_DEBUG, __VA_ARGS__) > +#define err_perror(...) logmsg_perror( LOG_ERR, __VA_ARGS__) > +#define warn_perror(...) logmsg_perror( LOG_WARNING, __VA_ARGS__) > +#define info_perror(...) logmsg_perror( LOG_INFO, __VA_ARGS__) > +#define debug_perror(...) logmsg_perror( LOG_DEBUG, __VA_ARGS__) > =20 > #define die(...) \ > do { \ > @@ -28,6 +35,12 @@ void logmsg(int pri, const char *format, ...) > exit(EXIT_FAILURE); \ > } while (0) > =20 > +#define die_perror(...) \ > + do { \ > + err_perror(__VA_ARGS__); \ > + exit(EXIT_FAILURE); \ > + } while (0) > + > extern int log_trace; > extern bool log_conf_parsed; > extern bool log_daemon_ready; --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --H9FNuquZR6fT76KU Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZyQMMACgkQzQJF27ox 2GdpUg//UHbDLY0igYJ2mefLI+B7lOHyMYgXnsiqM+8+mIWvr0s+lBmu+/ZiBvOC Ql79e9bEmDmm5YdJzvmMTqYaztjGGubwajk7u8i1IgmoCrdAPdFbfUE1DNGcidl2 en+No95EOkFiemShKKKfsM7d/owrDCDHX6JhheIbBYFF0LfClWss+X6LCj56YmMN fKXx38HRALfvAzGLh7ErlXbQKsYw7LFv4+6/oPECQIXjef7P5FrP7oPq+97trc7e BMAy8pcTCqtU5+W/BHzLQqYRDXe4a59Y5XeJjglhMDQ0+FUTfPknOdQman7UBGSn H9nLh/iaXURf/FxyPcQxNg5KkUJUXL+z3vFrmNmq6uUo8DhG4SL1hhHkoVmKAfSe 81D2lCyZz9pZZyB6n5bQ/RferAa/G4jnrf/QeLXG5FhNFtrQHefvZG3v45uM1yO9 AeBLkX2JB6nZ53YxyrXTxQL9TCo0XkTboWaVxSV5KeWCoShzVc81u12PqG8jI/Ya GYh4szkVu5eDaWR6QcRS+Y8e5xhdgnbLkX9ClApHjPkcm5Xz2+77mDyI1mKKVEn9 cATphyLxu6dqGZpsHc4J4W67yhsb5OUFBgdnUsSIe5ZKpBWDtnRsee0Hl4FclsjV sWyNZmCynUIEM9eQWMJdFLgaUKJfmfTZydtiVmX4Ux1KY9bgfvA= =S6+j -----END PGP SIGNATURE----- --H9FNuquZR6fT76KU--