From bfe8126f8fa37ddc6a636bd3d36bf953481907e3 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Tue, 11 Aug 2020 13:13:27 +0000 Subject: [PATCH] Logging: msg() now does not change errno (easier usage of "%m"). --- ucw/lib.h | 1 + ucw/log.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/ucw/lib.h b/ucw/lib.h index 5ce751e9..0465ca1e 100644 --- a/ucw/lib.h +++ b/ucw/lib.h @@ -137,6 +137,7 @@ enum log_levels { /** The available log levels to pass to msg() and friends. * /** * This is the basic printf-like function for logging a message. * The @flags contain the log level and possibly other flag bits (like `L_SIGHANDLER`). + * Does not change the value of errno. **/ void msg(uint flags, const char *fmt, ...) FORMAT_CHECK(printf,2,3); void vmsg(uint flags, const char *fmt, va_list args); /** A vararg version of msg(). **/ diff --git a/ucw/log.c b/ucw/log.c index 4a951e32..3ad879b2 100644 --- a/ucw/log.c +++ b/ucw/log.c @@ -4,6 +4,7 @@ * (c) 1997--2009 Martin Mares * (c) 2008 Tomas Gavenciak * (c) 2014 Tomas Valla + * (c) 2020 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -97,6 +98,7 @@ log_type_name(uint flags) void vmsg(uint cat, const char *fmt, va_list args) { + int saved_errno = errno; struct timeval tv; struct tm tm; va_list args2; @@ -144,6 +146,7 @@ vmsg(uint cat, const char *fmt, va_list args) /* Generate the message string */ va_copy(args2, args); + errno = saved_errno; len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2); va_end(args2); if (len < (int) sizeof(msgbuf) || sighandler) @@ -151,6 +154,7 @@ vmsg(uint cat, const char *fmt, va_list args) else { m.raw_msg = xmalloc(len+1); + errno = saved_errno; vsnprintf(m.raw_msg, len+1, fmt, args); } @@ -173,6 +177,8 @@ vmsg(uint cat, const char *fmt, va_list args) if (m.raw_msg != msgbuf) xfree(m.raw_msg); + + errno = saved_errno; } static void -- 2.39.2