[CRIU] [PATCH 1/2] Move printk helpers to a separate file

Cyrill Gorcunov gorcunov at openvz.org
Mon Feb 6 16:36:10 EST 2012


util.c is overdoes already so move printk
helpers to own printk.[ch].

This patch also introduces printk_perror
helper which may handle '\n' at the end
of the message format and append it after
the %m specificator (see commit
0b237ae9f2ad49499f7c9c1b3713c13267aca309
for rationale).

The idea behind is to eliminate special
call-form of pr_perror (which at moment
implies that \n will be appended implicitly)
and do not print \n out until explicitly
stated.

Based-on-patch-from: Kir Kolyshkin <kir at openvz.org>
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile         |    1 +
 include/printk.h |   34 ++++++++++++++++++++++++++++++++++
 include/util.h   |   26 +-------------------------
 printk.c         |   45 +++++++++++++++++++++++++++++++++++++++++++++
 util.c           |   10 +---------
 5 files changed, 82 insertions(+), 34 deletions(-)
 create mode 100644 include/printk.h
 create mode 100644 printk.c

diff --git a/Makefile b/Makefile
index 497ca4f..c8d5b68 100644
--- a/Makefile
+++ b/Makefile
@@ -34,6 +34,7 @@ OBJS		+= proc_parse.o
 OBJS		+= cr-dump.o
 OBJS		+= cr-show.o
 OBJS		+= util.o
+OBJS		+= printk.o
 OBJS		+= util-net.o
 OBJS		+= sysctl.o
 OBJS		+= ptrace.o
diff --git a/include/printk.h b/include/printk.h
new file mode 100644
index 0000000..7cd648c
--- /dev/null
+++ b/include/printk.h
@@ -0,0 +1,34 @@
+#ifndef CR_PRINTK_H__
+#define CR_PRINTK_H__
+
+#include <sys/types.h>
+
+#include "compiler.h"
+#include "types.h"
+
+extern void printk(const char *format, ...)
+	__attribute__ ((__format__ (__printf__, 1, 2)));
+
+extern void printk_perror(const char *format, ...)
+	__attribute__ ((__format__ (__printf__, 1, 2)));
+
+#define pr_info(fmt, ...)	printk(fmt, ##__VA_ARGS__)
+#define pr_err(fmt, ...)	printk("Error (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_panic(fmt, ...)	printk("PANIC (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_warning(fmt, ...)	printk("Warning (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_perror(fmt, ...)	pr_err(fmt ": %m\n", ##__VA_ARGS__)
+
+#ifdef CR_DEBUG
+#define pr_debug(fmt, ...)					\
+	do {							\
+		printk("%s:%d:%s: " fmt,			\
+		       __FILE__, __LINE__,__func__,		\
+		       ##__VA_ARGS__);				\
+	} while (0)
+#define dprintk(fmt, ...)	printk(fmt, ##__VA_ARGS__)
+#else
+#define pr_debug(fmt, ...)
+#define dprintk(fmt, ...)
+#endif
+
+#endif /* CR_PRINTK_H__ */
diff --git a/include/util.h b/include/util.h
index e9c1832..72f170a 100644
--- a/include/util.h
+++ b/include/util.h
@@ -16,8 +16,7 @@
 #include "compiler.h"
 #include "types.h"
 
-extern void printk(const char *format, ...)
-	__attribute__ ((__format__ (__printf__, 1, 2)));
+#include "printk.h"
 
 #define PREF_SHIFT_OP(pref, op, size)	((size) op (pref ##BYTES_SHIFT))
 #define KBYTES_SHIFT	10
@@ -32,11 +31,6 @@ extern void printk(const char *format, ...)
 #define MEGA(size)	PREF_SHIFT_OP(K, <<, size)
 #define GIGA(size)	PREF_SHIFT_OP(K, <<, size)
 
-#define pr_info(fmt, ...)	printk(fmt, ##__VA_ARGS__)
-#define pr_err(fmt, ...)	printk("Error (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
-#define pr_panic(fmt, ...)	printk("PANIC (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
-#define pr_warning(fmt, ...)	printk("Warning (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
-
 #define pr_err_jmp(label)					\
 	do {							\
 		printk("EJMP: %s:%d\n", __FILE__, __LINE__);	\
@@ -62,19 +56,6 @@ extern void printk(const char *format, ...)
 			pr_err_jmp(label);			\
 	} while (0)
 
-#ifdef CR_DEBUG
-#define pr_debug(fmt, ...)					\
-	do {							\
-		printk("%s:%d:%s: " fmt,			\
-		       __FILE__, __LINE__,__func__,		\
-		       ##__VA_ARGS__);				\
-	} while (0)
-#define dprintk(fmt, ...)	printk(fmt, ##__VA_ARGS__)
-#else
-#define pr_debug(fmt, ...)
-#define dprintk(fmt, ...)
-#endif
-
 #define die(fmt, ...)						\
 	do {							\
 		printk("die (%s:%d): " fmt, __FILE__,		\
@@ -82,11 +63,6 @@ extern void printk(const char *format, ...)
 		exit(1);					\
 	} while (0)
 
-#define pr_perror(fmt, ...)					\
-	do {							\
-		pr_err(fmt ": %m\n", ##__VA_ARGS__);		\
-	} while (0)
-
 #ifndef BUG_ON_HANDLER
 
 #ifdef CR_NOGLIBC
diff --git a/printk.c b/printk.c
new file mode 100644
index 0000000..335c329
--- /dev/null
+++ b/printk.c
@@ -0,0 +1,45 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include "compiler.h"
+#include "types.h"
+#include "log.h"
+
+#include "printk.h"
+
+static void vprintk(const char *format, va_list params)
+{
+	vdprintf(get_logfd(), format, params);
+}
+
+void printk_perror(const char *format, ...)
+{
+	va_list params;
+	int n = strlen(format);
+	char *buf = alloca(n + sizeof(": %m"));
+
+	strcpy(buf, format);
+	if (format[n - 1] == '\n')
+		strcpy(buf + n - 1, ": %m\n");
+	else
+		strcpy(buf + n, ": %m");
+
+	va_start(params, format);
+	vprintk(buf, params);
+	va_end(params);
+}
+
+void printk(const char *format, ...)
+{
+	va_list params;
+
+	va_start(params, format);
+	vprintk(format, params);
+	va_end(params);
+}
diff --git a/util.c b/util.c
index 1bb00a0..53abb2e 100644
--- a/util.c
+++ b/util.c
@@ -33,20 +33,12 @@
 #include "compiler.h"
 #include "types.h"
 #include "list.h"
+#include "printk.h"
 #include "util.h"
 #include "log.h"
 
 #include "crtools.h"
 
-void printk(const char *format, ...)
-{
-	va_list params;
-
-	va_start(params, format);
-	vdprintf(get_logfd(), format, params);
-	va_end(params);
-}
-
 void hex_dump(void *addr, unsigned long len)
 {
 	unsigned char *p = addr;
-- 
1.7.7.6



More information about the CRIU mailing list