]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: msg() now does not change errno (easier usage of "%m").
authorPavel Charvat <pchar@ucw.cz>
Tue, 11 Aug 2020 13:13:27 +0000 (13:13 +0000)
committerPavel Charvat <pchar@ucw.cz>
Thu, 13 Aug 2020 08:30:12 +0000 (08:30 +0000)
ucw/lib.h
ucw/log.c

index 5ce751e9789718da42fc9d22f865ed302618bdfa..0465ca1e4e4920fc441dd141bdafed25064e3ac1 100644 (file)
--- 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(). **/
index 4a951e3277dc4bad99eae8a99feeaebece38a4c6..3ad879b25c24e6d5932fbe887787d5726f1f97c1 100644 (file)
--- a/ucw/log.c
+++ b/ucw/log.c
@@ -4,6 +4,7 @@
  *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
  *     (c) 2014 Tomas Valla <tom@ucw.cz>
+ *     (c) 2020 Pavel Charvat <pchar@ucw.cz>
  *
  *     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