Commit ef97467d authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

log: Add log-levels

Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Acked-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent fe99f501
...@@ -11,7 +11,7 @@ crtools - checkpoint/restore in userspace ...@@ -11,7 +11,7 @@ crtools - checkpoint/restore in userspace
SYNOPSIS 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 DESCRIPTION
----------- -----------
...@@ -54,6 +54,10 @@ OPTIONS ...@@ -54,6 +54,10 @@ OPTIONS
-o <file>:: -o <file>::
Write logging messages to '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 AUTHOR
------ ------
OpenVZ team. OpenVZ team.
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -290,8 +291,9 @@ int main(int argc, char *argv[]) ...@@ -290,8 +291,9 @@ int main(int argc, char *argv[])
int opt, idx; int opt, idx;
int action = -1; int action = -1;
int log_inited = 0; 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); BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
...@@ -344,12 +346,32 @@ int main(int argc, char *argv[]) ...@@ -344,12 +346,32 @@ int main(int argc, char *argv[])
if (parse_ns_string(optarg, &opts.namespaces_flags)) if (parse_ns_string(optarg, &opts.namespaces_flags))
return -1; return -1;
break; 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': case 'h':
default: default:
goto usage; goto usage;
} }
} }
if (log_level < 0)
log_level = -log_level;
set_loglevel(log_level);
if (!log_inited) { if (!log_inited) {
ret = init_log(NULL); ret = init_log(NULL);
if (ret) if (ret)
...@@ -410,6 +432,12 @@ usage: ...@@ -410,6 +432,12 @@ usage:
printk("\nAdditional common parameters:\n"); printk("\nAdditional common parameters:\n");
printk(" -D dir save checkpoint files in specified directory\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"); printk("\n");
return -1; return -1;
......
#ifndef LOG_H__ #ifndef LOG_H__
#define LOG_H__ #define LOG_H__
extern void printk(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern int init_log(const char *name); extern int init_log(const char *name);
extern void fini_log(void); extern void fini_log(void);
extern int get_logfd(void); extern int get_logfd(void);
#define pr_info(fmt, ...) printk(fmt, ##__VA_ARGS__) #define LOG_ERROR (0) /* Errors only */
#define pr_err(fmt, ...) printk("Error (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__) #define LOG_WARN (1) /* Informative */
#define pr_panic(fmt, ...) printk("PANIC (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__) #define LOG_DEBUG (2) /* Debug ones */
#define pr_warning(fmt, ...) printk("Warning (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
extern void set_loglevel(unsigned int level);
#ifdef CR_DEBUG extern void printk_level(unsigned int level, const char *format, ...)
#define pr_debug(fmt, ...) \ __attribute__ ((__format__ (__printf__, 2, 3)));
do { \
printk("%s:%d:%s: " fmt, \ #define printk(fmt, ...) \
__FILE__, __LINE__,__func__, \ printk_level(LOG_WARN, fmt, ##__VA_ARGS__)
##__VA_ARGS__); \
} while (0) #define pr_info(fmt, ...) \
#define dprintk(fmt, ...) printk(fmt, ##__VA_ARGS__) printk_level(LOG_WARN, fmt, ##__VA_ARGS__)
#else
#define pr_debug(fmt, ...) #define pr_err(fmt, ...) \
#define dprintk(fmt, ...) printk_level(LOG_ERROR, "Error (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define pr_panic(fmt, ...) \
#define die(fmt, ...) \ printk_level(LOG_ERROR, "Panic (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
do { \
printk("die (%s:%d): " fmt, __FILE__, \ #define pr_warning(fmt, ...) \
__LINE__, ##__VA_ARGS__); \ printk_level(LOG_WARN, "Warn (%s:%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
exit(1); \
} while (0) #define pr_debug(fmt, ...) \
printk_level(LOG_DEBUG, fmt, ##__VA_ARGS__)
#define pr_perror(fmt, ...) \
do { \ #define pr_perror(fmt, ...) \
pr_err(fmt ": %m\n", ##__VA_ARGS__); \ pr_err(fmt ": %m\n", ##__VA_ARGS__)
} while (0)
#endif /* LOG_H__ */ #endif /* LOG_H__ */
...@@ -67,11 +67,23 @@ void fini_log(void) ...@@ -67,11 +67,23 @@ void fini_log(void)
logfd = STDERR_FILENO; logfd = STDERR_FILENO;
} }
void printk(const char *format, ...) static unsigned int loglevel = LOG_WARN;
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_list params;
va_start(params, format); if (level <= loglevel) {
vdprintf(get_logfd(), format, params); va_start(params, format);
va_end(params); vdprintf(get_logfd(), format, params);
va_end(params);
}
} }
...@@ -141,7 +141,7 @@ static void show_one_inet(const char *act, const struct inet_sk_desc *sk) ...@@ -141,7 +141,7 @@ static void show_one_inet(const char *act, const struct inet_sk_desc *sk)
pr_perror("Failed to translate address"); 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", "state %d src_addr %s\n",
act, sk->sd.ino, sk->sd.family, sk->type, sk->src_port, act, sk->sd.ino, sk->sd.family, sk->type, sk->src_port,
sk->state, src_addr); sk->state, src_addr);
...@@ -156,7 +156,7 @@ static void show_one_inet_img(const char *act, const struct inet_sk_entry *e) ...@@ -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"); 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", "state %d src_addr %s\n",
act, e->fd, e->family, e->type, e->proto, e->src_port, act, e->fd, e->family, e->type, e->proto, e->src_port,
e->state, src_addr); e->state, src_addr);
...@@ -164,20 +164,20 @@ static void show_one_inet_img(const char *act, const struct inet_sk_entry *e) ...@@ -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) 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); act, sk->sd.ino, sk->type, sk->state, sk->name);
if (sk->nr_icons) { if (sk->nr_icons) {
int i; int i;
for (i = 0; i < sk->nr_icons; 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) 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); 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, ...@@ -328,7 +328,7 @@ static int dump_one_unix(const struct socket_desc *_sk, int fd,
ue.flags |= USK_INFLIGHT; ue.flags |= USK_INFLIGHT;
ue.peer = e->sk_desc->sd.ino; 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); ue.id, ue.peer);
} }
...@@ -521,7 +521,7 @@ static int unix_collect_one(const struct unix_diag_msg *m, ...@@ -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); 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->peer_ino = d->icons[i];
e->sk_desc = d; e->sk_desc = d;
...@@ -697,7 +697,7 @@ enum { ...@@ -697,7 +697,7 @@ enum {
static void unix_show_job(const char *type, int fd, int id) 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; static struct unix_conn_job *conn_jobs;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment