This is to make the calling code more portable
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qblog.h | 14 ++------------
include/qb/qbutil.h | 5 +++++
lib/util.c | 18 ++++++++++++++++++
lib/util_int.h | 5 +++--
4 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/include/qb/qblog.h b/include/qb/qblog.h
index edc83cc..781b060 100644
--- a/include/qb/qblog.h
+++ b/include/qb/qblog.h
@@ -34,6 +34,7 @@ extern "C" {
#include <errno.h>
#include <syslog.h>
#include <string.h>
+#include <qb/qbutil.h>
#include <qb/qbconfig.h>
#ifdef S_SPLINT_S
@@ -324,23 +325,12 @@ void qb_log_from_external_source(const char *function,
* @param args usual printf style args
*/
#ifndef S_SPLINT_S
-#ifdef _GNU_SOURCE
-/* The GNU version of strerror_r returns a (char*) that *must* be used */
#define qb_perror(priority, fmt, args...) do { \
char _perr_buf_[QB_LOG_STRERROR_MAX_LEN]; \
- const char *_perr_str_ = strerror_r(errno, _perr_buf_, sizeof(_perr_buf_)); \
+ const char *_perr_str_ = qb_strerror_r(errno, _perr_buf_, sizeof(_perr_buf_)); \
qb_logt(priority, 0, fmt ": %s (%d)", ##args, _perr_str_, errno); \
} while(0)
#else
-/* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
-#define qb_perror(priority, fmt, args...) do { \
- char _perr_buf_[QB_LOG_STRERROR_MAX_LEN]; \
- if (strerror_r(errno, _perr_buf_, sizeof(_perr_buf_)) == 0) { \
- qb_logt(priority, 0, fmt ": %s (%d)", ##args, _perr_buf_, errno); \
- } else { qb_logt(priority, 0, fmt ": (%d)", ##args, errno); } \
- } while(0)
-#endif
-#else
#define qb_perror
#endif
diff --git a/include/qb/qbutil.h b/include/qb/qbutil.h
index 811c034..0f3ef19 100644
--- a/include/qb/qbutil.h
+++ b/include/qb/qbutil.h
@@ -120,6 +120,11 @@ uint64_t qb_util_nano_from_epoch_get(void);
*/
void qb_util_timespec_from_epoch_get(struct timespec *ts);
+/**
+ * strerror_r replacement.
+ */
+char *qb_strerror_r(int errnum, char *buf, size_t buflen);
+
/* *INDENT-OFF* */
#ifdef __cplusplus
}
diff --git a/lib/util.c b/lib/util.c
index e639a3f..48d5e9a 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -399,3 +399,21 @@ qb_util_fd_nonblock_cloexec_set(int32_t fd)
}
return res;
}
+
+char *
+qb_strerror_r(int errnum, char *buf, size_t buflen)
+{
+#ifdef QB_LINUX
+ return strerror_r(errnum, buf, buflen);
+#else
+ char * out_buf;
+
+ if (strerror_r(err_num, buffer, sizeof_buffer) == 0 ) {
+ out_ptr = buffer;
+ } else {
+ out_ptr = "";
+ }
+ return out_buf;
+#endif /* QB_LINUX */
+}
+
diff --git a/lib/util_int.h b/lib/util_int.h
index 0a3f1ff..1b1169f 100644
--- a/lib/util_int.h
+++ b/lib/util_int.h
@@ -36,8 +36,9 @@
#ifndef S_SPLINT_S
#define qb_util_perror(priority, fmt, args...) do { \
- const char *err = strerror(errno); \
- qb_logt(priority, QB_LOG_TAG_LIBQB_MSG, fmt ": %s (%d)", ##args, err, errno);
\
+ char _perr_buf_[QB_LOG_STRERROR_MAX_LEN]; \
+ const char *_perr_str_ = qb_strerror_r(errno, _perr_buf_, sizeof(_perr_buf_)); \
+ qb_logt(priority, QB_LOG_TAG_LIBQB_MSG, fmt ": %s (%d)", ##args, _perr_str_,
errno); \
} while(0)
#else
#define qb_util_perror
--
1.7.6