[PATCH v2 0/3] Some improvements to log functions
Several small improvements to the logging functions. Changes since v1: * Added printf warning attributes * Added vlogmsg() David Gibson (3): log: Don't define logging function 4 times log: Enable format warnings log: Add vlogmsg() Makefile | 3 ++- conf.c | 2 +- log.c | 72 +++++++++++++++++++++++++++++--------------------------- log.h | 14 +++++++---- packet.c | 7 +++--- pasta.c | 2 +- tcp.c | 13 ++++++---- 7 files changed, 63 insertions(+), 50 deletions(-) -- 2.41.0
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.
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.
This saves about 600 bytes of text in the executable (x86, non-AVX2).
Signed-off-by: David Gibson
On Fri, 13 Oct 2023 15:50:28 +1100
David Gibson
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.
If you've been wondering about the reason for this madness, by the way: this comes from (much) earlier versions where we had separate debug builds to avoid the cost of formatting debug messages (and in some cases fetch further information) in the regular build. It didn't actually make a measurable difference, though, and it doesn't anyway make sense any longer. -- Stefano
logmsg() takes printf like arguments, but because it's not a built in, the
compiler won't generate warnings if the format string and parameters don't
match. Enable those by using the format attribute.
Strictly speaking this is a gcc extension, but I believe it is also
supported by some other common compilers. We already use some other
attributes in various places. For now, just use it and we can worry about
compilers that don't support it if it comes up.
This exposes some warnings from existing callers, both in gcc and in
clang-tidy:
- Some are straight out bugs, which we correct
- It's occasionally useful to invoke the logging functions with an empty
string, which gcc objects to, so disable that specific warning in the
Makefile
- Strictly speaking the C standard requires that the parameter for a %p
be a (void *), not some other pointer type. That's only likely to cause
problems in practice on weird architectures with different sized
representations for pointers to different types. Nonetheless add the
casts to make it happy.
Signed-off-by: David Gibson
Currently logmsg() is only available as a variadic function. This is fine
for normal use, but is awkward if we ever want to write wrappers around it
which (for example) add standardised prefix information. To allow that,
add a vlogmsg() function which takes a va_list instead, and implement
logmsg() in terms of it.
Signed-off-by: David Gibson
On Fri, 13 Oct 2023 15:50:27 +1100
David Gibson
Several small improvements to the logging functions.
Changes since v1: * Added printf warning attributes * Added vlogmsg()
David Gibson (3): log: Don't define logging function 4 times log: Enable format warnings log: Add vlogmsg()
Applied. -- Stefano
participants (2)
-
David Gibson
-
Stefano Brivio