[CRIU] [PATCH 56/78] compel: Add callback-based log engine

Cyrill Gorcunov gorcunov at openvz.org
Mon Nov 7 08:36:41 PST 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 to make same close to what we have in criu.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 compel/Makefile                          |  2 ++
 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/log.h                     | 61 ++++++++++++++++++++++++++++++++
 compel/include/piegen.h                  | 23 ------------
 compel/include/uapi/compel.h             |  8 +++++
 compel/include/uapi/loglevels.h          | 13 +++++++
 compel/src/lib/handle-elf.c              |  1 +
 compel/src/lib/log-host.c                |  1 +
 compel/src/lib/log.c                     | 38 ++++++++++++++++++++
 compel/src/main.c                        | 20 ++++++++---
 13 files changed, 144 insertions(+), 27 deletions(-)
 create mode 100644 compel/include/log.h
 create mode 100644 compel/include/uapi/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 71e646ce8070..f47d48805681 100644
--- a/compel/Makefile
+++ b/compel/Makefile
@@ -19,6 +19,8 @@ lib-y			+= arch/$(ARCH)/src/lib/handle-elf.o
 host-lib-y		+= arch/$(ARCH)/src/lib/handle-elf.o
 lib-y			+= src/lib/handle-elf.o
 host-lib-y		+= src/lib/handle-elf.o
+lib-y			+= src/lib/log.o
+host-lib-y		+= src/lib/log.o
 
 ifeq ($(ARCH),x86)
 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 633a382cc2da..1c3686c48466 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 e2df0f90ddf5..8abf8dad1d43 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 6491f20850ad..3d4020f597d4 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 7cfbaa1dd026..38e27abd3a8a 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/log.h b/compel/include/log.h
new file mode 100644
index 000000000000..4619e8ab45a8
--- /dev/null
+++ b/compel/include/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/piegen.h b/compel/include/piegen.h
index 9789fa0a0831..d240cd642ff9 100644
--- a/compel/include/piegen.h
+++ b/compel/include/piegen.h
@@ -17,8 +17,6 @@ typedef struct {
 	char		*var_name;
 	char		*nrgotpcrel_name;
 	FILE		*fout;
-	FILE		*ferr;
-	FILE		*fdebug;
 } piegen_opt_t;
 
 extern piegen_opt_t opts;
@@ -29,27 +27,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.h b/compel/include/uapi/compel.h
index 10507ce3fb22..278a85455d9c 100644
--- a/compel/include/uapi/compel.h
+++ b/compel/include/uapi/compel.h
@@ -2,6 +2,7 @@
 #define UAPI_COMPEL_H__
 
 #include <errno.h>
+#include <stdarg.h>
 
 #define COMPEL_TYPE_INT		(1u << 0)
 #define COMPEL_TYPE_LONG	(1u << 1)
@@ -14,4 +15,11 @@ typedef struct {
 	long		value;
 } compel_reloc_t;
 
+/*
+ * Logging
+ */
+typedef void (*compel_log_fn)(unsigned int lvl, const char *fmt, va_list parms);
+extern void compel_log_init(compel_log_fn log_fn, unsigned int level);
+extern unsigned int compel_log_get_loglevel(void);
+
 #endif /* UAPI_COMPEL_H__ */
diff --git a/compel/include/uapi/loglevels.h b/compel/include/uapi/loglevels.h
new file mode 100644
index 000000000000..f7cdcc448475
--- /dev/null
+++ b/compel/include/uapi/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 32b715cd0496..ec41771db908 100644
--- a/compel/src/lib/handle-elf.c
+++ b/compel/src/lib/handle-elf.c
@@ -16,6 +16,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..4c98407c6e05
--- /dev/null
+++ b/compel/src/lib/log.c
@@ -0,0 +1,38 @@
+#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 compel_log_fn logfn;
+
+void compel_log_init(compel_log_fn log_fn, unsigned int level)
+{
+	logfn = log_fn;
+	current_loglevel = level;
+}
+
+unsigned int compel_log_get_loglevel(void)
+{
+	return current_loglevel;
+}
+
+void compel_print_on_level(unsigned int loglevel, const char *format, ...)
+{
+	va_list params;
+	compel_log_fn fn = logfn;
+
+	if (fn != NULL && !pr_quelled(loglevel)) {
+		va_start(params, format);
+		fn(loglevel, format, params);
+		va_end(params);
+	}
+}
diff --git a/compel/src/main.c b/compel/src/main.c
index 625ae1188b1d..643b6a96cccd 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 "					\
@@ -81,15 +84,19 @@ err:
 	return ret;
 }
 
+static void cli_log(unsigned int lvl, const char *fmt, va_list parms)
+{
+	if (!pr_quelled(lvl))
+		vprintf(fmt, parms);
+}
+
 int main(int argc, char *argv[])
 {
 	const char *current_cflags = NULL;
+	int log_level = DEFAULT_LOGLEVEL;
 	int opt, idx, i;
 	char *action;
 
-	opts.fdebug = stdout;
-	opts.ferr = stderr;
-
 	typedef struct {
 		const char	*arch;
 		const char	*cflags;
@@ -114,7 +121,7 @@ int main(int argc, char *argv[])
 		},
 	};
 
-	static const char short_opts[] = "a:f:o:s:p:v:r:u:hV";
+	static const char short_opts[] = "a:f:o:s:p:v:r:u:hVl:";
 	static struct option long_opts[] = {
 		{ "arch",	required_argument,	0, 'a' },
 		{ "file",	required_argument,	0, 'f' },
@@ -126,6 +133,7 @@ int main(int argc, char *argv[])
 		{ "pcrelocs",	required_argument,	0, 'r' },
 		{ "help",	required_argument,	0, 'h' },
 		{ "version",	no_argument,		0, 'V' },
+		{ "log-level",	required_argument,	0, 'l' },
 		{ },
 	};
 
@@ -170,6 +178,9 @@ int main(int argc, char *argv[])
 		case 'r':
 			opts.nrgotpcrel_name = optarg;
 			break;
+		case 'l':
+			break;
+			log_level = atoi(optarg);
 		case 'h':
 			goto usage;
 		case 'V':
@@ -204,6 +215,7 @@ int main(int argc, char *argv[])
 	if (!strcmp(action, "piegen")) {
 		if (!opts.input_filename)
 			goto usage;
+		compel_log_init(&cli_log, log_level);
 		return piegen();
 	}
 
-- 
2.7.4



More information about the CRIU mailing list