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

Cyrill Gorcunov gorcunov at openvz.org
Fri Feb 17 12:40:02 EST 2012


Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Documentation/crtools.txt |    6 ++++-
 crtools.c                 |   30 ++++++++++++++++++++++++++-
 include/log.h             |   49 +++++++++++++++++++++-----------------------
 log.c                     |   20 ++++++++++++++---
 4 files changed, 73 insertions(+), 32 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..1547d88 100644
--- a/include/log.h
+++ b/include/log.h
@@ -1,40 +1,37 @@
 #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__)
+#define LOG_ERROR	(1) /* Errors only */
+#define LOG_INFO	(3) /* And informative */
+#define LOG_DEBUG	(7) /* And even debug messages */
+
+extern void set_loglevel(unsigned int level);
+extern void printk_level(unsigned int level, const char *format, ...)
+	__attribute__ ((__format__ (__printf__, 2, 3)));
+
+#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,	"Debug (%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__)
+#define dprintk(fmt, ...)	printk_level(LOG_DEBUG, fmt, ##__VA_ARGS__)
 #else
-#define pr_debug(fmt, ...)
 #define dprintk(fmt, ...)
 #endif
 
-#define die(fmt, ...)						\
-	do {							\
-		printk("die (%s:%d): " fmt, __FILE__,		\
-			__LINE__, ##__VA_ARGS__);		\
-		exit(1);					\
-	} while (0)
-
-#define pr_perror(fmt, ...)					\
-	do {							\
-		pr_err(fmt ": %m\n", ##__VA_ARGS__);		\
-	} while (0)
-
 #endif /* LOG_H__ */
diff --git a/log.c b/log.c
index 3776873..1f79c18 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 |= ((2 << level) - 1);
+}
+
+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) == level) {
+		va_start(params, format);
+		vdprintf(get_logfd(), format, params);
+		va_end(params);
+	}
 }
-- 
1.7.7.6



More information about the CRIU mailing list