Unverified Commit 2cdf1af2 by 2xsec Committed by Christian Brauner

log: add lxc_log_strerror_r macro

Let's ensure that we always use the thread-safe strerror_r() function and add an approriate macro. Additionally, define SYS*() macros for all log levels. They will use the new macro and ensure thread-safe retrieval of errno values. Signed-off-by: 's avatar2xsec <dh48.jeong@samsung.com> [christian.brauner@ubuntu.com: simplify lxc_log_strerror_r macro] Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 160ef98f
......@@ -293,6 +293,27 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
(lxc_log_priority_to_string(lxc_log_category_##name.priority))
/*
* Helper macro to define errno string.
*/
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
#define lxc_log_strerror_r \
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
char *ptr = errno_buf; \
{ \
(void)strerror_r(errno, errno_buf, sizeof(errno_buf)); \
}
#else
#define lxc_log_strerror_r \
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
char *ptr; \
{ \
ptr = strerror_r(errno, errno_buf, sizeof(errno_buf)); \
if (!ptr) \
ptr = errno_buf; \
}
#endif
/*
* top categories
*/
#define TRACE(format, ...) do { \
......@@ -340,11 +361,41 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define SYSERROR(format, ...) do { \
ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
} while (0)
#define SYSTRACE(format, ...) \
do { \
lxc_log_strerror_r; \
TRACE("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
#define SYSDEBUG(format, ...) \
do { \
lxc_log_strerror_r; \
DEBUG("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
#define SYSINFO(format, ...) \
do { \
lxc_log_strerror_r; \
INFO("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
#define SYSNOTICE(format, ...) \
do { \
lxc_log_strerror_r; \
NOTICE("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
#define SYSWARN(format, ...) \
do { \
lxc_log_strerror_r; \
WARN("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
#define SYSERROR(format, ...) \
do { \
lxc_log_strerror_r; \
ERROR("%s - " format, ptr, ##__VA_ARGS__); \
} while (0)
extern int lxc_log_fd;
......
......@@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
......
......@@ -22,6 +22,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
......
......@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <lxc/lxccontainer.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment