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.
They are definitely convenient: offer similar convenience with
_perror() logging variants, so that we can drop those direct perror()
calls.
Signed-off-by: Stefano Brivio <sbrivio(a)redhat.com>
---
log.c | 21 +++++++++++++++++++++
log.h | 21 +++++++++++++++++----
2 files changed, 38 insertions(+), 4 deletions(-)
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)
}
}
+/**
+ * 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);
}
+/**
+ * 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.
Oops, nice catch.
--
Stefano