[CRIU] [RFC 2/4] compel: make output streams options

Dmitry Safonov dsafonov at virtuozzo.com
Fri May 6 09:14:57 PDT 2016


That will help to omit pr_* output on the screen on tests,
where one can redefine `opts`.

Cc: Cyrill Gorcunov <gorcunov at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 compel/include/piegen.h | 32 +++++++++++++++++++++++++++-----
 compel/main.c           | 14 ++++++++------
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/compel/include/piegen.h b/compel/include/piegen.h
index da6ff219b7fe..736152a2c475 100644
--- a/compel/include/piegen.h
+++ b/compel/include/piegen.h
@@ -15,17 +15,39 @@ typedef struct {
 	char		*prefix_name;
 	char		*var_name;
 	char		*nrgotpcrel_name;
+	FILE		*fout;
+	FILE		*ferr;
+	FILE		*fdebug;
 } piegen_opt_t;
 
 extern piegen_opt_t opts;
-extern FILE *fout;
 
-#define pr_out(fmt, ...)	fprintf(fout, fmt, ##__VA_ARGS__)
+#define pr_out(fmt, ...)							\
+do {										\
+	if (opts.fout)								\
+		fprintf(opts.fout, fmt, ##__VA_ARGS__);				\
+} while (0)
 
-#define pr_debug(fmt, ...)	printf("%s: "fmt, opts.stream_name, ##__VA_ARGS__)
+#define pr_debug(fmt, ...)							\
+do {										\
+	if (opts.fdebug)							\
+		fprintf(opts.fdebug, "%s: "fmt,					\
+			opts.stream_name, ##__VA_ARGS__);			\
+} while (0)
 
-#define pr_err(fmt, ...)	fprintf(stderr, "%s: Error (%s:%d): "fmt, opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__)
-#define pr_perror(fmt, ...)	fprintf(stderr, "%s: Error (%s:%d): "fmt ": %m\n", opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__)
+#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);
 
diff --git a/compel/main.c b/compel/main.c
index 5fe30c31648d..6f117cded569 100644
--- a/compel/main.c
+++ b/compel/main.c
@@ -25,10 +25,9 @@ piegen_opt_t opts = {
 	.prefix_name		= "__",
 	.var_name		= "elf_relocs",
 	.nrgotpcrel_name	= "nr_gotpcrel",
+	.fout			= NULL,
 };
 
-FILE *fout;
-
 static int piegen(void)
 {
 	struct stat st;
@@ -46,8 +45,8 @@ static int piegen(void)
 		goto err;
 	}
 
-	fout = fopen(opts.output_filename, "w");
-	if (fout == NULL) {
+	opts.fout = fopen(opts.output_filename, "w");
+	if (opts.fout == NULL) {
 		pr_perror("Can't open %s", opts.output_filename);
 		goto err;
 	}
@@ -69,8 +68,8 @@ static int piegen(void)
 err:
 	if (fd >= 0)
 		close(fd);
-	if (fout)
-		fclose(fout);
+	if (opts.fout)
+		fclose(opts.fout);
 	if (!ret)
 		printf("%s generated successfully.\n", opts.output_filename);
 	return ret;
@@ -82,6 +81,9 @@ 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.8.0



More information about the CRIU mailing list