[sheepdog] [PATCH v6 1/5] logger: rename and change a role of select_log_formatter()

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Thu Feb 21 09:36:20 CET 2013


This patch renames select_log_formatter(). The new name is
early_log_init().

early_log_init() is also used for selecting log formatter, but it
also used for registering information of logger user. The information
is registered as 2nd parameter of the function which is typed struct
logger_user_info.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
v5: writing initial log entry with user specified callback is removed

 include/logger.h |    7 ++++++-
 lib/logger.c     |   27 +++++++++++++++++++++++----
 sheep/sheep.c    |   11 ++++++++++-
 3 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/include/logger.h b/include/logger.h
index 309b484..0ed457d 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -21,7 +21,12 @@
 #define MAX_MSG_SIZE 256
 #define MAX_THREAD_NAME_LEN	20
 
-void select_log_formatter(const char *format_name);
+struct logger_user_info {
+	int port;
+};
+
+void early_log_init(const char *format_name,
+		struct logger_user_info *user_info);
 int log_init(const char *progname, int size, bool to_stdout, int level,
 	char *outfile);
 void log_close(void);
diff --git a/lib/logger.c b/lib/logger.c
index aa465bf..987379d 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -36,6 +36,8 @@
 #include "logger.h"
 #include "util.h"
 
+struct logger_user_info *logger_user_info;
+
 static void dolog(int prio, const char *func, int line, const char *fmt,
 		  va_list ap) __attribute__ ((format (printf, 4, 0)));
 
@@ -227,7 +229,22 @@ static notrace int json_log_formatter(char *buff, size_t size,
 	int i, body_len;
 	char *p = buff;
 
-	snprintf(p, size, "{");
+	snprintf(p, size, "{ \"user_info\": {");
+	p += strlen(p);
+
+	snprintf(p, size - strlen(buff), "\"program_name\": \"%s\", ",
+		log_name);
+	p += strlen(p);
+
+	assert(logger_user_info);
+	snprintf(p, size - strlen(buff), "\"port\": %d",
+		logger_user_info->port);
+	p += strlen(p);
+
+	snprintf(p, size - strlen(buff), "},");
+	p += strlen(p);
+
+	snprintf(p, size - strlen(buff), "\"body\": {");
 	p += strlen(p);
 
 	snprintf(p, size - strlen(buff), "\"second\": %lu", msg->tv.tv_sec);
@@ -254,7 +271,7 @@ static notrace int json_log_formatter(char *buff, size_t size,
 	snprintf(p, size - strlen(buff), ", \"line\": %d", msg->line);
 	p += strlen(p);
 
-	snprintf(p, size - strlen(buff), ", \"body\": \"");
+	snprintf(p, size - strlen(buff), ", \"msg\": \"");
 	p += strlen(p);
 
 	body_len = strlen(msg->str) - 1;
@@ -265,7 +282,7 @@ static notrace int json_log_formatter(char *buff, size_t size,
 		*p++ = msg->str[i];
 	}
 
-	snprintf(p, size - strlen(buff), "\"}\n");
+	snprintf(p, size - strlen(buff), "\"} }\n");
 	p += strlen(p);
 
 	return p - buff;
@@ -518,10 +535,12 @@ static notrace void logger(char *log_dir, char *outfile)
 	exit(0);
 }
 
-void select_log_formatter(const char *format_name)
+void early_log_init(const char *format_name, struct logger_user_info *user_info)
 {
 	struct log_format *f;
 
+	logger_user_info = user_info;
+
 	list_for_each_entry(f, &log_formats, list) {
 		if (!strcmp(f->name, format_name)) {
 			format = f;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 316d4de..0d05d0b 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -432,6 +432,8 @@ 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";
+	static struct logger_user_info sheep_info;
 
 	signal(SIGPIPE, SIG_IGN);
 
@@ -571,7 +573,7 @@ int main(int argc, char **argv)
 			exit(0);
 			break;
 		case 'F':
-			select_log_formatter(optarg);
+			log_format = optarg;
 			break;
 		default:
 			usage(1);
@@ -579,6 +581,13 @@ int main(int argc, char **argv)
 		}
 	}
 
+	/*
+	 * early_log_init() must be called before any calling of
+	 * sd_printf() series
+	 */
+	sheep_info.port = port;
+	early_log_init(log_format, &sheep_info);
+
 	if (nr_vnodes == 0) {
 		sys->gateway_only = true;
 		sys->disk_space = 0;
-- 
1.7.2.5




More information about the sheepdog mailing list