commit d1efe7d91defa30bfc1db3814c6e034277e2d1bf
parent 5f8cf7ab2560fab8db1cdde6c7258e13c40bba17
Author: Robert Russell <robertrussell.72001@gmail.com>
Date: Tue, 13 Jun 2023 13:40:34 -0700
Make r_log preserve errno
Diffstat:
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/inc/log.h b/inc/log.h
@@ -27,5 +27,5 @@ void r_log_init(int color, bool log_time, bool log_loc, int min_level);
/* Log a message to stderr. See definition of r_infof, r_warnf, etc. for usage.
*
- * r_log is thread-safe. */
+ * r_log is thread-safe and preserves errno. */
void r_log(char *file, int line, int level, char *name, char *color, int code, char *fmt, ...);
diff --git a/src/log.c b/src/log.c
@@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 199506L /* flockfile, funlockfile */
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@@ -54,9 +55,11 @@ r_log(
if (level < min_level)
return;
+ int saved_errno = errno;
+
char stamp[ISO8601_SIZE];
if (log_time)
- iso8601(stamp, time(0)); /* Do this before locking stderr */
+ iso8601(stamp, time(0)); /* Note we do this before locking stderr. */
/* Lock stderr so other threads' IO does not get interleaved with ours. */
flockfile(stderr);
@@ -77,4 +80,6 @@ r_log(
if (code)
exit(code);
+
+ errno = saved_errno;
}