[CRIU] [PATCH 07/22] compel: Add simple log engine
Cyrill Gorcunov
gorcunov at openvz.org
Wed Oct 19 12:21:22 PDT 2016
pr_out is only special left in piegen engine,
the rest use compel's pr_x output. Probably
we will need to enhance it one day.
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
compel/Makefile | 3 +
compel/arch/aarch64/src/lib/handle-elf.c | 1 +
compel/arch/arm/src/lib/handle-elf.c | 1 +
compel/arch/ppc64/src/lib/handle-elf.c | 1 +
compel/arch/x86/src/lib/handle-elf.c | 1 +
compel/include/compel/log.h | 61 ++++++++++++++++++
compel/include/compel/piegen.h | 23 -------
compel/include/uapi/compel/compel.h | 10 +++
compel/include/uapi/compel/loglevels.h | 13 ++++
compel/src/lib/handle-elf.c | 1 +
compel/src/lib/log-host.c | 1 +
compel/src/lib/log.c | 104 +++++++++++++++++++++++++++++++
compel/src/main.c | 6 +-
13 files changed, 200 insertions(+), 26 deletions(-)
create mode 100644 compel/include/compel/log.h
create mode 100644 compel/include/uapi/compel/loglevels.h
create mode 120000 compel/src/lib/log-host.c
create mode 100644 compel/src/lib/log.c
diff --git a/compel/Makefile b/compel/Makefile
index f07bb8dad025..3de60601087f 100644
--- a/compel/Makefile
+++ b/compel/Makefile
@@ -43,6 +43,9 @@ $(obj)/include/uapi/compel/asm/types.h: $(obj)/arch/$(ARCH)/src/lib/include/comp
cleanup-y += $(obj)/include/uapi/compel/asm/types.h
headers-y += $(obj)/include/uapi/compel/asm/types.h
+lib-y += src/lib/log.o
+host-lib-y += src/lib/log.o
+
ifeq ($(ARCH),x86)
lib-y += src/lib/handle-elf-32.o
host-lib-y += src/lib/handle-elf-32.o
diff --git a/compel/arch/aarch64/src/lib/handle-elf.c b/compel/arch/aarch64/src/lib/handle-elf.c
index 22ae01c501fc..52c65148d768 100644
--- a/compel/arch/aarch64/src/lib/handle-elf.c
+++ b/compel/arch/aarch64/src/lib/handle-elf.c
@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
+#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
diff --git a/compel/arch/arm/src/lib/handle-elf.c b/compel/arch/arm/src/lib/handle-elf.c
index 4998688a0ad9..d5df37263b1e 100644
--- a/compel/arch/arm/src/lib/handle-elf.c
+++ b/compel/arch/arm/src/lib/handle-elf.c
@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
+#include "log.h"
static const unsigned char __maybe_unused
elf_ident_32[EI_NIDENT] = {
diff --git a/compel/arch/ppc64/src/lib/handle-elf.c b/compel/arch/ppc64/src/lib/handle-elf.c
index 78703bc1d9a6..a0ed0509e568 100644
--- a/compel/arch/ppc64/src/lib/handle-elf.c
+++ b/compel/arch/ppc64/src/lib/handle-elf.c
@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
+#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
diff --git a/compel/arch/x86/src/lib/handle-elf.c b/compel/arch/x86/src/lib/handle-elf.c
index 26b15ec7ac13..30b9ea136757 100644
--- a/compel/arch/x86/src/lib/handle-elf.c
+++ b/compel/arch/x86/src/lib/handle-elf.c
@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
+#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
diff --git a/compel/include/compel/log.h b/compel/include/compel/log.h
new file mode 100644
index 000000000000..4619e8ab45a8
--- /dev/null
+++ b/compel/include/compel/log.h
@@ -0,0 +1,61 @@
+#ifndef COMPEL_LOG_H__
+#define COMPEL_LOG_H__
+
+#include "uapi/compel/compel.h"
+#include "uapi/compel/loglevels.h"
+
+#ifndef LOG_PREFIX
+# define LOG_PREFIX
+#endif
+
+static inline int pr_quelled(unsigned int loglevel)
+{
+ return compel_log_get_loglevel() < loglevel && loglevel != LOG_MSG;
+}
+
+extern void compel_print_on_level(unsigned int loglevel, const char *format, ...);
+
+#define pr_msg(fmt, ...) \
+ compel_print_on_level(LOG_MSG, \
+ fmt, ##__VA_ARGS__)
+
+#define pr_info(fmt, ...) \
+ compel_print_on_level(LOG_INFO, \
+ LOG_PREFIX fmt, ##__VA_ARGS__)
+
+#define pr_err(fmt, ...) \
+ compel_print_on_level(LOG_ERROR, \
+ "Error (%s:%d): " LOG_PREFIX fmt, \
+ __FILE__, __LINE__, ##__VA_ARGS__)
+
+#define pr_err_once(fmt, ...) \
+ do { \
+ static bool __printed; \
+ if (!__printed) { \
+ pr_err(fmt, ##__VA_ARGS__); \
+ __printed = 1; \
+ } \
+ } while (0)
+
+#define pr_warn(fmt, ...) \
+ compel_print_on_level(LOG_WARN, \
+ "Warn (%s:%d): " LOG_PREFIX fmt, \
+ __FILE__, __LINE__, ##__VA_ARGS__)
+
+#define pr_warn_once(fmt, ...) \
+ do { \
+ static bool __printed; \
+ if (!__printed) { \
+ pr_warn(fmt, ##__VA_ARGS__); \
+ __printed = 1; \
+ } \
+ } while (0)
+
+#define pr_debug(fmt, ...) \
+ compel_print_on_level(LOG_DEBUG, \
+ LOG_PREFIX fmt, ##__VA_ARGS__)
+
+#define pr_perror(fmt, ...) \
+ pr_err(fmt ": %m\n", ##__VA_ARGS__)
+
+#endif /* COMPEL_LOG_H__ */
diff --git a/compel/include/compel/piegen.h b/compel/include/compel/piegen.h
index f1ed2e13d309..4ad4ec40539a 100644
--- a/compel/include/compel/piegen.h
+++ b/compel/include/compel/piegen.h
@@ -16,8 +16,6 @@ typedef struct {
char *var_name;
char *nrgotpcrel_name;
FILE *fout;
- FILE *ferr;
- FILE *fdebug;
} piegen_opt_t;
extern piegen_opt_t opts;
@@ -28,27 +26,6 @@ do { \
fprintf(opts.fout, fmt, ##__VA_ARGS__); \
} while (0)
-#define pr_debug(fmt, ...) \
-do { \
- if (opts.fdebug) \
- fprintf(opts.fdebug, "%s: "fmt, \
- opts.stream_name, ##__VA_ARGS__); \
-} while (0)
-
-#define pr_err(fmt, ...) \
-do { \
- if (opts.ferr) \
- fprintf(opts.ferr, "%s: Error (%s:%d): "fmt, \
- opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__); \
-} while (0)
-
-#define pr_perror(fmt, ...) \
-do { \
- if (opts.ferr) \
- fprintf(opts.ferr, "%s: Error (%s:%d): "fmt ": %m\n", \
- opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__); \
-} while (0)
-
extern int handle_binary(void *mem, size_t size);
#endif /* COMPEL_PIEGEN_H__ */
diff --git a/compel/include/uapi/compel/compel.h b/compel/include/uapi/compel/compel.h
index 10507ce3fb22..dcbffdbbf9ea 100644
--- a/compel/include/uapi/compel/compel.h
+++ b/compel/include/uapi/compel/compel.h
@@ -14,4 +14,14 @@ typedef struct {
long value;
} compel_reloc_t;
+/*
+ * Logging
+ */
+extern int compel_log_init(const char *output);
+extern void compel_log_fini(void);
+extern void compel_log_set_loglevel(unsigned int loglevel);
+extern unsigned int compel_log_get_loglevel(void);
+extern int compel_log_get_fd(void);
+extern void compel_log_set_fd(int fd);
+
#endif /* UAPI_COMPEL_H__ */
diff --git a/compel/include/uapi/compel/loglevels.h b/compel/include/uapi/compel/loglevels.h
new file mode 100644
index 000000000000..f7cdcc448475
--- /dev/null
+++ b/compel/include/uapi/compel/loglevels.h
@@ -0,0 +1,13 @@
+#ifndef UAPI_COMPEL_LOGLEVELS_H__
+#define UAPI_COMPEL_LOGLEVELS_H__
+
+#define LOG_UNSET (-1)
+#define LOG_MSG (0) /* Print message regardless of log level */
+#define LOG_ERROR (1) /* Errors only, when we're in trouble */
+#define LOG_WARN (2) /* Warnings, dazen and confused but trying to continue */
+#define LOG_INFO (3) /* Informative, everything is fine */
+#define LOG_DEBUG (4) /* Debug only */
+
+#define DEFAULT_LOGLEVEL LOG_WARN
+
+#endif /* UAPI_COMPEL_LOGLEVELS_H__ */
diff --git a/compel/src/lib/handle-elf.c b/compel/src/lib/handle-elf.c
index 8fa89135e268..cd3d78b31894 100644
--- a/compel/src/lib/handle-elf.c
+++ b/compel/src/lib/handle-elf.c
@@ -17,6 +17,7 @@
#include "handle-elf.h"
#include "piegen.h"
+#include "log.h"
/* Check if pointer is out-of-bound */
static bool __ptr_oob(const uintptr_t ptr, const uintptr_t start, const size_t size)
diff --git a/compel/src/lib/log-host.c b/compel/src/lib/log-host.c
new file mode 120000
index 000000000000..918e3d32f6ad
--- /dev/null
+++ b/compel/src/lib/log-host.c
@@ -0,0 +1 @@
+log.c
\ No newline at end of file
diff --git a/compel/src/lib/log.c b/compel/src/lib/log.c
new file mode 100644
index 000000000000..aa379f3d32fc
--- /dev/null
+++ b/compel/src/lib/log.c
@@ -0,0 +1,104 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <fcntl.h>
+
+#include <compel/compel.h>
+
+#include "log.h"
+
+static unsigned int current_loglevel = DEFAULT_LOGLEVEL;
+static int logfd = STDOUT_FILENO;
+
+int compel_log_init(const char *output)
+{
+ int new_logfd;
+
+ if (output && !strncmp(output, "-", 2)) {
+ new_logfd = dup(STDOUT_FILENO);
+ if (new_logfd < 0) {
+ pr_perror("Cant't dup stdout stream");
+ return -1;
+ }
+ } else if (output) {
+ new_logfd = open(output, O_CREAT|O_TRUNC|O_WRONLY|O_APPEND, 0600);
+ if (new_logfd < 0) {
+ pr_perror("Can't create log file %s", output);
+ return -1;
+ }
+ } else {
+ new_logfd = dup(STDERR_FILENO);
+ if (new_logfd < 0) {
+ pr_perror("Can't dup log file");
+ return -1;
+ }
+ }
+
+ logfd = new_logfd;
+ return 0;
+}
+
+void compel_log_fini(void)
+{
+ if (logfd >= 0 && logfd != STDOUT_FILENO) {
+ close(logfd);
+ logfd = STDOUT_FILENO;
+ }
+}
+
+int compel_log_get_fd(void)
+{
+ return logfd;
+}
+
+void compel_log_set_fd(int fd)
+{
+ compel_log_fini();
+ logfd = fd;
+}
+
+void compel_log_set_loglevel(unsigned int level)
+{
+ if (level == LOG_UNSET)
+ current_loglevel = DEFAULT_LOGLEVEL;
+ else
+ current_loglevel = level;
+}
+
+unsigned int compel_log_get_loglevel(void)
+{
+ return current_loglevel;
+}
+
+static void __print_on_level(unsigned int loglevel, const char *format, va_list params)
+{
+ size_t size, off = 0;
+ int __errno = errno;
+ char buffer[4096];
+ ssize_t ret;
+
+ if (pr_quelled(loglevel))
+ return;
+
+ size = vsnprintf(buffer, sizeof(buffer), format, params);
+ while (off < size) {
+ ret = write(logfd, buffer + off, size - off);
+ if (ret <= 0)
+ break;
+ off += ret;
+ }
+ errno = __errno;
+}
+
+void compel_print_on_level(unsigned int loglevel, const char *format, ...)
+{
+ va_list params;
+
+ va_start(params, format);
+ __print_on_level(loglevel, format, params);
+ va_end(params);
+}
diff --git a/compel/src/main.c b/compel/src/main.c
index f3d45e6a5f14..734a51a58c3d 100644
--- a/compel/src/main.c
+++ b/compel/src/main.c
@@ -12,8 +12,11 @@
#include <sys/stat.h>
#include <sys/mman.h>
+#include "uapi/compel/compel.h"
+
#include "version.h"
#include "piegen.h"
+#include "log.h"
#define CFLAGS_DEFAULT_SET \
"-Wstrict-prototypes -Wa,--noexecstack " \
@@ -88,9 +91,6 @@ int main(int argc, char *argv[])
int opt, idx, i;
char *action;
- opts.fdebug = stdout;
- opts.ferr = stderr;
-
typedef struct {
const char *arch;
const char *cflags;
--
2.7.4
More information about the CRIU
mailing list