[sheepdog] [PATCH RFC 3/3] logger: selectable log format

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Jan 28 12:15:26 CET 2013


This patch adds new option '-F' (or '--log-format') to sheep. With
this option, users can specify a log format. e.g.
sheep -F json /store/sheep     # format log with json
sheep -F default /store/sheep  # format with default style

Without -F option, sheep selects a default style for formatting.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/logger.h |    2 +-
 lib/logger.c     |   31 +++++++++++++++++++++++++++++--
 sheep/sheep.c    |    8 +++++++-
 3 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/include/logger.h b/include/logger.h
index a972dd0..eac4655 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -22,7 +22,7 @@
 #define MAX_THREAD_NAME_LEN	20
 
 int log_init(const char *progname, int size, bool to_stdout, int level,
-		    char *outfile);
+	char *outfile, const char *format_name);
 void log_close(void);
 void dump_logmsg(void *);
 void log_write(int prio, const char *func, int line, const char *fmt, ...)
diff --git a/lib/logger.c b/lib/logger.c
index 54464ad..f5c6a3e 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -67,6 +67,23 @@ struct logmsg {
 	char str[0];
 };
 
+struct log_format {
+	const char *name;
+	int (*do_log)(char *, size_t, const struct logmsg *);
+	struct list_head list;
+};
+
+#define log_format_register(n, log_func)				\
+	static void __attribute__((constructor))			\
+	regist_ ## log_func(void) {					\
+		static struct log_format f =				\
+			{ .name = n, .do_log = log_func };		\
+		list_add(&f.list, &log_formats);			\
+}
+
+static LIST_HEAD(log_formats);
+static struct log_format *format;
+
 static int log_fd = -1;
 static __thread const char *worker_name;
 static __thread int worker_idx;
@@ -182,6 +199,7 @@ static notrace int default_log_format(char *buff, size_t size,
 
 	return p - buff;
 }
+log_format_register("default", default_log_format);
 
 static notrace int json_log_format(char *buff, size_t size,
 				const struct logmsg *msg)
@@ -229,6 +247,7 @@ static notrace int json_log_format(char *buff, size_t size,
 
 	return p - buff;
 }
+log_format_register("json", json_log_format);
 
 /*
  * this one can block under memory pressure
@@ -238,7 +257,7 @@ static notrace void log_syslog(const struct logmsg *msg)
 	char str[MAX_MSG_SIZE * 2];
 
 	memset(str, 0, MAX_MSG_SIZE * 2);
-	default_log_format(str, MAX_MSG_SIZE, msg);
+	format->do_log(str, MAX_MSG_SIZE, msg);
 	if (log_fd >= 0)
 		xwrite(log_fd, str, strlen(str));
 	else
@@ -465,10 +484,18 @@ static notrace void logger(char *log_dir, char *outfile)
 }
 
 notrace int log_init(const char *program_name, int size, bool to_stdout,
-		     int level, char *outfile)
+		int level, char *outfile, const char *format_name)
 {
 	char log_dir[PATH_MAX], tmp[PATH_MAX];
 
+	list_for_each_entry(format, &log_formats, list) {
+		if (!strcmp(format->name, format_name))
+			goto format_found;
+	}
+
+	panic("invalid format: %s\n", format_name);
+format_found:
+
 	log_level = level;
 
 	log_name = program_name;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index f2c7bcd..25c1867 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -59,6 +59,7 @@ static struct sd_option sheep_options[] = {
 	{'w', "write-cache", true, "specify the cache type"},
 	{'y', "myaddr", true, "specify the address advertised to other sheep"},
 	{'z', "zone", true, "specify the zone id"},
+	{'F', "log-format", true, "specify log format"},
 	{ 0, NULL, false, NULL },
 };
 
@@ -395,6 +396,7 @@ int main(int argc, char **argv)
 	int64_t zone = -1, free_space = 0;
 	struct cluster_driver *cdrv;
 	struct option *long_options;
+	const char *log_format = "default";
 
 	signal(SIGPIPE, SIG_IGN);
 
@@ -530,6 +532,9 @@ int main(int argc, char **argv)
 				PACKAGE_VERSION);
 			exit(0);
 			break;
+		case 'F':
+			log_format = optarg;
+			break;
 		default:
 			usage(1);
 			break;
@@ -560,7 +565,8 @@ int main(int argc, char **argv)
 	if (is_daemon && daemon(0, 0))
 		exit(1);
 
-	ret = log_init(program_name, LOG_SPACE_SIZE, to_stdout, log_level, path);
+	ret = log_init(program_name, LOG_SPACE_SIZE, to_stdout, log_level, path,
+		log_format);
 	if (ret)
 		exit(1);
 
-- 
1.7.2.5




More information about the sheepdog mailing list