From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id A44035A026F for ; Fri, 13 Oct 2023 07:17:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1697174266; bh=6IwI9G7w3D7OO4J3vohiO6YwyCZ4+7z7j4699jOwHY8=; h=Date:From:To:Subject:References:In-Reply-To:From; b=fots8tkGt4ty6UmJjGD6eOc4wRvt+RJWeZCNhZYdmzcExrhoRAKG1kWJetCOsE3DP Kg6msa8kVtrDmtSsh7Pq83dPwgnn+5SAyqy9V5ZWG2gVA45g7WeetN9BzECuSaFB8M 2SykDeRy/4+l3zJyCi/QZc6El3l1WkDBc2TjgEBA= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S6FBf2DC0z4x5K; Fri, 13 Oct 2023 16:17:46 +1100 (AEDT) Date: Fri, 13 Oct 2023 16:17:14 +1100 From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: Re: [PATCH] log: Don't define logging function 4 times Message-ID: References: <20231012050906.3258198-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="riL4cFNzp3Q8Qn4Z" Content-Disposition: inline In-Reply-To: <20231012050906.3258198-1-david@gibson.dropbear.id.au> Message-ID-Hash: WRQVQIGQ5IPIRGJX6XZ5IU2RMOMGOGLL X-Message-ID-Hash: WRQVQIGQ5IPIRGJX6XZ5IU2RMOMGOGLL 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 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: --riL4cFNzp3Q8Qn4Z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 12, 2023 at 04:09:06PM +1100, David Gibson wrote: > In log.c we use a macro to define logging functions for each of 4 priority > levels. The only difference between these is the priority we pass to > vsyslog() and similar functions. Because it's done as a macro, however, > the entire functions code is included in the binary 4 times. >=20 > Rearrange this to take the priority level as a parameter to a regular > function, then just use macros to define trivial wrappers which pass the > priority level. >=20 > This saves about 600 bytes of text in the executable (x86, non-AVX2). >=20 > Signed-off-by: David Gibson Superseded by a new series with a couple of extra patches. > --- > log.c | 65 +++++++++++++++++++++++++++-------------------------------- > log.h | 12 +++++++---- > 2 files changed, 38 insertions(+), 39 deletions(-) >=20 > diff --git a/log.c b/log.c > index c57bc8d..fd33f64 100644 > --- a/log.c > +++ b/log.c > @@ -47,36 +47,36 @@ int log_to_stdout; /* Print to stdout instead of st= derr */ > =20 > #define BEFORE_DAEMON (setlogmask(0) =3D=3D LOG_MASK(LOG_EMERG)) > =20 > -#define logfn(name, level) \ > -void name(const char *format, ...) { \ > - FILE *out =3D log_to_stdout ? stdout : stderr; \ > - struct timespec tp; \ > - va_list args; \ > - \ > - if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) { \ > - clock_gettime(CLOCK_REALTIME, &tp); \ > - fprintf(out, "%lli.%04lli: ", \ > - (long long int)tp.tv_sec - log_start, \ > - (long long int)tp.tv_nsec / (100L * 1000)); \ > - } \ > - \ > - if ((LOG_MASK(LOG_PRI(level)) & log_mask) || BEFORE_DAEMON) { \ > - va_start(args, format); \ > - if (log_file !=3D -1) \ > - logfile_write(level, format, args); \ > - else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG))) \ > - passt_vsyslog(level, format, args); \ > - va_end(args); \ > - } \ > - \ > - if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) || \ > - (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) { \ > - va_start(args, format); \ > - (void)vfprintf(out, format, args); \ > - va_end(args); \ > - if (format[strlen(format)] !=3D '\n') \ > - fprintf(out, "\n"); \ > - } \ > +void logmsg(int pri, const char *format, ...) > +{ > + FILE *out =3D log_to_stdout ? stdout : stderr; > + struct timespec tp; > + va_list args; > + > + if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) { > + clock_gettime(CLOCK_REALTIME, &tp); > + fprintf(out, "%lli.%04lli: ", > + (long long int)tp.tv_sec - log_start, > + (long long int)tp.tv_nsec / (100L * 1000)); > + } > + > + if ((LOG_MASK(LOG_PRI(pri)) & log_mask) || BEFORE_DAEMON) { > + va_start(args, format); > + if (log_file !=3D -1) > + logfile_write(pri, format, args); > + else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG))) > + passt_vsyslog(pri, format, args); > + va_end(args); > + } > + > + if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file =3D=3D -1) || > + (BEFORE_DAEMON && !(log_opt & LOG_PERROR))) { > + va_start(args, format); > + (void)vfprintf(out, format, args); > + va_end(args); > + if (format[strlen(format)] !=3D '\n') > + fprintf(out, "\n"); > + } > } > =20 > /* Prefixes for log file messages, indexed by priority */ > @@ -89,11 +89,6 @@ const char *logfile_prefix[] =3D { > " ", /* LOG_DEBUG */ > }; > =20 > -logfn(err, LOG_ERR) > -logfn(warn, LOG_WARNING) > -logfn(info, LOG_INFO) > -logfn(debug,LOG_DEBUG) > - > /** > * trace_init() - Set log_trace depending on trace (debug) mode > * @enable: Tracing debug mode enabled if non-zero > diff --git a/log.h b/log.h > index a17171a..b4fb7e9 100644 > --- a/log.h > +++ b/log.h > @@ -6,14 +6,18 @@ > #ifndef LOG_H > #define LOG_H > =20 > +#include > + > #define LOGFILE_SIZE_DEFAULT (1024 * 1024UL) > #define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */ > #define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE)) > =20 > -void err(const char *format, ...); > -void warn(const char *format, ...); > -void info(const char *format, ...); > -void debug(const char *format, ...); > +void logmsg(int pri, const char *format, ...); > + > +#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 die(...) \ > do { \ --=20 David Gibson | 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 --riL4cFNzp3Q8Qn4Z Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmUo0sQACgkQzQJF27ox 2GdcPg//QEnSAd9457FXRlHZNf4B1xwOELTzcm89C5fINAV0bBxMYURFQwseYrXF 16ELm2grPmKk85Ei1fqlgv0E8mu29+F/c6qa1CZ/hpemPdXIIlzA2FIlkBu4E0CB kX2NQn4ybN1fEm3dKFe1NR4pe1t5I6CAW+cpVi1RLgQD5Kxiy5gQIRdoiVH5ALi2 XoxYDxCOXdNMCVlFMYesr+CLE8W8p7HBTYUiPIMgJZsMn+wUaABAAyQpZOXVuY7K IRj2sGtnkf6NZOKh4ktxQUsc+cWRH7yJ/vi2kmOflRYUL8I4ZT6cpBmSZfXJ8X01 VcgnjKHSocGselfliUYKKI8VYa28TVjexqMaKAl+/b0Qfig1JfPT6RQR4+viuG9k 8vnRXL1FoFpDPq8d2RgvZQvatnyW5JAtwQoaS3T4sYWPLnMx6HRAipwsHLoSFQl+ +k5PgZwCLCFmHrfc/GnGQy4A3L1OjfYBnokwTCm+x2u8Vjjk7CrxoJs3EuDpvWw5 +vl6r55bzMjkNWuqRsF396lPT/C3SHFHPSFdj0RMXCTETb2DZmy1Vj+8DxTSE3JO JJy9VgNai5wPQKR3YCb2WH2I9bMHWBQSm3CwpKWHg7qAittuOVFdGrq+NbPE8y3W 7+b5ojRiEssU90YptoDW3mzNPmyR8xigcC1bC/ymwHOL0fVKsN4= =E5zH -----END PGP SIGNATURE----- --riL4cFNzp3Q8Qn4Z--