[CRIU] Re: [PATCH 6/6] log: Add log-levels

Cyrill Gorcunov gorcunov at openvz.org
Fri Feb 17 13:12:47 EST 2012


This one should do the trick

	Cyrill
---
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Fri, 17 Feb 2012 22:11:50 +0400
Subject: [PATCH] log: Add log-levels

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Documentation/crtools.txt |    6 ++++-
 crtools.c                 |   30 ++++++++++++++++++++++++++-
 include/log.h             |   49 ++++++++++++++++++--------------------------
 log.c                     |   20 ++++++++++++++---
 sockets.c                 |   16 +++++++-------
 5 files changed, 78 insertions(+), 43 deletions(-)

diff --git a/Documentation/crtools.txt b/Documentation/crtools.txt
index c1f6bcc..92c5fa9 100644
--- a/Documentation/crtools.txt
+++ b/Documentation/crtools.txt
@@ -11,7 +11,7 @@ crtools - checkpoint/restore in userspace
 
 SYNOPSIS
 --------
-'crtools' [-c] [-f <file>] [-d] [-n] [-o <path>] [-D <path>] [--help] <command> (-p|-t) <pid>
+'crtools' [-c] [-f <file>] [-d] [-n] [-o <path>] [-D <path>] [-v [num]] [--help] <command> (-p|-t) <pid>
 
 DESCRIPTION
 -----------
@@ -54,6 +54,10 @@ OPTIONS
 -o <file>::
     Write logging messages to 'file'.
 
+-v <num>::
+    Set logging level to 'num'. Valid options are: 0 - (silent, error messages
+    only), 1 - informative (default), 2 - debug messages.
+
 AUTHOR
 ------
 OpenVZ team.
diff --git a/crtools.c b/crtools.c
index 1305b5a..743a849 100644
--- a/crtools.c
+++ b/crtools.c
@@ -5,6 +5,7 @@
 #include <errno.h>
 #include <getopt.h>
 #include <string.h>
+#include <ctype.h>
 
 #include <fcntl.h>
 
@@ -290,8 +291,9 @@ int main(int argc, char *argv[])
 	int opt, idx;
 	int action = -1;
 	int log_inited = 0;
+	int log_level = 0;
 
-	static const char short_opts[] = "dsf:p:t:hcD:o:n:";
+	static const char short_opts[] = "dsf:p:t:hcD:o:n:v";
 
 	BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
 
@@ -344,12 +346,32 @@ int main(int argc, char *argv[])
 			if (parse_ns_string(optarg, &opts.namespaces_flags))
 				return -1;
 			break;
+		case 'v':
+			if (optind < argc - 1) {
+				char *opt = argv[optind + 1];
+
+				if (isdigit(*opt)) {
+					log_level = -atoi(opt);
+					optind++;
+				} else {
+					if (log_level >= 0)
+						log_level++;
+				}
+			} else {
+				if (log_level >= 0)
+					log_level++;
+			}
+			break;
 		case 'h':
 		default:
 			goto usage;
 		}
 	}
 
+	if (log_level < 0)
+		log_level = -log_level;
+	set_loglevel(log_level);
+
 	if (!log_inited) {
 		ret = init_log(NULL);
 		if (ret)
@@ -410,6 +432,12 @@ usage:
 
 	printk("\nAdditional common parameters:\n");
 	printk("  -D dir         save checkpoint files in specified directory\n");
+	printk("  -v [num]       set logging level\n");
+	printk("                 0 - silent (only error messages)\n");
+	printk("                 1 - informative (default)\n");
+	printk("                 2 - debug\n");
+	printk("  -vv            same as -v 1\n");
+	printk("  -vvv           same as -v 2\n");
 	printk("\n");
 
 	return -1;
diff --git a/include/log.h b/include/log.h
index e6eaaa5..7890c6b 100644
--- a/include/log.h
+++ b/include/log.h
@@ -1,40 +1,31 @@
 #ifndef LOG_H__
 #define LOG_H__
 
-extern void printk(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
-
 extern int init_log(const char *name);
 extern void fini_log(void);
 extern int get_logfd(void);
 
-#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__)
-
-#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 LOG_ERROR	(0) /* Errors only */
+#define LOG_INFO	(1) /* And informative */
+#define LOG_DEBUG	(2) /* And even debug messages */
 
-#define die(fmt, ...)						\
-	do {							\
-		printk("die (%s:%d): " fmt, __FILE__,		\
-			__LINE__, ##__VA_ARGS__);		\
-		exit(1);					\
-	} while (0)
+extern void set_loglevel(unsigned int level);
+extern void printk_level(unsigned int level, const char *format, ...)
+	__attribute__ ((__format__ (__printf__, 2, 3)));
 
-#define pr_perror(fmt, ...)					\
-	do {							\
-		pr_err(fmt ": %m\n", ##__VA_ARGS__);		\
-	} while (0)
+#define printk(fmt, ...)	\
+	printk_level(LOG_INFO, fmt, ##__VA_ARGS__)
+#define pr_info(fmt, ...)	\
+	printk_level(LOG_INFO,  fmt, ##__VA_ARGS__)
+#define pr_err(fmt, ...)	\
+	printk_level(LOG_ERROR, "Error (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_panic(fmt, ...)	\
+	printk_level(LOG_ERROR, "Panic (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_warning(fmt, ...)	\
+	printk_level(LOG_INFO,	"Warn  (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
+#define pr_debug(fmt, ...)	\
+	printk_level(LOG_DEBUG,	fmt, ##__VA_ARGS__)
+#define pr_perror(fmt, ...)	\
+	pr_err(fmt ": %m\n", ##__VA_ARGS__)
 
 #endif /* LOG_H__ */
diff --git a/log.c b/log.c
index 3776873..f4be808 100644
--- a/log.c
+++ b/log.c
@@ -67,11 +67,23 @@ void fini_log(void)
 	logfd = STDERR_FILENO;
 }
 
-void printk(const char *format, ...)
+static unsigned int loglevel = LOG_INFO;
+
+void set_loglevel(unsigned int level)
+{
+	if (!level)
+		loglevel = LOG_ERROR;
+	else
+		loglevel = level;
+}
+
+void printk_level(unsigned int level, const char *format, ...)
 {
 	va_list params;
 
-	va_start(params, format);
-	vdprintf(get_logfd(), format, params);
-	va_end(params);
+	if (level <= loglevel) {
+		va_start(params, format);
+		vdprintf(get_logfd(), format, params);
+		va_end(params);
+	}
 }
diff --git a/sockets.c b/sockets.c
index 0609e70..2666968 100644
--- a/sockets.c
+++ b/sockets.c
@@ -141,7 +141,7 @@ static void show_one_inet(const char *act, const struct inet_sk_desc *sk)
 		pr_perror("Failed to translate address");
 	}
 
-	dprintk("\t%s: ino %d family %d type %d port %d "
+	pr_debug("\t%s: ino %d family %d type %d port %d "
 		"state %d src_addr %s\n",
 		act, sk->sd.ino, sk->sd.family, sk->type, sk->src_port,
 		sk->state, src_addr);
@@ -156,7 +156,7 @@ static void show_one_inet_img(const char *act, const struct inet_sk_entry *e)
 		pr_perror("Failed to translate address");
 	}
 
-	dprintk("\t%s: fd %d family %d type %d proto %d port %d "
+	pr_debug("\t%s: fd %d family %d type %d proto %d port %d "
 		"state %d src_addr %s\n",
 		act, e->fd, e->family, e->type, e->proto, e->src_port,
 		e->state, src_addr);
@@ -164,20 +164,20 @@ static void show_one_inet_img(const char *act, const struct inet_sk_entry *e)
 
 static void show_one_unix(char *act, const struct unix_sk_desc *sk)
 {
-	dprintk("\t%s: ino %d type %d state %d name %s\n",
+	pr_debug("\t%s: ino %d type %d state %d name %s\n",
 		act, sk->sd.ino, sk->type, sk->state, sk->name);
 
 	if (sk->nr_icons) {
 		int i;
 
 		for (i = 0; i < sk->nr_icons; i++)
-			dprintk("\t\ticon: %4d\n", sk->icons[i]);
+			pr_debug("\t\ticon: %4d\n", sk->icons[i]);
 	}
 }
 
 static void show_one_unix_img(const char *act, const struct unix_sk_entry *e)
 {
-	dprintk("\t%s: fd %d type %d state %d name %d bytes\n",
+	pr_debug("\t%s: fd %d type %d state %d name %d bytes\n",
 		act, e->fd, e->type, e->state, e->namelen);
 }
 
@@ -328,7 +328,7 @@ static int dump_one_unix(const struct socket_desc *_sk, int fd,
 		ue.flags	|= USK_INFLIGHT;
 		ue.peer		= e->sk_desc->sd.ino;
 
-		dprintk("\t\tFixed inflight socket %d peer %d)\n",
+		pr_debug("\t\tFixed inflight socket %d peer %d)\n",
 				ue.id, ue.peer);
 	}
 
@@ -521,7 +521,7 @@ static int unix_collect_one(const struct unix_diag_msg *m,
 
 			SK_HASH_LINK(unix_listen_icons, d->icons[i], e);
 
-			dprintk("\t\tCollected icon %d\n", d->icons[i]);
+			pr_debug("\t\tCollected icon %d\n", d->icons[i]);
 
 			e->peer_ino	= d->icons[i];
 			e->sk_desc	= d;
@@ -697,7 +697,7 @@ enum {
 
 static void unix_show_job(const char *type, int fd, int id)
 {
-	dprintk("%s job fd %d id %d\n", type, fd, id);
+	pr_debug("%s job fd %d id %d\n", type, fd, id);
 }
 
 static struct unix_conn_job *conn_jobs;
-- 
1.7.7.6



More information about the CRIU mailing list